Skip to content

Commit 66e9bff

Browse files
committed
Remove use of .value in mcrbm
1 parent 46fc8c9 commit 66e9bff

7 files changed

Lines changed: 42 additions & 41 deletions

File tree

code/convolutional_mlp.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2,2)):
7878
fan_out = filter_shape[0] * numpy.prod(filter_shape[2:]) / numpy.prod(poolsize)
7979
# replace weight values with random weights
8080
W_bound = numpy.sqrt(6./(fan_in + fan_out))
81-
self.W.value = numpy.asarray(
81+
self.W.set_value(numpy.asarray(
8282
rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
83-
dtype = theano.config.floatX)
84-
83+
dtype = theano.config.floatX),
84+
borrow=True)
85+
8586
# downsample each feature map individually, using maxpooling
8687
pooled_out = downsample.max_pool_2d( input = conv_out,
8788
ds = poolsize, ignore_border=True)
@@ -124,9 +125,9 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, dataset='../data/mnist.pkl.
124125

125126

126127
# compute number of minibatches for training, validation and testing
127-
n_train_batches = train_set_x.value.shape[0] / batch_size
128-
n_valid_batches = valid_set_x.value.shape[0] / batch_size
129-
n_test_batches = test_set_x.value.shape[0] / batch_size
128+
n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
129+
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
130+
n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size
130131

131132
# allocate symbolic variables for the data
132133
index = T.lscalar() # index to a [mini]batch

code/mcrbm/hmc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,4 @@ def draw(self, **kwargs):
392392
Numpy matrix whose of dimensions similar to `initial_position`.
393393
"""
394394
self.simulate()
395-
return self.positions.value.copy()
395+
return self.positions.get_value(borrow=False)

code/mcrbm/mcRBM.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def n_visible_units(self):
372372
373373
"""
374374
try:
375-
return self.W.value.shape[0]
375+
return self.W.get_value(borrow=True).shape[0]
376376
except AttributeError:
377377
return self.W.shape[0]
378378

@@ -384,7 +384,7 @@ def n_hidden_cov_units(self):
384384
385385
"""
386386
try:
387-
return self.U.value.shape[1]
387+
return self.U.get_value(borrow=True).shape[1]
388388
except AttributeError:
389389
return self.U.shape[1]
390390

@@ -396,7 +396,7 @@ def n_hidden_mean_units(self):
396396
397397
"""
398398
try:
399-
return self.W.value.shape[1]
399+
return self.W.get_value(borrow=True).shape[1]
400400
except AttributeError:
401401
return self.W.shape[1]
402402

@@ -545,7 +545,7 @@ def n_hidden_cov_units(self):
545545
546546
"""
547547
try:
548-
return self.P.value.shape[1]
548+
return self.P.get_value(borrow=True).shape[1]
549549
except AttributeError:
550550
return self.P.shape[1]
551551

@@ -630,7 +630,7 @@ def alloc_for_P(cls, rbm, visible_batch, batchsize, initial_lr_per_example=0.075
630630
rval = cls.alloc(rbm, visible_batch, batchsize, initial_lr_per_example, rng, l1_penalty,
631631
l1_penalty_start, learn_rate_multipliers, lr_anneal_start, persistent_chains)
632632

633-
rval.p_mask = sharedX((rbm.P.value!=0).astype('float32'), 'p_mask')
633+
rval.p_mask = sharedX((rbm.P.get_value(borrow=True) !=0 ).astype('float32'), 'p_mask')
634634

635635
rval.p_lr = p_lr
636636
rval.p_training_start=p_training_start

code/mcrbm/mcrbm.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def n_visible_units(self):
372372
373373
"""
374374
try:
375-
return self.W.value.shape[0]
375+
return self.W.get_value(borrow=True).shape[0]
376376
except AttributeError:
377377
return self.W.shape[0]
378378

@@ -384,7 +384,7 @@ def n_hidden_cov_units(self):
384384
385385
"""
386386
try:
387-
return self.U.value.shape[1]
387+
return self.U.get_value(borrow=True).shape[1]
388388
except AttributeError:
389389
return self.U.shape[1]
390390

@@ -396,7 +396,7 @@ def n_hidden_mean_units(self):
396396
397397
"""
398398
try:
399-
return self.W.value.shape[1]
399+
return self.W.get_value(borrow=True).shape[1]
400400
except AttributeError:
401401
return self.W.shape[1]
402402

