Skip to content

Commit 0fea774

Browse files
committed
pep8
1 parent 1192c4c commit 0fea774

1 file changed

Lines changed: 37 additions & 36 deletions

File tree

doc/rbm.txt

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,13 @@ corresponding sigmoidal layer of an MLP network.
378378
# initialize input layer for standalone RBM or layer0 of DBN
379379
self.input = input if input else T.dmatrix('input')
380380

381-
self.W = W
382-
self.hbias = hbias
383-
self.vbias = vbias
381+
self.W = W
382+
self.hbias = hbias
383+
self.vbias = vbias
384384
self.theano_rng = theano_rng
385385
# **** WARNING: It is not a good idea to put things in this list
386386
# other than shared variables created in this function.
387-
self.params = [self.W, self.hbias, self.vbias]
387+
self.params = [self.W, self.hbias, self.vbias]
388388

389389

390390
Next step is to define functions which construct the symbolic graph associated
@@ -412,8 +412,8 @@ with Eqs. :eq:`rbm_propup` - :eq:`rbm_propdown`. The code is as follows:
412412
# Note that theano_rng.binomial returns a symbolic sample of dtype
413413
# int64 by default. If we want to keep our computations in floatX
414414
# for the GPU we need to specify to return the dtype floatX
415-
h1_sample = self.theano_rng.binomial(size = h1_mean.shape, n = 1, p = h1_mean,
416-
dtype = theano.config.floatX)
415+
h1_sample = self.theano_rng.binomial(size=h1_mean.shape, n=1, p=h1_mean,
416+
dtype=theano.config.floatX)
417417
return [pre_sigmoid_h1, h1_mean, h1_sample]
418418

