def readFromPickle(data_input_file): ww = pkl.load(open(data_input_file, 'rb'))[0] wcc = ww[0] bcc = ww[1] b_i, b_C, b_f, b_o = np.split(bcc, 4, axis=0) w_i, w_C, w_f, w_o = np.split(wcc, 4, axis=1) input_size = w_i.shape[0] - b_i.shape[0] dim = len(b_o) w_xi = w_i[:input_size, :] w_hi = w_i[input_size:, :] w_xC = w_C[:input_size, :] w_hC = w_C[input_size:, :] w_xf = w_f[:input_size, :] w_hf = w_f[input_size:, :] w_xo = w_o[:input_size, :] w_ho = w_o[input_size:, :] w = { 'Wo': [], 'Wf': [], 'Wi': [], 'Wg': [], 'Wo_x': [], 'Wf_x': [], 'Wi_x': [], 'Wg_x': [], 'bo': [], 'bf': [], 'bo': [], 'bg': [] } w['Wi_x'] = w_i[:input_size, :].T w['Wi'] = w_i[input_size:, :].T w['Wg_x'] = w_C[:input_size, :].T w['Wg'] = w_C[input_size:, :].T w['Wf_x'] = w_f[:input_size, :].T w['Wf'] = w_f[input_size:, :].T w['Wo_x'] = w_o[:input_size, :].T w['Wo'] = w_o[input_size:, :].T w['bi'] = np.array([b_i]).T w['bo'] = np.array([b_o]).T w['bf'] = np.array([b_f]).T w['bg'] = np.array([b_C]).T return w
def _correctorSteps_v2(self,y0,b): def fun(x): y = np.array([x]).T F = self._PC_F(y) return np.append(F,b.dot(y-y0),axis=0)[:,0] def dfun(x): y = np.array([x]).T dF = self._PC_DF(y) return np.append(dF,b,axis=0) (y) = fsolve(fun,y0[:,0],(),dfun) return (np.array([y]).T,0)
def dfun(x): y = np.array([x]).T dF = self._PC_DF(y) return np.append(dF,b,axis=0)
def fun(x): y = np.array([x]).T F = self._PC_F(y) return np.append(F,b.dot(y-y0),axis=0)[:,0]
epsPred = 9e-3 #Prediction error epsCorr = 1e-9 #Correction error maxSteps = 5e2 #Max number of steps thrPrune = .01 lstm = LSTMNN(None) weights = readFromPickle(data_input_file) weights, pPrune = pruneW(weights, thrPrune) lstm.updateWeigths(weights) ndim = 2 * lstm.dimOfOutput() x0 = np.zeros([ndim, 1]) lstm.setInitStat(x0) print("dim = ", str(ndim)) (t, x, eCode) = lstm.findEquilibria(epsPred, epsCorr, maxSteps) xeq = lstm.refineTheEquilibria(x) xeq = np.array([xeq]).T lstm.errorInterpreter(eCode) print("||F(xeq)-xeq|| = ", linalg.norm(xeq - lstm.F(xeq), 2)) tmp_out = {'xeq': xeq, 'prTh': thrPrune} scipyio.savemat(output_eq_file, tmp_out)
def dfun(x): y = np.append(np.array([x]).T, [1]) return self._PC_DF(np.array([y]).T)[:, :-1]
def fun(x): y = np.append(np.array([x]).T, [1]) return self._PC_F(np.array([y]).T)[:, 0]
def DF(self, x): return np.array([[2. * x[0, 0] / 3., 2.], [x[0, 0], x[1, 0]**2]])
def F(self, x): #x = x.tolist() return np.array([[(x[0, 0]**2 / 3. + 2. * x[1, 0])], [x[1, 0]**3 / 3 + x[0, 0]**2 / 2 - 2]])
epsCorr=1e-3, hStep=1e-3, maxSteps=1000): super(PolyTest, self).__init__(xinit, ndim, epsPred, epsCorr, hStep, maxSteps) def F(self, x): #x = x.tolist() return np.array([[(x[0, 0]**2 / 3. + 2. * x[1, 0])], [x[1, 0]**3 / 3 + x[0, 0]**2 / 2 - 2]]) def DF(self, x): return np.array([[2. * x[0, 0] / 3., 2.], [x[0, 0], x[1, 0]**2]]) x0 = np.array([[0], [0]]) hStep = 1e-3 epsPred = 1e-5 epsCorr = 1e-9 ndim = 2 maxSteps = 1e4 PT = PolyTest(x0, ndim, epsPred, epsCorr, hStep, maxSteps) (y, errCode) = PT.runSolver() print errCode print y PT.errorInterpreter(errCode) if ~(errCode >> 1 & 0b010): y[-1] = 1 print "Valid equilibrium::H(x,1)=", linalg.norm(PT._PC_F(y), 2) if test == 2: