本文整理汇总了Python中rbm.RBM.get_sampling_fn方法的典型用法代码示例。如果您正苦于以下问题:Python RBM.get_sampling_fn方法的具体用法?Python RBM.get_sampling_fn怎么用?Python RBM.get_sampling_fn使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rbm.RBM
的用法示例。
在下文中一共展示了RBM.get_sampling_fn方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_rbm_mnist
# 需要导入模块: from rbm import RBM [as 别名]
# 或者: from rbm.RBM import get_sampling_fn [as 别名]
#.........这里部分代码省略.........
n_vis = train_x.get_value(borrow=True).shape[1]
print numpy.linalg.matrix_rank(train_x.get_value(borrow=True))
n_train_batches = train_x.get_value(borrow=True).shape[0] / batch_size
# construct the RBM class
rbm = RBM(n_visible=n_vis, n_hidden=n_hidden, isPCD=isPCD)
train_fn = rbm.get_train_fn(train_x, batch_size)
#################################
# Training the RBM #
#################################
if not os.path.isdir(output_folder):
os.makedirs(output_folder)
os.chdir(output_folder)
plotting_time = 0.
start_time = time.clock()
import PIL.Image
from visualizer import tile_raster_images
# go through training epochs
for epoch in xrange(training_epochs):
# go through the training set
mean_cost = []
for batch_index in xrange(n_train_batches):
# for each batch, we extract the gibbs chain
new_cost = train_fn(index=batch_index, lr=learning_rate)
mean_cost += [new_cost]
print 'Training epoch %d, cost is ' % epoch, numpy.mean(mean_cost)
# monitor projected rank
projection = rbm.project(train_x)
print 'rank: ' + str(numpy.linalg.matrix_rank(projection))
# W shape is [784 500]
# Plot filters after each training epoch
plotting_start = time.clock()
# Construct image from the weight matrix
image = PIL.Image.fromarray(tile_raster_images(
X=rbm.W.get_value(borrow=True).T,
img_shape=(28, 28), tile_shape=(20, 20),
tile_spacing=(1, 1)))
image.save('filters_at_epoch_%i.png' % epoch)
plotting_stop = time.clock()
plotting_time += (plotting_stop - plotting_start)
end_time = time.clock()
pretraining_time = (end_time - start_time) - plotting_time
print ('Training took %f minutes' % (pretraining_time / 60.))
#################################
# Sampling from the RBM #
#################################
test_idx = 1
test_x, test_y = data['test']
sample_fn = rbm.get_sampling_fn(test_x, test_idx, n_chains)
print '... begin sampling'
# plot initial image first
orig_img = test_x.get_value(borrow=True)[test_idx:test_idx + 1]
image = PIL.Image.fromarray(tile_raster_images(
X=orig_img,
img_shape=(28, 28), tile_shape=(1, 1),
tile_spacing=(1, 1)))
image.save('orig_img.png')
# create a space to store the image for plotting ( we need to leave
# room for the tile_spacing as well)
image_data = numpy.zeros((29 * n_samples + 1, 29 * n_chains - 1),
dtype='uint8')
for idx in xrange(n_samples):
# generate `plot_every` intermediate samples that we discard,
# because successive samples in the chain are too correlated
vis_mf, vis_sample = sample_fn()
print ' ... plotting sample ', idx
image_data[29 * idx:29 * idx + 28, :] = tile_raster_images(
X=vis_mf,
img_shape=(28, 28),
tile_shape=(1, n_chains),
tile_spacing=(1, 1))
# construct image
image = PIL.Image.fromarray(image_data)
image.save('samples.png')
os.chdir('../')
#################################
# Projecting from the RBM #
#################################
projection = rbm.project(train_x)
print numpy.linalg.matrix_rank(projection)