@@ -545,7 +545,7 @@ def n_hidden_cov_units(self):
545545
546546
"""
547547
try:
548-
return self.P.value.shape[1]
548+
return self.P.get_value(borrow=True).shape[1]
549549
except AttributeError:
550550
return self.P.shape[1]
551551

@@ -630,7 +630,7 @@ def alloc_for_P(cls, rbm, visible_batch, batchsize, initial_lr_per_example=0.075
630630
rval = cls.alloc(rbm, visible_batch, batchsize, initial_lr_per_example, rng, l1_penalty,
631631
l1_penalty_start, learn_rate_multipliers, lr_anneal_start, persistent_chains)
632632

633-
rval.p_mask = sharedX((rbm.P.value!=0).astype('float32'), 'p_mask')
633+
rval.p_mask = sharedX((rbm.P.get_value(borrow=True) != 0).astype('float32'), 'p_mask')
634634

635635
rval.p_lr = p_lr
636636
rval.p_training_start=p_training_start

code/mcrbm/test_hmc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ def gaussian_energy(x):
4141
print 'empirical_cov:\n', np.cov(samples.T)
4242

4343
print '****** HMC INTERNALS ******'
44-
print 'final stepsize', sampler.stepsize.value
45-
print 'final acceptance_rate', sampler.avg_acceptance_rate.value
44+
print 'final stepsize', sampler.stepsize.get_value()
45+
print 'final acceptance_rate', sampler.avg_acceptance_rate.get_value()
4646

4747
return sampler
4848

4949
def test_hmc():
5050
sampler = sampler_on_nd_gaussian(HMC_sampler.new_from_shared_positions,
5151
burnin=1000, n_samples=1000, dim=5)
5252
assert abs(sampler.avg_acceptance_rate - sampler.target_acceptance_rate) < .1
53-
assert sampler.stepsize.value >= sampler.stepsize_min
54-
assert sampler.stepsize.value <= sampler.stepsize_max
53+
assert sampler.stepsize.get_value() >= sampler.stepsize_min
54+
assert sampler.stepsize.get_value() <= sampler.stepsize_max
5555

code/mcrbm/test_mcrbm.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,31 @@ def test_reproduce_ranzato_hinton_2010(dataset='MAR',
8282
if print_jj:
8383
tile(imgs_fn(jj), "imgs_%06i.png"%jj)
8484
if persistent_chains:
85-
tile(smplr.positions.value, "sample_%06i.png"%jj)
86-
tile(rbm.U.value.T, "U_%06i.png"%jj)
87-
tile(rbm.W.value.T, "W_%06i.png"%jj)
85+
tile(smplr.positions.get_value(borrow=True), "sample_%06i.png"%jj)
86+
tile(rbm.U.get_value(borrow=True).T, "U_%06i.png"%jj)
87+
tile(rbm.W.get_value(borrow=True).T, "W_%06i.png"%jj)
8888

8989
print 'saving samples', jj, 'epoch', jj/(epoch_size/batchsize)
9090

91-
print 'l2(U)', l2(rbm.U.value),
92-
print 'l2(W)', l2(rbm.W.value),
91+
print 'l2(U)', l2(rbm.U.get_value(borrow=True)),
92+
print 'l2(W)', l2(rbm.W.get_value(borrow=True)),
9393
print 'l1_penalty',
9494
try:
95-
print trainer.effective_l1_penalty.value
95+
print trainer.effective_l1_penalty.get_value()
9696
except:
9797
print trainer.effective_l1_penalty
9898

99-
print 'U min max', rbm.U.value.min(), rbm.U.value.max(),
100-
print 'W min max', rbm.W.value.min(), rbm.W.value.max(),
101-
print 'a min max', rbm.a.value.min(), rbm.a.value.max(),
102-
print 'b min max', rbm.b.value.min(), rbm.b.value.max(),
103-
print 'c min max', rbm.c.value.min(), rbm.c.value.max()
99+
print 'U min max', rbm.U.get_value(borrow=True).min(), rbm.U.get_value(borrow=True).max(),
100+
print 'W min max', rbm.W.get_value(borrow=True).min(), rbm.W.get_value(borrow=True).max(),
101+
print 'a min max', rbm.a.get_value(borrow=True).min(), rbm.a.get_value(borrow=True).max(),
102+
print 'b min max', rbm.b.get_value(borrow=True).min(), rbm.b.get_value(borrow=True).max(),
103+
print 'c min max', rbm.c.get_value(borrow=True).min(), rbm.c.get_value(borrow=True).max()
104104

105105
if persistent_chains:
106-
print 'parts min', smplr.positions.value.min(),
107-
print 'max',smplr.positions.value.max(),
108-
print 'HMC step', smplr.stepsize.value,
109-
print 'arate', smplr.avg_acceptance_rate.value
106+
print 'parts min', smplr.positions.get_value(borrow=True).min(),
107+
print 'max',smplr.positions.get_value(borrow=True).max(),
108+
print 'HMC step', smplr.stepsize.get_value(),
109+
print 'arate', smplr.avg_acceptance_rate.get_value()
110110

111111

112112
l2_of_Ugrad = learn_fn(jj)

doc/hmc.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ elements are:
583583
Numpy matrix whose of dimensions similar to `initial_position`.
584584
"""
585585
self.simulate()
586-
return self.positions.value.copy()
586+
return self.positions.get_value(borrow=False)
587587

588588

589589
Testing our Sampler
@@ -641,17 +641,17 @@ compare the empirical mean and covariance matrix to their true values.
641641
print 'empirical_cov:\n', np.cov(samples.T)
642642

643643
print '****** HMC INTERNALS ******'
644-
print 'final stepsize', sampler.stepsize.value
645-
print 'final acceptance_rate', sampler.avg_acceptance_rate.value
644+
print 'final stepsize', sampler.stepsize.get_value()
645+
print 'final acceptance_rate', sampler.avg_acceptance_rate.get_value()
646646

647647
return sampler
648648

649649
def test_hmc():
650650
sampler = sampler_on_nd_gaussian(HMC_sampler.new_from_shared_positions,
651651
burnin=1000, n_samples=1000, dim=5)
652652
assert abs(sampler.avg_acceptance_rate - sampler.target_acceptance_rate) < .1
653-
assert sampler.stepsize.value >= sampler.stepsize_min
654-
assert sampler.stepsize.value <= sampler.stepsize_max
653+
assert sampler.stepsize.get_value() >= sampler.stepsize_min
654+
assert sampler.stepsize.get_value() <= sampler.stepsize_max
655655

656656

657657
The above code can be run using the command: "nosetests -s code/mcrbm/test\_hmc.py". The output is as follows:

0 commit comments

Comments
 (0)