419419
def propdown(self, hid):
@@ -426,7 +426,7 @@ with Eqs. :eq:`rbm_propup` - :eq:`rbm_propdown`. The code is as follows:
426426
stable graph (see details in the reconstruction cost function)
427427
'''
428428
pre_sigmoid_activation = T.dot(hid, self.W.T) + self.vbias
429-
return [pre_sigmoid_activation,T.nnet.sigmoid(pre_sigmoid_activation)]
429+
return [pre_sigmoid_activation, T.nnet.sigmoid(pre_sigmoid_activation)]
430430

431431
def sample_v_given_h(self, h0_sample):
432432
''' This function infers state of visible units given hidden units '''
@@ -436,8 +436,8 @@ with Eqs. :eq:`rbm_propup` - :eq:`rbm_propdown`. The code is as follows:
436436
# Note that theano_rng.binomial returns a symbolic sample of dtype
437437
# int64 by default. If we want to keep our computations in floatX
438438
# for the GPU we need to specify to return the dtype floatX
439-
v1_sample = self.theano_rng.binomial(size = v1_mean.shape,n = 1,p = v1_mean,
440-
dtype = theano.config.floatX)
439+
v1_sample = self.theano_rng.binomial(size=v1_mean.shape,n=1, p=v1_mean,
440+
dtype=theano.config.floatX)
441441
return [pre_sigmoid_v1, v1_mean, v1_sample]
442442

443443
We can then use these functions to define the symbolic graph for a Gibbs
@@ -502,7 +502,7 @@ needed for computing the gradient of the parameters
502502
''' Function to compute the free energy '''
503503
wx_b = T.dot(v_sample, self.W) + self.hbias
504504
vbias_term = T.dot(v_sample, self.vbias)
505-
hidden_term = T.sum(T.log(1+T.exp(wx_b)),axis = 1)
505+
hidden_term = T.sum(T.log(1 + T.exp(wx_b)), axis=1)
506506
return -hidden_term - vbias_term
507507

508508

@@ -511,7 +511,7 @@ gradients for CD-k and PCD-k updates.
511511

512512
.. code-block:: python
513513

514-
def get_cost_updates(self, lr = 0.1, persistent=None, k =1):
514+
def get_cost_updates(self, lr=0.1, persistent=None, k=1):
515515
"""
516516
This functions implements one step of CD-k or PCD-k
517517

@@ -564,8 +564,8 @@ op provided by Theano, therefore we urge the reader to look it up by following t
564564
# the None are place holders, saying that
565565
# chain_start is the initial state corresponding to the
566566
# 6th output
567-
outputs_info = [None, None, None,None,None,chain_start],
568-
n_steps = k)
567+
outputs_info=[None, None, None, None, None, chain_start],
568+
n_steps=k)
569569

570570

571571
Once we have the generated the chain we take the sample at the end of the
@@ -586,7 +586,7 @@ want (it will mess up our gradients) and therefire we need to indicate to
586586

587587
cost = T.mean(self.free_energy(self.input)) - T.mean(self.free_energy(chain_end))
588588
# We must not compute the gradient through the gibbs sampling
589-
gparams = T.grad(cost, self.params,consider_constant = [chain_end])
589+
gparams = T.grad(cost, self.params, consider_constant=[chain_end])
590590

591591
Finally, we add to the updates dictionary returned by scan (which contains
592592
updates rules for random states of ``theano_rng``) to contain the parameter
@@ -598,7 +598,7 @@ containing the state of the Gibbs chain.
598598
# constructs the update dictionary
599599
for gparam, param in zip(gparams, self.params):
600600
# make sure that the learning rate is of the right dtype
601-
updates[param] = param - gparam * T.cast(lr, dtype = theano.config.floatX)
601+
updates[param] = param - gparam * T.cast(lr, dtype=theano.config.floatX)
602602
if persistent:
603603
# Note that this works only if persistent is a shared variable
604604
updates[persistent] = nh_samples[-1]
@@ -687,7 +687,7 @@ compute the pseudo-likelihood:
687687
"""Stochastic approximation to the pseudo-likelihood"""
688688

689689
# index of bit i in expression p(x_i | x_{\i})
690-
bit_i_idx = theano.shared(value=0, name = 'bit_i_idx')
690+
bit_i_idx = theano.shared(value=0, name='bit_i_idx')
691691

692692
# binarize the input image by rounding to nearest integer
693693
xi = T.iround(self.input)
@@ -698,7 +698,7 @@ compute the pseudo-likelihood:
698698
# flip bit x_i of matrix xi and preserve all other bits x_{\i}
699699
# Equivalent to xi[:,bit_i_idx] = 1-xi[:, bit_i_idx], but assigns
700700
# the result to xi_flip, instead of working in place on xi.
701-
xi_flip = T.set_subtensor(xi[:,bit_i_idx], 1-xi[:,bit_i_idx])
701+
xi_flip = T.set_subtensor(xi[:, bit_i_idx], 1 - xi[:, bit_i_idx])
702702

703703
# calculate free energy with bit flipped
704704
fe_xi_flip = self.free_energy(xi_flip)
@@ -734,8 +734,8 @@ been shown to lead to a better generative model ([Tieleman08]_).
734734
# it is ok for a theano function to have no output
735735
# the purpose of train_rbm is solely to update the RBM parameters
736736
train_rbm = theano.function([index], cost,
737-
updates = updates,
738-
givens = { x: train_set_x[index*batch_size:(index+1)*batch_size]})
737+
updates=updates,
738+
givens={ x: train_set_x[index * batch_size:(index + 1) * batch_size]})
739739

740740
plotting_time = 0.
741741
start_time = time.clock()
@@ -755,9 +755,9 @@ been shown to lead to a better generative model ([Tieleman08]_).
755755
plotting_start = time.clock()
756756
# Construct image from the weight matrix
757757
image = PIL.Image.fromarray(tile_raster_images(
758-
X = rbm.W.get_value(borrow=True).T,
759-
img_shape = (28,28),tile_shape = (10,10),
760-
tile_spacing=(1,1)))
758+
X=rbm.W.get_value(borrow=True).T,
759+
img_shape=(28, 28), tile_shape=(10, 10),
760+
tile_spacing=(1, 1)))
761761
image.save('filters_at_epoch_%i.png'%epoch)
762762
plotting_stop = time.clock()
763763
plotting_time += (plotting_stop - plotting_start)
@@ -766,7 +766,7 @@ been shown to lead to a better generative model ([Tieleman08]_).
766766

767767
pretraining_time = (end_time - start_time) - plotting_time
768768

769-
print ('Training took %f minutes' %(pretraining_time/60.))
769+
print ('Training took %f minutes' % (pretraining_time / 60.))
770770

771771
Once the RBM is trained, we can then use the ``gibbs_vhv`` function to implement
772772
the Gibbs chain required for sampling. We initialize the Gibbs chain starting
@@ -785,9 +785,9 @@ each plotting.
785785
number_of_test_samples = test_set_x.get_value(borrow=True).shape[0]
786786

787787
# pick random test examples, with which to initialize the persistent chain
788-
test_idx = rng.randint(number_of_test_samples-20)
788+
test_idx = rng.randint(number_of_test_samples - 20)
789789
persistent_vis_chain = theano.shared(numpy.asarray(
790-
test_set_x.get_value(borrow=True)[test_idx:test_idx+20],
790+
test_set_x.get_value(borrow=True)[test_idx: test_idx + 20],
791791
dtype=theano.config.floatX))
792792

793793
Next we create the 20 persistent chains in parallel to get our
@@ -805,39 +805,40 @@ samples at every 1000 steps.
805805
# pick random test examples, with which to initialize the persistent chain
806806
test_idx = rng.randint(number_of_test_samples-n_chains)
807807
persistent_vis_chain = theano.shared(numpy.array(
808-
test_set_x.get_value(borrow=True)[test_idx:test_idx+100],
808+
test_set_x.get_value(borrow=True)[test_idx:test_idx + 100],
809809
dtype=theano.config.floatX))
810810

811811
plot_every = 1000
812812
# define one step of Gibbs sampling (mf = mean-field)
813813
# define a function that does `plot_every` steps before returning the sample for plotting
814814
[presig_hids, hid_mfs, hid_samples, presig_vis, vis_mfs, vis_samples], updates = \
815815
theano.scan(rbm.gibbs_vhv,
816-
outputs_info = [None, None,None,None,None,persistent_vis_chain],
817-
n_steps = plot_every)
816+
outputs_info=[None, None, None, None, None, persistent_vis_chain],
817+
n_steps=plot_every)
818818

819819
# add to updates the shared variable that takes care of our persistent
820820
# chain :
821-
updates.update({ persistent_vis_chain: vis_samples[-1]})
821+
updates.update({persistent_vis_chain: vis_samples[-1]})
822822
# construct the function that implements our persistent chain
823823
# we generate the "mean field" activations for plotting and the actual samples for
824824
# reinitializing the state of our persistent chain
825825
sample_fn = theano.function([], [vis_mfs[-1], vis_samples[-1]],
826-
updates = updates)
826+
updates=updates)
827827

828828
# sample the RBM, plotting at least `n_samples`
829829
n_samples = 10
830830
# create a space to store the image for plotting ( we need to leave
831831
# room for the tile_spacing as well)
832-
image_data = numpy.zeros((29*n_samples+1,29*n_chains-1),dtype='uint8')
832+
image_data = numpy.zeros((29 * n_samples + 1, 29 * n_chains - 1),
833+
dtype='uint8')
833834
for idx in xrange(n_samples):
834835
# generate `plot_every` intermediate samples that we discard, because successive samples in the chain are too correlated
835836
vis_mf, vis_sample = sample_fn()
836-
image_data[29*idx:29*idx+28,:] = tile_raster_images(
837-
X = vis_mf,
838-
img_shape = (28,28),
839-
tile_shape = (1, batch_size),
840-
tile_spacing = (1,1))
837+
image_data[29 * idx: 29 * idx + 28, :] = tile_raster_images(
838+
X=vis_mf,
839+
img_shape=(28, 28),
840+
tile_shape=(1, batch_size),
841+
tile_spacing=(1, 1))
841842
# construct image
842843

843844
image = PIL.Image.fromarray(image_data)

0 commit comments

Comments
 (0)