Skip to content

Commit 8596e69

Browse files
committed
use borrow=True when making shared variable to make the example work with 32 bits python.
1 parent ff602c9 commit 8596e69

6 files changed

Lines changed: 34 additions & 21 deletions

File tree

code/cA.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,18 @@ def __init__(self, numpy_rng, input=None, n_visible=784, n_hidden=100,
133133
high=4 * numpy.sqrt(6. / (n_hidden + n_visible)),
134134
size=(n_visible, n_hidden)),
135135
dtype=theano.config.floatX)
136-
W = theano.shared(value=initial_W, name='W')
136+
W = theano.shared(value=initial_W, name='W', borrow=True)
137137

138138
if not bvis:
139139
bvis = theano.shared(value=numpy.zeros(n_visible,
140-
dtype=theano.config.floatX))
140+
dtype=theano.config.floatX),
141+
borrow=True)
141142

142143
if not bhid:
143144
bhid = theano.shared(value=numpy.zeros(n_hidden,
144145
dtype=theano.config.floatX),
145-
name='b')
146+
name='b',
147+
borrow=True)
146148

147149
self.W = W
148150
# b corresponds to the bias of the hidden

code/dA.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,18 @@ def __init__(self, numpy_rng, theano_rng=None, input=None,
139139
low=-4 * numpy.sqrt(6. / (n_hidden + n_visible)),
140140
high=4 * numpy.sqrt(6. / (n_hidden + n_visible)),
141141
size=(n_visible, n_hidden)), dtype=theano.config.floatX)
142-
W = theano.shared(value=initial_W, name='W')
142+
W = theano.shared(value=initial_W, name='W', borrow=True)
143143

144144
if not bvis:
145145
bvis = theano.shared(value=numpy.zeros(n_visible,
146-
dtype=theano.config.floatX))
146+
dtype=theano.config.floatX),
147+
borrow=True)
147148

148149
if not bhid:
149150
bhid = theano.shared(value=numpy.zeros(n_hidden,
150-
dtype=theano.config.floatX), name='b')
151+
dtype=theano.config.floatX),
152+
name='b',
153+
borrow=True)
151154

152155
self.W = W
153156
# b corresponds to the bias of the hidden

code/logistic_cg.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def __init__(self, input, n_in, n_out):
8080
# n_in*n_out + n_out elements
8181
self.theta = theano.shared(value=numpy.zeros(n_in * n_out + n_out,
8282
dtype=theano.config.floatX),
83-
name='theta')
83+
name='theta',
84+
borrow=True)
8485
# W is represented by the fisr n_in*n_out elements of theta
8586
self.W = self.theta[0:n_in * n_out].reshape((n_in, n_out))
8687
# b is the rest (last n_out elements)
@@ -154,7 +155,7 @@ def cg_optimization_mnist(n_epochs=50, mnist_pkl_gz='../data/mnist.pkl.gz'):
154155
train_set, valid_set, test_set = cPickle.load(f)
155156
f.close()
156157

157-
def shared_dataset(data_xy):
158+
def shared_dataset(data_xy, borrow=True):
158159
""" Function that loads the dataset into shared variables
159160
160161
The reason we store our dataset in shared variables is to allow
@@ -165,9 +166,11 @@ def shared_dataset(data_xy):
165166
"""
166167
data_x, data_y = data_xy
167168
shared_x = theano.shared(numpy.asarray(data_x,
168-
dtype=theano.config.floatX))
169+
dtype=theano.config.floatX),
170+
borrow=borrow)
169171
shared_y = theano.shared(numpy.asarray(data_y,
170-
dtype=theano.config.floatX))
172+
dtype=theano.config.floatX),
173+
borrow=borrow)
171174
# When storing data on the GPU it has to be stored as floats
172175
# therefore we will store the labels as ``floatX`` as well
173176
# (``shared_y`` does exactly that). But during our computations

