原文章:http://blog.csdn.net/u010223750/article/details/61196549
1 #coding=‘utf-8‘ 2 """ 3 desc: RBM model 4 author:luchi 5 date:9/3/17 6 """ 7 import numpy as np 8 class RBM_Model(object): 9 10 def __init__(self,visible_size,hidden_size,lr): 11 self.visible_size = visible_size 12 self.hidden_size = hidden_size 13 self.lr = lr 14 np.random.seed(10) 15 self.b_v = np.random.uniform(-1,1,size=[self.visible_size])*0 16 self.W = np.random.uniform(-1,1,size=[self.visible_size,self.hidden_size]) 17 self.b_h = np.random.uniform(-1,1,size=[self.hidden_size])*0 18 19 def sampling(self,data): 20 21 """ 22 sampling h_0 using v_0 23 """ 24 25 h_0 = self.logist_fun(np.dot(data,self.W)+self.b_h) 26 #print h_0 27 h_shape = np.shape(h_0) 28 #h_0_state = h_0>(np.random.rand(h_shape[0],h_shape[1])) 29 h_0_state = h_0>(np.ones_like(h_0)*0.5) 30 31 32 """ 33 building contrastive sampling 34 """ 35 v_1 = self.logist_fun(np.dot(h_0_state,np.transpose(self.W))+self.b_v) 36 v_shape = np.shape(v_1) 37 #v_1_state = v_1>(np.random.rand(v_shape[0],v_shape[1])) 38 v_1_state = v_1>(np.ones_like(v_1)*0.5) 39 h_1 = self.logist_fun(np.dot(v_1,self.W)+self.b_h) 40 41 return h_0,v_1,h_1,v_1_state 42 43 44 def train(self,data,iter_time): 45 46 h_0,v_1,h_1,v_1_state = self.sampling(data) 47 if iter_time%100==0: 48 error = np.sum(np.mean((data-v_1) ** 2,axis=0)) 49 print("the %i iter_time error is %s" % (iter_time, error)) 50 51 """ 52 updating weight 53 """ 54 updating_matrix = [] 55 size = len(data) 56 57 for i in range(size): 58 w_v0= np.reshape(data[i],[self.visible_size,1]) 59 w_h0 = np.reshape(h_0[i],[1,self.hidden_size]) 60 w_u0 = np.dot(w_v0,w_h0) 61 62 w_v1 = np.reshape(v_1[i],[self.visible_size,1]) 63 w_h1 = np.reshape(h_1[i],[1,self.hidden_size]) 64 w_u1 = np.dot(w_v1,w_h1) 65 66 updating_matrix.append(w_u0-w_u1) 67 updating_matrix = np.mean(np.array(updating_matrix),axis=0) 68 self.W = self.W + self.lr*updating_matrix 69 self.b_v = self.b_v + self.lr*np.mean((data-v_1),axis=0) 70 self.b_h = self.b_h + self.lr*np.mean((h_0-h_1),axis=0) 71 72 def logist_fun(self,narray): 73 narray = np.clip(narray,-100,100) 74 return 1.0/(1+np.exp(-1*narray)) 75 76 def softmax(self,narray): 77 narray = np.clip(narray,-100,100) 78 num_a = np.exp(narray) 79 num_b = np.sum(num_a,axis=1) 80 return num_a*1.0/num_b[:,None] 81 82 def recomendation(self,test_data,topK): 83 84 h_0,v_1,h_1 ,_= self.sampling(test_data) 85 sorted_index = np.argsort(-1*v_1,axis=1) 86 return sorted_index[:,:topK]
RBM_Model.py
问题一:
Traceback (most recent call last):
File "E:\eclipse-workspace\RBM_MovieRecomendation-master\RBM_Model.py", line 7, in <module>
import numpy as np
ModuleNotFoundError: No module named ‘numpy‘
分析:NumPy函数库是Python开发环境的一个独立模块,而且大多数Python发行版没有默认安装NumPy数据库,因此在安装Python之后必须单独安装NumPy数据库。
解决:通过pip install numpy可直接安装numpy
时间: 2024-11-10 16:12:09