Skip to content

Commit d7ea6c2

Browse files
committed
Merge git://github.com/fidlej/DeepLearningTutorials
2 parents e371d52 + 8469cc9 commit d7ea6c2

4 files changed

Lines changed: 26 additions & 54 deletions

File tree

code/mlp.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,12 @@ def __init__(self, rng, input, n_in, n_out, activation = T.tanh):
6969
# For example, results presented in [Xavier10] suggest that you
7070
# should use 4 times larger initial weights for sigmoid
7171
# compared to tanh
72-
if activation == theano.tensor.tanh:
73-
W_values = numpy.asarray( rng.uniform(
74-
low = - numpy.sqrt(6./(n_in+n_out)),
75-
high = numpy.sqrt(6./(n_in+n_out)),
76-
size = (n_in, n_out)), dtype = theano.config.floatX)
77-
elif activation == theano.tensor.nnet.sigmoid:
78-
W_values = numpy.asarray( 4*rng.uniform(
79-
low = - numpy.sqrt(6./(n_in+n_out)),
80-
high = numpy.sqrt(6./(n_in+n_out)),
81-
size = (n_in, n_out)), dtype = theano.config.floatX)
82-
else:
83-
# how should we initialize the weights for your activation function ?
84-
W_values = numpy.asarray( rng.uniform(
85-
low = - numpy.sqrt(6./(n_in+n_out)),
86-
high = numpy.sqrt(6./(n_in+n_out)),
87-
size = (n_in,n_out)), dtype = theano.config.floatX)
72+
W_values = numpy.asarray( rng.uniform(
73+
low = - numpy.sqrt(6./(n_in+n_out)),
74+
high = numpy.sqrt(6./(n_in+n_out)),
75+
size = (n_in, n_out)), dtype = theano.config.floatX)
76+
if activation == theano.tensor.nnet.sigmoid:
77+
W_values *= 4
8878

8979
self.W = theano.shared(value = W_values, name ='W')
9080

code/rbm.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ def __init__(self, input=None, n_visible=784, n_hidden=500, \
5050
self.n_hidden = n_hidden
5151

5252

53+
if numpy_rng is None:
54+
# create a number generator
55+
numpy_rng = numpy.random.RandomState(1234)
56+
57+
if theano_rng is None :
58+
theano_rng = RandomStreams(numpy_rng.randint(2**30))
59+
5360
if W is None :
5461
# W is initialized with `initial_W` which is uniformely sampled
5562
# from -4*sqrt(6./(n_visible+n_hidden)) and 4*sqrt(6./(n_hidden+n_visible))
@@ -73,13 +80,6 @@ def __init__(self, input=None, n_visible=784, n_hidden=500, \
7380
vbias = theano.shared(value =numpy.zeros(n_visible,
7481
dtype = theano.config.floatX),name='vbias')
7582

76-
if numpy_rng is None:
77-
# create a number generator
78-
numpy_rng = numpy.random.RandomState(1234)
79-
80-
if theano_rng is None :
81-
theano_rng = RandomStreams(numpy_rng.randint(2**30))
82-
8383

8484
# initialize input layer for standalone RBM or layer0 of DBN
8585
self.input = input
@@ -93,10 +93,6 @@ def __init__(self, input=None, n_visible=784, n_hidden=500, \
9393
# **** WARNING: It is not a good idea to put things in this list
9494
# other than shared variables created in this function.
9595
self.params = [self.W, self.hbias, self.vbias]
96-
# cast batch_size to floatX, because its type is int64,
97-
# and otherwise the gradients are upcasted to float64,
98-
# even when floatX == float32
99-
self.batch_size = T.cast(self.input.shape[0], dtype = theano.config.floatX)
10096

10197

10298
def free_energy(self, v_sample):

doc/mlp.txt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -161,22 +161,12 @@ both upward (activations flowing from inputs to outputs) and backward
161161
# For example, results presented in [Xavier10]_ suggest that you
162162
# should use 4 times larger initial weights for sigmoid
163163
# compared to tanh
164-
if activation == theano.tensor.tanh:
165-
W_values = numpy.asarray( rng.uniform(
166-
low = - numpy.sqrt(6./(n_in+n_out)),
167-
high = numpy.sqrt(6./(n_in+n_out)),
168-
size = (n_in, n_out)), dtype = theano.config.floatX)
169-
elif activation == theano.tensor.nnet.sigmoid:
170-
W_values = numpy.asarray( 4*rng.uniform(
171-
low = - numpy.sqrt(6./(n_in+n_out)),
172-
high = numpy.sqrt(6./(n_in+n_out)),
173-
size = (n_in, n_out)), dtype = theano.config.floatX)
174-
else:
175-
# how should we initialize the weights for your activation function ?
176-
W_values = numpy.asarray( rng.uniform(
177-
low = - numpy.sqrt(6./(n_in+n_out)),
178-
high = numpy.sqrt(6./(n_in+n_out)),
179-
size = (n_in,n_out)), dtype = theano.config.floatX)
164+
W_values = numpy.asarray( rng.uniform(
165+
low = - numpy.sqrt(6./(n_in+n_out)),
166+
high = numpy.sqrt(6./(n_in+n_out)),
167+
size = (n_in, n_out)), dtype = theano.config.floatX)
168+
if activation == theano.tensor.nnet.sigmoid:
169+
W_values *= 4
180170

181171
self.W = theano.shared(value = W_values, name ='W')
182172

doc/rbm.txt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ corresponding sigmoidal layer of an MLP network.
344344
self.n_hidden = n_hidden
345345

346346

347+
if numpy_rng is None:
348+
# create a number generator
349+
numpy_rng = numpy.random.RandomState(1234)
350+
351+
if theano_rng is None :
352+
theano_rng = RandomStreams(numpy_rng.randint(2**30))
353+
347354
if W is None :
348355
# W is initialized with `initial_W` which is uniformely sampled
349356
# from -4.*sqrt(6./(n_visible+n_hidden)) and 4.*sqrt(6./(n_hidden+n_visible))
@@ -367,13 +374,6 @@ corresponding sigmoidal layer of an MLP network.
367374
vbias = theano.shared(value =numpy.zeros(n_visible,
368375
dtype = theano.config.floatX),name='vbias')
369376

370-
if numpy_rng is None:
371-
# create a number generator
372-
numpy_rng = numpy.random.RandomState(1234)
373-
374-
if theano_rng is None :
375-
theano_rng = RandomStreams(numpy_rng.randint(2**30))
376-
377377

378378
# initialize input layer for standalone RBM or layer0 of DBN
379379
self.input = input if input else T.dmatrix('input')
@@ -385,10 +385,6 @@ corresponding sigmoidal layer of an MLP network.
385385
# **** WARNING: It is not a good idea to put things in this list
386386
# other than shared variables created in this function.
387387
self.params = [self.W, self.hbias, self.vbias]
388-
# cast batch_size to floatX, because its type is int64,
389-
# and otherwise the gradients are upcasted to float64,
390-
# even when floatX == float32
391-
self.batch_size = T.cast(self.input.shape[0], dtype = theano.config.floatX)
392388

393389

394390
Next step is to define functions which construct the symbolic graph associated

0 commit comments

Comments
 (0)