code/logistic_sgd.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ def __init__(self, input, n_in, n_out):
7676
# initialize with 0 the weights W as a matrix of shape (n_in, n_out)
7777
self.W = theano.shared(value=numpy.zeros((n_in, n_out),
7878
dtype=theano.config.floatX),
79-
name='W')
79+
name='W', borrow=True)
8080
# initialize the baises b as a vector of n_out 0s
8181
self.b = theano.shared(value=numpy.zeros((n_out,),
8282
dtype=theano.config.floatX),
83-
name='b')
83+
name='b', borrow=True)
8484

8585
# compute vector of class-membership probabilities in symbolic form
8686
self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)
@@ -176,7 +176,7 @@ def load_data(dataset):
176176
#the number of rows in the input. It should give the target
177177
#target to the example with the same index in the input.
178178

179-
def shared_dataset(data_xy):
179+
def shared_dataset(data_xy, borrow=True):
180180
""" Function that loads the dataset into shared variables
181181
182182
The reason we store our dataset in shared variables is to allow
@@ -187,9 +187,11 @@ def shared_dataset(data_xy):
187187
"""
188188
data_x, data_y = data_xy
189189
shared_x = theano.shared(numpy.asarray(data_x,
190-
dtype=theano.config.floatX))
190+
dtype=theano.config.floatX),
191+
borrow=borrow)
191192
shared_y = theano.shared(numpy.asarray(data_y,
192-
dtype=theano.config.floatX))
193+
dtype=theano.config.floatX),
194+
borrow=borrow)
193195
# When storing data on the GPU it has to be stored as floats
194196
# therefore we will store the labels as ``floatX`` as well
195197
# (``shared_y`` does exactly that). But during our computations

code/mlp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ def __init__(self, rng, input, n_in, n_out, W=None, b=None,
8686
if activation == theano.tensor.nnet.sigmoid:
8787
W_values *= 4
8888

89-
W = theano.shared(value=W_values, name='W')
89+
W = theano.shared(value=W_values, name='W', borrow=True)
9090

9191
if b is None:
9292
b_values = numpy.zeros((n_out,), dtype=theano.config.floatX)
93-
b = theano.shared(value=b_values, name='b')
93+
b = theano.shared(value=b_values, name='b', borrow=True)
9494

9595
self.W = W
9696
self.b = b

code/rbm.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,19 @@ def __init__(self, input=None, n_visible=784, n_hidden=500, \
7272
size=(n_visible, n_hidden)),
7373
dtype=theano.config.floatX)
7474
# theano shared variables for weights and biases
75-
W = theano.shared(value=initial_W, name='W')
75+
W = theano.shared(value=initial_W, name='W', borrow=True)
7676

7777
if hbias is None:
7878
# create shared variable for hidden units bias
7979
hbias = theano.shared(value=numpy.zeros(n_hidden,
80-
dtype=theano.config.floatX), name='hbias')
80+
dtype=theano.config.floatX),
81+
name='hbias', borrow=True)
8182

8283
if vbias is None:
8384
# create shared variable for visible units bias
8485
vbias = theano.shared(value=numpy.zeros(n_visible,
85-
dtype=theano.config.floatX), name='vbias')
86+
dtype=theano.config.floatX),
87+
name='vbias', borrow=True)
8688

8789
# initialize input layer for standalone RBM or layer0 of DBN
8890
self.input = input
@@ -352,7 +354,8 @@ def test_rbm(learning_rate=0.1, training_epochs=15,
352354
# initialize storage for the persistent chain (state = hidden
353355
# layer of chain)
354356
persistent_chain = theano.shared(numpy.zeros((batch_size, n_hidden),
355-
dtype=theano.config.floatX))
357+
dtype=theano.config.floatX),
358+
borrow=True)
356359

357360
# construct the RBM class
358361
rbm = RBM(input=x, n_visible=28 * 28,

0 commit comments

Comments
 (0)