Skip to content

Commit 7f7cdea

Browse files
committed
Merge branch 'master' of git@github.com:lisa-lab/DeepLearningTutorials
2 parents 52855a3 + 91e06a8 commit 7f7cdea

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

code/rbm.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,27 +158,27 @@ def oneGibbsStep( vis_km1, hid_km1):
158158

159159
# keep_outputs tells scan that we do not care about intermediate values
160160
# of n_vis and n_hid, and that it should only return the last one
161-
n_vis, n_hid = scan(oneGibbsStep,[],[self.input, p_hid],\
162-
[], n_steps = n_Gibbs_steps, keep_outputs = {0:False, 1:False} )
161+
n_vis_vals, n_hid_vals = scan(oneGibbsStep,[],[self.input, p_hid],\
162+
[], n_steps = n_Gibbs_steps )
163163

164-
g_vbias = T.mean( self.input - n_vis , axis = 0)
165-
g_hbias = T.mean( p_hid - n_hid , axis = 0)
164+
g_vbias = T.mean( self.input - n_vis_vals[-1] , axis = 0)
165+
g_hbias = T.mean( p_hid - n_hid_vals[-1] , axis = 0)
166166

167167
# ***Why are we using mean for the biases but a dot()/size formula for the weights?
168168
# It's a minor point, but we're confusing two kinds of terminology.
169169
# Better would be using mean & covariance (I think we have a cov() op...)
170170
# -or- sum()/batchsize and then dot() / batchsize
171171

172-
g_W = T.dot(p_hid.T, self.input )/ self.batch_size - \
173-
T.dot(n_hid.T, n_vis )/ self.batch_size
172+
g_W = T.dot(p_hid.T , self.input )/ self.batch_size - \
173+
T.dot(n_hid_vals[-1].T, n_vis_vals[-1] )/ self.batch_size
174174

175175
gparams = [g_W.T, g_vbias, g_hbias]
176176
# define dictionary of stochastic gradient update equations
177177

178178
# *** It is misleading to say that it returns a cost, since usually a cost is the thing
179179
# we are minimizing.
180180

181-
cost = T.mean(abs(self.input - n_vis))
181+
cost = T.mean(abs(self.input - n_vis_vals[-1]))
182182
return (gparams, cost)
183183

184184

@@ -345,9 +345,23 @@ def gibbs_1(self, v_sample):
345345
return v1_mean, v1_act
346346

347347
def gibbs_k(self, k):
348-
def gibbs_step(v_sample):
349-
raise NotImplementedError('waiting for scan op')
350-
return gibbs_step
348+
def gibbs_steps(v_sample):
349+
v0_sample = v_sample; del v_sample
350+
h0_mean = T.nnet.sigmoid(T.dot(v0_sample, self.W) + self.hbias)
351+
h0_sample = self.theano_rng.binomial(h0_mean.shape, 1, h0_mean)
352+
v1_mean = T.nnet.sigmoid(T.dot(h0_sample, self.W.T) + self.vbias)
353+
v1_act = self.theano_rng.binomial(v1_mean.shape, 1, v1_mean)
354+
355+
def gibbs_step(v_sample_tm1, v_mean_tm1 ):
356+
h_mean_t = T.nnet.sigmoid(T.dot(v_sample_tm1, self.W) + self.hbias)
357+
h_sample_t = self.theano_rng.binomial(h_mean_t.shape, 1, h_mean_t)
358+
v_mean_t = T.nnet.sigmoid(T.dot(h_sample_t, self.W.T) + self.vbias)
359+
v_sample_t = self.theano_rng.binomial(v_mean_t.shape, 1, v_mean_t)
360+
return v_sample_t, v_mean_t
361+
362+
v_samples, v_means = scan(gibbs_step, [], [v1_act, v1_mean],[], \
363+
n_steps = k-1)
364+
return v_means[-1], v_samples[-1]
351365

352366
def free_energy(self, v_sample):
353367
h_mean = T.nnet.sigmoid(T.dot(v_sample, self.W) + self.hbias)
@@ -451,12 +465,12 @@ def shared_dataset(data_xy):
451465
# construct the RBM class
452466
rbm = RBM_option2.new(input = x, n_visible=28*28, n_hidden=500, numpy_rng=
453467
numpy.random.RandomState(234234))
454-
455-
cost = rbm.cd()[0]
468+
step = rbm.gibbs_k(10)
469+
cost = rbm.cd(step = step)[0]
456470

457471
print '... compiling train function'
458-
train_rbm = theano.function([index], rbm.cd()[0],
459-
updates = rbm.cd_updates(learning_rate),
472+
train_rbm = theano.function([index], rbm.cd(step = step)[0],
473+
updates = rbm.cd_updates(learning_rate, step = step),
460474
givens = {
461475
x: train_set_x[index*batch_size:(index+1)*batch_size],
462476
y: train_set_y[index*batch_size:(index+1)*batch_size]}

0 commit comments

Comments
 (0)