annotate deep/rbm/rbm.py @ 369:d81284e13d77

modified to run experiments with PNIST
author goldfinger
date Sat, 24 Apr 2010 11:32:26 -0400
parents 9685e9d94cc4
children 1e99dc965b5b
rev   line source
347
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
1 """This tutorial introduces restricted boltzmann machines (RBM) using Theano.
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
2
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
3 Boltzmann Machines (BMs) are a particular form of energy-based model which
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
4 contain hidden variables. Restricted Boltzmann Machines further restrict BMs
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
5 to those without visible-visible and hidden-hidden connections.
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
6 """
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
7
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
8 import numpy, time, cPickle, gzip, PIL.Image
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
9
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
10 import theano
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
11 import theano.tensor as T
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
12 import os
369
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
13 import pdb
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
14 import numpy
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
15 import pylab
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
16 import time
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
17 import theano.tensor.nnet
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
18 import pylearn
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
19 import ift6266
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
20 import theano,pylearn.version,ift6266
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
21 from pylearn.io import filetensor as ft
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
22 from ift6266 import datasets
347
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
23
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
24 from theano.tensor.shared_randomstreams import RandomStreams
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
25
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
26 from utils import tile_raster_images
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
27 from logistic_sgd import load_data
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
28
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
29 class RBM(object):
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
30 """Restricted Boltzmann Machine (RBM) """
369
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
31 def __init__(self, input=None, n_visible=32*32, n_hidden=500, \
347
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
32 W = None, hbias = None, vbias = None, numpy_rng = None,
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
33 theano_rng = None):
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
34 """
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
35 RBM constructor. Defines the parameters of the model along with
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
36 basic operations for inferring hidden from visible (and vice-versa),
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
37 as well as for performing CD updates.
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
38
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
39 :param input: None for standalone RBMs or symbolic variable if RBM is
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
40 part of a larger graph.
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
41
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
42 :param n_visible: number of visible units
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
43
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
44 :param n_hidden: number of hidden units
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
45
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
46 :param W: None for standalone RBMs or symbolic variable pointing to a
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
47 shared weight matrix in case RBM is part of a DBN network; in a DBN,
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
48 the weights are shared between RBMs and layers of a MLP
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
49
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
50 :param hbias: None for standalone RBMs or symbolic variable pointing
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
51 to a shared hidden units bias vector in case RBM is part of a
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
52 different network
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
53
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
54 :param vbias: None for standalone RBMs or a symbolic variable
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
55 pointing to a shared visible units bias
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
56 """
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
57
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
58 self.n_visible = n_visible
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
59 self.n_hidden = n_hidden
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
60
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
61
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
62 if W is None :
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
63 # W is initialized with `initial_W` which is uniformely sampled
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
64 # from -6./sqrt(n_visible+n_hidden) and 6./sqrt(n_hidden+n_visible)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
65 # the output of uniform if converted using asarray to dtype
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
66 # theano.config.floatX so that the code is runable on GPU
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
67 initial_W = numpy.asarray( numpy.random.uniform(
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
68 low = -numpy.sqrt(6./(n_hidden+n_visible)),
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
69 high = numpy.sqrt(6./(n_hidden+n_visible)),
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
70 size = (n_visible, n_hidden)),
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
71 dtype = theano.config.floatX)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
72 # theano shared variables for weights and biases
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
73 W = theano.shared(value = initial_W, name = 'W')
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
74
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
75 if hbias is None :
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
76 # create shared variable for hidden units bias
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
77 hbias = theano.shared(value = numpy.zeros(n_hidden,
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
78 dtype = theano.config.floatX), name='hbias')
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
79
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
80 if vbias is None :
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
81 # create shared variable for visible units bias
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
82 vbias = theano.shared(value =numpy.zeros(n_visible,
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
83 dtype = theano.config.floatX),name='vbias')
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
84
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
85 if numpy_rng is None:
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
86 # create a number generator
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
87 numpy_rng = numpy.random.RandomState(1234)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
88
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
89 if theano_rng is None :
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
90 theano_rng = RandomStreams(numpy_rng.randint(2**30))
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
91
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
92
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
93 # initialize input layer for standalone RBM or layer0 of DBN
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
94 self.input = input if input else T.dmatrix('input')
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
95
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
96 self.W = W
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
97 self.hbias = hbias
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
98 self.vbias = vbias
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
99 self.theano_rng = theano_rng
369
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
100
347
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
101 # **** WARNING: It is not a good idea to put things in this list
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
102 # other than shared variables created in this function.
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
103 self.params = [self.W, self.hbias, self.vbias]
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
104 self.batch_size = self.input.shape[0]
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
105
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
106 def free_energy(self, v_sample):
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
107 ''' Function to compute the free energy '''
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
108 wx_b = T.dot(v_sample, self.W) + self.hbias
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
109 vbias_term = T.sum(T.dot(v_sample, self.vbias))
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
110 hidden_term = T.sum(T.log(1+T.exp(wx_b)))
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
111 return -hidden_term - vbias_term
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
112
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
113 def sample_h_given_v(self, v0_sample):
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
114 ''' This function infers state of hidden units given visible units '''
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
115 # compute the activation of the hidden units given a sample of the visibles
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
116 h1_mean = T.nnet.sigmoid(T.dot(v0_sample, self.W) + self.hbias)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
117 # get a sample of the hiddens given their activation
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
118 h1_sample = self.theano_rng.binomial(size = h1_mean.shape, n = 1, prob = h1_mean)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
119 return [h1_mean, h1_sample]
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
120
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
121 def sample_v_given_h(self, h0_sample):
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
122 ''' This function infers state of visible units given hidden units '''
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
123 # compute the activation of the visible given the hidden sample
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
124 v1_mean = T.nnet.sigmoid(T.dot(h0_sample, self.W.T) + self.vbias)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
125 # get a sample of the visible given their activation
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
126 v1_sample = self.theano_rng.binomial(size = v1_mean.shape,n = 1,prob = v1_mean)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
127 return [v1_mean, v1_sample]
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
128
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
129 def gibbs_hvh(self, h0_sample):
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
130 ''' This function implements one step of Gibbs sampling,
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
131 starting from the hidden state'''
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
132 v1_mean, v1_sample = self.sample_v_given_h(h0_sample)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
133 h1_mean, h1_sample = self.sample_h_given_v(v1_sample)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
134 return [v1_mean, v1_sample, h1_mean, h1_sample]
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
135
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
136 def gibbs_vhv(self, v0_sample):
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
137 ''' This function implements one step of Gibbs sampling,
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
138 starting from the visible state'''
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
139 h1_mean, h1_sample = self.sample_h_given_v(v0_sample)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
140 v1_mean, v1_sample = self.sample_v_given_h(h1_sample)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
141 return [h1_mean, h1_sample, v1_mean, v1_sample]
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
142
369
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
143 def cd(self, lr = 0.1, persistent=None, k=1):
347
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
144 """
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
145 This functions implements one step of CD-1 or PCD-1
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
146
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
147 :param lr: learning rate used to train the RBM
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
148 :param persistent: None for CD. For PCD, shared variable containing old state
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
149 of Gibbs chain. This must be a shared variable of size (batch size, number of
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
150 hidden units).
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
151
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
152 Returns the updates dictionary. The dictionary contains the update rules for weights
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
153 and biases but also an update of the shared variable used to store the persistent
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
154 chain, if one is used.
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
155 """
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
156
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
157 # compute positive phase
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
158 ph_mean, ph_sample = self.sample_h_given_v(self.input)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
159
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
160 # decide how to initialize persistent chain:
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
161 # for CD, we use the newly generate hidden sample
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
162 # for PCD, we initialize from the old state of the chain
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
163 if persistent is None:
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
164 chain_start = ph_sample
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
165 else:
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
166 chain_start = persistent
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
167
369
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
168 # perform actual negative phase (the CD-1)
347
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
169 [nv_mean, nv_sample, nh_mean, nh_sample] = self.gibbs_hvh(chain_start)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
170
369
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
171 #perform CD-k
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
172 if k-1>0:
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
173 for i in range(k-1):
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
174 [nv_mean, nv_sample, nh_mean, nh_sample] = self.gibbs_hvh(nh_sample)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
175
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
176
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
177
347
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
178 # determine gradients on RBM parameters
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
179 g_vbias = T.sum( self.input - nv_mean, axis = 0)/self.batch_size
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
180 g_hbias = T.sum( ph_mean - nh_mean, axis = 0)/self.batch_size
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
181 g_W = T.dot(ph_mean.T, self.input )/ self.batch_size - \
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
182 T.dot(nh_mean.T, nv_mean )/ self.batch_size
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
183
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
184 gparams = [g_W.T, g_hbias, g_vbias]
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
185
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
186 # constructs the update dictionary
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
187 updates = {}
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
188 for gparam, param in zip(gparams, self.params):
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
189 updates[param] = param + gparam * lr
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
190
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
191 if persistent:
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
192 # Note that this works only if persistent is a shared variable
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
193 updates[persistent] = T.cast(nh_sample, dtype=theano.config.floatX)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
194 # pseudo-likelihood is a better proxy for PCD
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
195 cost = self.get_pseudo_likelihood_cost(updates)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
196 else:
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
197 # reconstruction cross-entropy is a better proxy for CD
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
198 cost = self.get_reconstruction_cost(updates, nv_mean)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
199
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
200 return cost, updates
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
201
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
202 def get_pseudo_likelihood_cost(self, updates):
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
203 """Stochastic approximation to the pseudo-likelihood"""
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
204
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
205 # index of bit i in expression p(x_i | x_{\i})
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
206 bit_i_idx = theano.shared(value=0, name = 'bit_i_idx')
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
207
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
208 # binarize the input image by rounding to nearest integer
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
209 xi = T.iround(self.input)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
210
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
211 # calculate free energy for the given bit configuration
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
212 fe_xi = self.free_energy(xi)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
213
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
214 # flip bit x_i of matrix xi and preserve all other bits x_{\i}
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
215 # Equivalent to xi[:,bit_i_idx] = 1-xi[:, bit_i_idx]
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
216 # NB: slice(start,stop,step) is the python object used for
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
217 # slicing, e.g. to index matrix x as follows: x[start:stop:step]
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
218 xi_flip = T.setsubtensor(xi, 1-xi[:, bit_i_idx],
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
219 idx_list=(slice(None,None,None),bit_i_idx))
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
220
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
221 # calculate free energy with bit flipped
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
222 fe_xi_flip = self.free_energy(xi_flip)
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
223
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
224 # equivalent to e^(-FE(x_i)) / (e^(-FE(x_i)) + e^(-FE(x_{\i})))
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
225 cost = self.n_visible * T.log(T.nnet.sigmoid(fe_xi_flip - fe_xi))
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
226
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
227 # increment bit_i_idx % number as part of updates
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
228 updates[bit_i_idx] = (bit_i_idx + 1) % self.n_visible
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
229
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
230 return cost
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
231
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
232 def get_reconstruction_cost(self, updates, nv_mean):
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
233 """Approximation to the reconstruction error"""
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
234
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
235 cross_entropy = T.mean(
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
236 T.sum(self.input*T.log(nv_mean) +
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
237 (1 - self.input)*T.log(1-nv_mean), axis = 1))
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
238
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
239 return cross_entropy
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
240
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
241
9685e9d94cc4 base class for an rbm
goldfinger
parents:
diff changeset
242
369
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
243 def test_rbm(b_size = 25, nhidden = 1000, kk = 1, persistance = 0,
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
244 dataset= 0):
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
245 """
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
246 Demonstrate ***
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
247
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
248 This is demonstrated on MNIST.
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
249
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
250 :param learning_rate: learning rate used for training the RBM
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
251
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
252 :param training_epochs: number of epochs used for training
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
253
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
254 :param dataset: path the the pickled dataset
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
255
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
256 """
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
257
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
258 learning_rate=0.1
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
259
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
260 if data_set==0:
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
261 datasets=datasets.nist_all()
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
262 elif data_set==1:
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
263 datasets=datasets.nist_P07()
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
264 elif data_set==2:
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
265 datasets=datasets.PNIST07()
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
266
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
267
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
268 # revoir la recuperation des donnees
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
269 ## dataset = load_data(dataset)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
270 ##
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
271 ## train_set_x, train_set_y = datasets[0]
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
272 ## test_set_x , test_set_y = datasets[2]
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
273 ## training_epochs = 10 # a determiner
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
274
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
275 batch_size = b_size # size of the minibatch
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
276
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
277 # compute number of minibatches for training, validation and testing
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
278 #n_train_batches = train_set_x.value.shape[0] / batch_size
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
279
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
280 # allocate symbolic variables for the data
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
281 index = T.lscalar() # index to a [mini]batch
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
282 x = T.matrix('x') # the data is presented as rasterized images
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
283
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
284 rng = numpy.random.RandomState(123)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
285 theano_rng = RandomStreams( rng.randint(2**30))
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
286
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
287
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
288 # construct the RBM class
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
289 rbm = RBM( input = x, n_visible=32*32, \
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
290 n_hidden = nhidden, numpy_rng = rng, theano_rng = theano_rng)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
291
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
292
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
293 # initialize storage fot the persistent chain (state = hidden layer of chain)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
294 if persistance == 1:
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
295 persistent_chain = theano.shared(numpy.zeros((batch_size, 500)))
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
296 # get the cost and the gradient corresponding to one step of CD
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
297 cost, updates = rbm.cd(lr=learning_rate, persistent=persistent_chain, k= kk)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
298
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
299 else:
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
300 # get the cost and the gradient corresponding to one step of CD
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
301 #persistance_chain = None
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
302 cost, updates = rbm.cd(lr=learning_rate, persistent=None, k= kk)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
303
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
304 #################################
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
305 # Training the RBM #
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
306 #################################
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
307 dirname = 'data=%i'%dataset + ' persistance=%i'%persistance + ' n_hidden=%i'%n_hidden + 'batch_size=i%'%b_size
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
308 os.makedirs(dirname)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
309 os.chdir(dirname)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
310
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
311 # it is ok for a theano function to have no output
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
312 # the purpose of train_rbm is solely to update the RBM parameters
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
313 train_rbm = theano.function([x], cost,
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
314 updates = updates,
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
315 )
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
316
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
317 plotting_time = 0.0
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
318 start_time = time.clock()
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
319 bufsize = 1000
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
320
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
321 # go through training epochs
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
322 costs = []
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
323 for epoch in xrange(training_epochs):
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
324
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
325 # go through the training set
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
326 mean_cost = []
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
327 for mini_x, mini_y in datasets.train(b_size):
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
328 mean_cost += [train_rbm(mini_x)]
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
329 ## learning_rate = learning_rate - 0.0001
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
330 ## learning_rate = learning_rate/(tau+( epoch*batch_index*batch_size))
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
331
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
332 #learning_rate = learning_rate/10
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
333
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
334 costs.append(numpy.mean(mean_cost))
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
335
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
336 # Plot filters after each training epoch
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
337 plotting_start = time.clock()
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
338 # Construct image from the weight matrix
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
339 image = PIL.Image.fromarray(tile_raster_images( X = rbm.W.value.T,
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
340 img_shape = (32,32),tile_shape = (10,10),
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
341 tile_spacing=(1,1)))
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
342 image.save('filters_at_epoch_%i.png'%epoch)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
343 plotting_stop = time.clock()
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
344 plotting_time += (plotting_stop - plotting_start)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
345
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
346 end_time = time.clock()
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
347
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
348 pretraining_time = (end_time - start_time) - plotting_time
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
349
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
350
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
351
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
352
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
353
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
354
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
355 #################################
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
356 # Sampling from the RBM #
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
357 #################################
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
358
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
359 # find out the number of test samples
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
360 number_of_test_samples = 1000
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
361
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
362 test_set_x, test_y = datasets.test(100*b_size)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
363 # pick random test examples, with which to initialize the persistent chain
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
364 test_idx = rng.randint(number_of_test_samples - b_size)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
365 persistent_vis_chain = theano.shared(test_set_x.value[test_idx:test_idx+b_size])
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
366
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
367 # define one step of Gibbs sampling (mf = mean-field)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
368 [hid_mf, hid_sample, vis_mf, vis_sample] = rbm.gibbs_vhv(persistent_vis_chain)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
369
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
370 # the sample at the end of the channel is returned by ``gibbs_1`` as
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
371 # its second output; note that this is computed as a binomial draw,
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
372 # therefore it is formed of ints (0 and 1) and therefore needs to
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
373 # be converted to the same dtype as ``persistent_vis_chain``
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
374 vis_sample = T.cast(vis_sample, dtype=theano.config.floatX)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
375
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
376 # construct the function that implements our persistent chain
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
377 # we generate the "mean field" activations for plotting and the actual samples for
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
378 # reinitializing the state of our persistent chain
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
379 sample_fn = theano.function([], [vis_mf, vis_sample],
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
380 updates = { persistent_vis_chain:vis_sample})
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
381
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
382 # sample the RBM, plotting every `plot_every`-th sample; do this
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
383 # until you plot at least `n_samples`
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
384 n_samples = 10
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
385 # run minibatch size chains for gibbs samples (number of negative particles)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
386 plot_every = b_size
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
387
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
388 for idx in xrange(n_samples):
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
389
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
390 # do `plot_every` intermediate samplings of which we do not care
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
391 for jdx in xrange(plot_every):
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
392 vis_mf, vis_sample = sample_fn()
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
393
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
394 # construct image
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
395 image = PIL.Image.fromarray(tile_raster_images(
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
396 X = vis_mf,
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
397 img_shape = (32,32),
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
398 tile_shape = (10,10),
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
399 tile_spacing = (1,1) ) )
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
400 #print ' ... plotting sample ', idx
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
401 image.save('sample_%i_step_%i.png'%(idx,idx*jdx))
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
402
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
403 #save the model
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
404 model = [rbm.W, rbm.vbias, rbm.hbias]
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
405 f = fopen('params.txt', 'w')
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
406 pickle.dump(model, f)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
407 f.close()
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
408 #os.chdir('./..')
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
409 return numpy.mean(costs), pretraining_time/360
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
410
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
411
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
412 def experiment(state, channel):
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
413
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
414 (mean_cost, time_execution) = test_rbm(b_size = state.b_size,\
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
415 nhidden = state.ndidden,\
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
416 kk = state.kk,\
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
417 persistance = state.persistance,\
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
418 dataset = state.dataset)
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
419
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
420 state.mean_costs = mean_costs
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
421 state.time_execution = time_execution
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
422 pylearn.version.record_versions(state,[theano,ift6266,pylearn])
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
423 return channel.COMPLETE
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
424
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
425 if __name__ == '__main__':
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
426
d81284e13d77 modified to run experiments with PNIST
goldfinger
parents: 347
diff changeset
427 test_rbm()