本文整理汇总了Python中rbm.RBM.train方法的典型用法代码示例。如果您正苦于以下问题:Python RBM.train方法的具体用法?Python RBM.train怎么用?Python RBM.train使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rbm.RBM
的用法示例。
在下文中一共展示了RBM.train方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from rbm import RBM [as 别名]
# 或者: from rbm.RBM import train [as 别名]
class SFG:
def __init__(self):
self.image_width = self.image_height = 28
self.visible_units = self.image_width * self.image_height
self.hidden_units = self.visible_units / 10
self.rbm = RBM(self.visible_units, self.hidden_units)
#assumes there are only training images in the training_folder
def train(self, training_folder, epochs = 500):
data = []
for training_image in os.listdir(training_folder):
image = pil.open(training_folder + '/' + training_image)
image = self.array_for_image(image)
data.append(image)
self.rbm.train(data, epochs)
#takes a pil Image and returns an arary of 1s and 0s
def array_for_image(self, image):
return np.array(image.convert("L")).flatten() / 255
def regen_image(self, image, samples):
data = self.array_for_image(image)
(v, _) = self.rbm.regenerate([data],samples)
return self.image_for_array(v[0])
def image_for_array(self, array):
img_array = []
for row in range(0, self.image_height):
img_array.append(array[row * self.image_width : (row+1) * self.image_width])
img_array = np.asarray(img_array, np.uint8) * 255
return pil.fromarray(img_array)
示例2: RBMTest
# 需要导入模块: from rbm import RBM [as 别名]
# 或者: from rbm.RBM import train [as 别名]
class RBMTest(unittest.TestCase):
def setUp(self):
self.rbm = RBM(10,10)
def can_make_rbm_test(self):
rbm = RBM(10, 10)
def logistic_function_test(self):
self.assertEquals(self.rbm.logistic(0), 1)
def train_throws_error_with_inconsistent_matrix_sizes_test(self):
with self.assertRaises(TypeError):
self.rbm.train([[1,0,1,1,1,1,0,1], [1,1,1,1,0], [1,1,1,1,1,1]])
def regenerate_throws_error_with_inconsistent_matrix_sizes_test(self):
with self.assertRaises(TypeError):
self.rbm.regenerate([[1,0,1,1,1,1,0,1], [1,1,1,1,0], [1,1,1,1,1,1]])
示例3: train
# 需要导入模块: from rbm import RBM [as 别名]
# 或者: from rbm.RBM import train [as 别名]
def train(self):
print "train rbm level 1"
rbm = RBM(self.converter.dimensionality, 1000)
rbm.train(self.converter.train_images, max_epochs = 10, batch = 100)
hidden_probs1 = rbm.hidden_probs
self.pickle_dumps(rbm.weights, 'l1_w.pkl')
self.pickle_dumps(rbm.hidden_bias, 'l1_hb.pkl')
self.pickle_dumps(rbm.visible_bias, 'l1_vb.pkl')
del rbm
del self.converter
print "train rbm level 1 end\n"
print "train rbm level 2"
rbm_l2 = RBM(1000, 500)
rbm_l2.train(hidden_probs1, max_epochs = 10, batch = 100)
hidden_probs2 = rbm_l2.hidden_probs
self.pickle_dumps(rbm_l2.weights, 'l2_w.pkl')
self.pickle_dumps(rbm_l2.hidden_bias, 'l2_hb.pkl')
self.pickle_dumps(rbm_l2.visible_bias, 'l2_vb.pkl')
del rbm_l2
del hidden_probs1
print "train rbm level 2 end\n"
print "train rbm level 3"
rbm_l3 = RBM(500, 250)
rbm_l3.train(hidden_probs2, max_epochs = 10, batch = 100)
hidden_probs3 = rbm_l3.hidden_probs
self.pickle_dumps(rbm_l3.weights, 'l3_w.pkl')
self.pickle_dumps(rbm_l3.hidden_bias, 'l3_hb.pkl')
self.pickle_dumps(rbm_l3.visible_bias, 'l3_vb.pkl')
del rbm_l3
del hidden_probs2
print "train rbm level 3 end\n"
print "train rbm level 4"
rbm_l4 = RBM(250, 30, isLinear = True)
rbm_l4.train(hidden_probs3, max_epochs = 10, batch = 100)
hidden_top = rbm_l4.hidden_probs
self.pickle_dumps(rbm_l4.weights, 'l4_w.pkl')
self.pickle_dumps(rbm_l4.hidden_bias, 'l4_hb.pkl')
self.pickle_dumps(rbm_l4.visible_bias, 'l4_vb.pkl')
del rbm_l4
del hidden_probs3
print "train rbm level 4 end\n"
示例4: train
# 需要导入模块: from rbm import RBM [as 别名]
# 或者: from rbm.RBM import train [as 别名]
def train(dim_h, lrate, max_epoches, batch_size, train_ratio):
"""
traing a model and save to disk
"""
result_file = "mnist_%s.pkl" % strftime("%b_%d_%H_%M_%S", gmtime())
if os.access(result_file, os.R_OK):
raise RuntimeError("%s already exists" % result_file)
data = load_digits()
learner = RBM.train(
data, dim_h=dim_h, lrate=lrate, max_epoches=max_epoches, batch_size=batch_size, train_ratio=train_ratio
)
with open(result_file, "wb") as handle:
pickle.dump(learner, handle)
print('learner sleeping at "%s"' % result_file)
示例5: range
# 需要导入模块: from rbm import RBM [as 别名]
# 或者: from rbm.RBM import train [as 别名]
args = parser.parse_args()
# Data pre-processing using pandas
trainDataFrame = pd.read_csv(args.trainFile,delim_whitespace=True,header=None,usecols=[0,1,2])
trainDataFrame[2] = trainDataFrame[2].apply(lambda x: 1 if x>2 else 0)
trainMatrix=trainDataFrame.as_matrix()
trainArray = np.ndarray(shape=(totalUsers,totalMovies), dtype=int)
row,column = trainMatrix.shape
# Converting raw dataframe into training numpy array
for i in range (0, row):
userID = trainMatrix[i][0] -1
movieID = trainMatrix[i][1] -1
trainArray[userID][movieID]=trainMatrix[i][2]
# training
rbmObject = RBM(num_visible = totalMovies,num_hidden = args.hiddenUnits)
rbmObject.train(trainArray, max_epochs = args.epochs)
print(rbmObject.weights)
# pickling
modelName = "model-"+str(args.epochs)+"-"+str(args.hiddenUnits)+".p"
print modelName
fp = open( modelName, "wb" )
for obj in [infoMatrix, movieMatrix, rbmObject]:
pickle.dump(obj, fp, protocol=pickle.HIGHEST_PROTOCOL)
print 'Model trained and pickled'
示例6: print
# 需要导入模块: from rbm import RBM [as 别名]
# 或者: from rbm.RBM import train [as 别名]
#ipdb.set_trace()
print('reading ratings file')
ratings = pd.read_csv('ratings_liked.csv',dtype = {'userId': np.int32, 'movieId': np.int32,'liked':np.bool})
movies = pd.read_csv('movies.csv')
print('creating one hot encoding')
ratings_wide = pd.pivot_table(ratings, values='liked', index=['userId'],columns=['movieId'], aggfunc=np.sum)
from rbm import RBM
vi_unit = ratings_wide.shape[1]
rbm = RBM(num_visible = vi_unit, num_hidden = 500)
ratings_wide = ratings_wide.fillna(0)
ratings_wide = ratings_wide.astype(int)
training_data = ratings_wide.as_matrix()
print('starting training')
rbm.train(training_data, max_epochs = 20) # Don't run the training for more than 5000 epochs.
while(True):
userInput = raw_input('enter "run" to predict: ')
if userInput=="run":
user_liked =[]
with open(serverPath+'user_liked.json', 'r') as infile:
user_liked = json.load(infile)
#ipdb.set_trace()
user_liked = [ int(i) for i in re.sub(r"\[|]","",user_liked).split(',')]
user_liked_vec = [int(j) for j in [i in user_liked for i in pd.Series(ratings_wide.columns) ]]
visible_data = np.array([user_liked_vec])
hidden = rbm.run_visible(visible_data)
recom = rbm.run_hidden(hidden)