annotate deep/rbm/mnistrbm.py @ 618:14ba0120baff

review response changes
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Sun, 09 Jan 2011 14:13:23 -0500
parents 45156cbf6722
children
rev   line source
348
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
1 import sys
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
2 import os, os.path
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
3
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
4 import numpy as N
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
5
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
6 import theano
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
7 import theano.tensor as T
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
8
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
9 from crbm import CRBM, ConvolutionParams
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
10
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
11 from pylearn.datasets import MNIST
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
12 from pylearn.io.image_tiling import tile_raster_images
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
13
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
14 import Image
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
15
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
16 from pylearn.io.seriestables import *
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
17 import tables
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
18
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
19 IMAGE_OUTPUT_DIR = 'img/'
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
20
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
21 REDUCE_EVERY = 100
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
22
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
23 def filename_from_time(suffix):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
24 import datetime
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
25 return str(datetime.datetime.now()) + suffix + ".png"
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
26
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
27 # Just a shortcut for a common case where we need a few
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
28 # related Error (float) series
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
29
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
30 def get_accumulator_series_array( \
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
31 hdf5_file, group_name, series_names,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
32 reduce_every,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
33 index_names=('epoch','minibatch'),
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
34 stdout_too=True,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
35 skip_hdf5_append=False):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
36 all_series = []
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
37
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
38 hdf5_file.createGroup('/', group_name)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
39
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
40 other_targets = []
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
41 if stdout_too:
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
42 other_targets = [StdoutAppendTarget()]
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
43
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
44 for sn in series_names:
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
45 series_base = \
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
46 ErrorSeries(error_name=sn,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
47 table_name=sn,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
48 hdf5_file=hdf5_file,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
49 hdf5_group='/'+group_name,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
50 index_names=index_names,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
51 other_targets=other_targets,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
52 skip_hdf5_append=skip_hdf5_append)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
53
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
54 all_series.append( \
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
55 AccumulatorSeriesWrapper( \
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
56 base_series=series_base,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
57 reduce_every=reduce_every))
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
58
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
59 ret_wrapper = SeriesArrayWrapper(all_series)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
60
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
61 return ret_wrapper
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
62
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
63 class ExperienceRbm(object):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
64 def __init__(self):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
65 self.mnist = MNIST.full()#first_10k()
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
66
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
67
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
68 datasets = load_data(dataset)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
69
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
70 train_set_x, train_set_y = datasets[0]
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
71 test_set_x , test_set_y = datasets[2]
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
72
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
73
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
74 batch_size = 100 # size of the minibatch
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
75
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
76 # compute number of minibatches for training, validation and testing
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
77 n_train_batches = train_set_x.value.shape[0] / batch_size
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
78
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
79 # allocate symbolic variables for the data
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
80 index = T.lscalar() # index to a [mini]batch
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
81 x = T.matrix('x') # the data is presented as rasterized images
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
82
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
83 rng = numpy.random.RandomState(123)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
84 theano_rng = RandomStreams( rng.randint(2**30))
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
85
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
86 # initialize storage fot the persistent chain (state = hidden layer of chain)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
87 persistent_chain = theano.shared(numpy.zeros((batch_size, 500)))
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
88
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
89 # construct the RBM class
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
90 self.rbm = RBM( input = x, n_visible=28*28, \
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
91 n_hidden = 500,numpy_rng = rng, theano_rng = theano_rng)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
92
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
93 # get the cost and the gradient corresponding to one step of CD
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
94
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
95
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
96 self.init_series()
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
97
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
98 def init_series(self):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
99
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
100 series = {}
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
101
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
102 basedir = os.getcwd()
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
103
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
104 h5f = tables.openFile(os.path.join(basedir, "series.h5"), "w")
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
105
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
106 cd_series_names = self.rbm.cd_return_desc
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
107 series['cd'] = \
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
108 get_accumulator_series_array( \
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
109 h5f, 'cd', cd_series_names,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
110 REDUCE_EVERY,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
111 stdout_too=True)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
112
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
113
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
114
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
115 # so first we create the names for each table, based on
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
116 # position of each param in the array
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
117 params_stdout = StdoutAppendTarget("\n------\nParams")
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
118 series['params'] = SharedParamsStatisticsWrapper(
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
119 new_group_name="params",
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
120 base_group="/",
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
121 arrays_names=['W','b_h','b_x'],
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
122 hdf5_file=h5f,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
123 index_names=('epoch','minibatch'),
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
124 other_targets=[params_stdout])
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
125
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
126 self.series = series
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
127
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
128 def train(self, persistent, learning_rate):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
129
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
130 training_epochs = 15
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
131
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
132 #get the cost and the gradient corresponding to one step of CD
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
133 if persistant:
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
134 persistent_chain = theano.shared(numpy.zeros((batch_size, self.rbm.n_hidden)))
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
135 cost, updates = self.rbm.cd(lr=learning_rate, persistent=persistent_chain)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
136
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
137 else:
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
138 cost, updates = self.rbm.cd(lr=learning_rate)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
139
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
140 dirname = 'lr=%.5f'%self.rbm.learning_rate
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
141 os.makedirs(dirname)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
142 os.chdir(dirname)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
143
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
144 # the purpose of train_rbm is solely to update the RBM parameters
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
145 train_rbm = theano.function([index], cost,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
146 updates = updates,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
147 givens = { x: train_set_x[index*batch_size:(index+1)*batch_size]})
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
148
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
149 plotting_time = 0.
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
150 start_time = time.clock()
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
151
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
152
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
153 # go through training epochs
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
154 for epoch in xrange(training_epochs):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
155
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
156 # go through the training set
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
157 mean_cost = []
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
158 for batch_index in xrange(n_train_batches):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
159 mean_cost += [train_rbm(batch_index)]
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
160
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
161
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
162 pretraining_time = (end_time - start_time)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
163
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
164
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
165
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
166
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
167 def sample_from_rbm(self, gibbs_steps, test_set_x):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
168
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
169 # find out the number of test samples
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
170 number_of_test_samples = test_set_x.value.shape[0]
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
171
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
172 # pick random test examples, with which to initialize the persistent chain
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
173 test_idx = rng.randint(number_of_test_samples-20)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
174 persistent_vis_chain = theano.shared(test_set_x.value[test_idx:test_idx+20])
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
175
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
176 # define one step of Gibbs sampling (mf = mean-field)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
177 [hid_mf, hid_sample, vis_mf, vis_sample] = self.rbm.gibbs_vhv(persistent_vis_chain)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
178
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
179 # the sample at the end of the channel is returned by ``gibbs_1`` as
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
180 # its second output; note that this is computed as a binomial draw,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
181 # therefore it is formed of ints (0 and 1) and therefore needs to
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
182 # be converted to the same dtype as ``persistent_vis_chain``
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
183 vis_sample = T.cast(vis_sample, dtype=theano.config.floatX)
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
184
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
185 # construct the function that implements our persistent chain
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
186 # we generate the "mean field" activations for plotting and the actual samples for
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
187 # reinitializing the state of our persistent chain
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
188 sample_fn = theano.function([], [vis_mf, vis_sample],
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
189 updates = { persistent_vis_chain:vis_sample})
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
190
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
191 # sample the RBM, plotting every `plot_every`-th sample; do this
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
192 # until you plot at least `n_samples`
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
193 n_samples = 10
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
194 plot_every = 1000
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
195
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
196 for idx in xrange(n_samples):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
197
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
198 # do `plot_every` intermediate samplings of which we do not care
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
199 for jdx in xrange(plot_every):
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
200 vis_mf, vis_sample = sample_fn()
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
201
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
202 # construct image
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
203 image = PIL.Image.fromarray(tile_raster_images(
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
204 X = vis_mf,
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
205 img_shape = (28,28),
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
206 tile_shape = (10,10),
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
207 tile_spacing = (1,1) ) )
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
208
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
209 image.save('sample_%i_step_%i.png'%(idx,idx*jdx))
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
210
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
211
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
212 if __name__ == '__main__':
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
213 mc = ExperienceRbm()
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
214 mc.train()
45156cbf6722 training an rbm using cd or pcd
goldfinger
parents:
diff changeset
215