(Python 3)
1 ImportNumPy2 fromSciPyImportSparse as S3 fromMatplotlibImportPyplot as Plt4 fromScipy.sparse.csrImportCsr_matrix5 ImportPandas6 7 defnormalize (x):8V =x.copy ()9V-= X.min (Axis=1). Reshape (x.shape[0],1)TenV/= V.max (Axis=1). Reshape (x.shape[0],1) One returnV A - defsigmoid (x): - #return x* (x > 0) the #return Numpy.tanh (x) - return1.0/(1+numpy.exp (-x)) - - classRBM (): + def __init__(Self, N_visible=none, N_hidden=none, w=none, learning_rate = 0.1, weight_decay=1,cd_steps=1,momentum=0.5): - ifW = =None: +Self. W = Numpy.random.uniform (-.1,0.1, (n_visible, N_hidden))/numpy.sqrt (n_visible +N_hidden) ASelf. W = Numpy.insert (self. W, 0, 0, axis = 1) atSelf. W = Numpy.insert (self. W, 0, 0, Axis =0) - Else: -Self. w=W -Self.learning_rate =learning_rate -Self.momentum =Momentum -Self.last_change =0 inSelf.last_update =0 -Self.cd_steps =cd_steps toSelf.epoch =0 +Self.weight_decay =Weight_decay -Self. Errors = [] the * $ defFit (self, Input, Max_epochs = 1, batch_size=100): Panax Notoginseng ifisinstance (Input, S.csr_matrix): -Bias = S.csr_matrix (Numpy.ones (input.shape[0], 1))) theCSR =S.hstack ([bias, Input]). TOCSR () + Else: ACSR = Numpy.insert (Input, 0, 1, 1) the forEpochinchRange (Max_epochs): +IDX =Numpy.arange (csr.shape[0]) - numpy.random.shuffle (IDX) $IDX =Idx[:batch_size] $ -Self. V_state =Csr[idx] -Self. H_state =self.activate (self. V_state) thePos_associations =Self . V_state. T.dot (self. H_state) - Wuyi forIinchRange (self.cd_steps): theSelf. V_state =self.sample (self. H_state) -Self. H_state =self.activate (self. V_state) Wu -Neg_associations =Self . V_state. T.dot (self. H_state) AboutSelf. V_state =self.sample (self. H_state) $ - #Update weights. -W_update = Self.learning_rate * ((pos_associations-neg_associations)/batch_size) -Total_change =numpy.sum (Numpy.abs (w_update)) ASelf. W + = Self.momentum * Self.last_change +w_update +Self. W *=Self.weight_decay the -Self.last_change =w_update $ theRMSE = Numpy.mean ((Csr[idx]-Self. v_state) **2) **0.5 the Self . Errors.append (RMSE) theSelf.epoch + = 1 the Print("Epoch%s:rmse =%s; | | w| |:%6.1f; Sum Update:%f"%(Self.epoch, RMSE, Numpy.sum (Numpy.abs (self). W)), Total_change)) - return Self in the defLearning_curve (self): the plt.ion () About #plt.figure () the plt.show () theE =Numpy.array (self. Errors) thePlt.plot (Pandas.rolling_mean (E, 50) [50:]) + - defActivate (self, X): the ifX.SHAPE[1]! =Self . W.shape[0]:Bayi ifisinstance (X, S.csr_matrix): theBias = S.csr_matrix (Numpy.ones (x.shape[0], 1))) theCSR =S.hstack ([bias, X]). TOCSR () - Else: -CSR = Numpy.insert (X, 0, 1, 1) the Else: theCSR =X thep =sigmoid (Csr.dot (self). W)) thep[:,0] = 1.0 - returnP the the defSample (self, H, addbias=True): the ifH.SHAPE[1] = =Self . W.shape[0]:94 ifisinstance (H, S.csr_matrix): theBias = S.csr_matrix (Numpy.ones (h.shape[0], 1))) theCSR =S.hstack ([bias, H]). TOCSR () the Else:98CSR = Numpy.insert (H, 0, 1, 1) About Else: -CSR =H101p =sigmoid (Csr.dot (self). W.T))102p[:,0] = 1103 returnP104 the if __name__=="__main__":106data = Numpy.random.uniform (0,1, (100,10))107RBM = RBM (10,15)108Rbm.fit (data,1000)109Rbm.learning_curve ()
Implementation of Boltzmann machine neural network python