changeset 1447:fbe470217937

Use .get_value() and .set_value() of shared instead of the .value property
author Pascal Lamblin <lamblinp@iro.umontreal.ca>
date Wed, 16 Mar 2011 20:20:02 -0400
parents 6e50d209b5f1
children 8110ca3cec3f
files pylearn/sampling/hmc.py pylearn/sampling/mcmc.py pylearn/sampling/tests/test_hmc.py pylearn/sampling/tests/test_mcmc.py pylearn/shared/layers/tests/test_kouh2008.py pylearn/shared/layers/tests/test_sigmoidal_layer.py
diffstat 6 files changed, 23 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/sampling/hmc.py	Tue Mar 08 12:50:37 2011 -0500
+++ b/pylearn/sampling/hmc.py	Wed Mar 16 20:20:02 2011 -0400
@@ -237,7 +237,7 @@
         # allocate shared vars
 
         if shared_positions_shape==None:
-            shared_positions_shape = shared_positions.value.shape
+            shared_positions_shape = shared_positions.get_value(borrow=True).shape
         batchsize = shared_positions_shape[0]
 
         stepsize = shared(numpy.asarray(initial_stepsize).astype(theano.config.floatX), 'hmc_stepsize')
@@ -289,7 +289,7 @@
         `borrow=True`.
         """
         self.simulate()
-        return self.positions.value.copy()
+        return self.positions.get_value(borrow=False)
 
     def updates(self):
         """Returns the update expressions required to simulate the Markov Chain
--- a/pylearn/sampling/mcmc.py	Tue Mar 08 12:50:37 2011 -0500
+++ b/pylearn/sampling/mcmc.py	Wed Mar 16 20:20:02 2011 -0400
@@ -55,7 +55,7 @@
             The len of this vector is the batchsize.
         """
 
-        batchsize = positions[0].value.shape[0]
+        batchsize = positions[0].get_value(borrow=True).shape[0]
         self.s_rng = TT.shared_randomstreams.RandomStreams(seed)
         self.positions = positions
         self.prev_energy = shared(np.zeros(batchsize) + float('inf'))
@@ -64,7 +64,7 @@
 
         s_stepsize = TT.scalar('stepsize')
 
-        new_positions = [p + s_stepsize * self.s_rng.normal(size=p.value.shape)
+        new_positions = [p + s_stepsize * self.s_rng.normal(size=p.get_value(borrow=True).shape)
                 for p in self.positions]
 
         # accept-reject according to Metropolis-Hastings 
@@ -90,7 +90,7 @@
                 self.stepsize = min(self.stepsize*self.stepsize_inc,self.stepsize_max)
 
     def get_position(self):
-        return [q.value for q in self.positions]
+        return [q.get_value(borrow=True) for q in self.positions]
 
     def draw(self, n_steps=None):
         """Return the current sample in the Markov chain as a list of numpy arrays
--- a/pylearn/sampling/tests/test_hmc.py	Tue Mar 08 12:50:37 2011 -0500
+++ b/pylearn/sampling/tests/test_hmc.py	Wed Mar 16 20:20:02 2011 -0400
@@ -22,16 +22,16 @@
     position = shared(rng.randn(batchsize, 2).astype(theano.config.floatX))
     sampler = sampler_cls(position, gaussian_energy)
 
-    print 'initial position', position.value
-    print 'initial stepsize', sampler.stepsize.value
+    print 'initial position', position.get_value(borrow=True)
+    print 'initial stepsize', sampler.stepsize.get_value(borrow=True)
 
     # DRAW SAMPLES
 
     samples = [sampler.draw() for r in xrange(burnin)] #burn-in
     samples = np.asarray([sampler.draw() for r in xrange(n_samples)])
 
-    assert sampler.avg_acceptance_rate.value > 0
-    assert sampler.avg_acceptance_rate.value < 1
+    assert sampler.avg_acceptance_rate.get_value() > 0
+    assert sampler.avg_acceptance_rate.get_value() < 1
 
     # TEST THAT THEY ARE FROM THE RIGHT DISTRIBUTION
 
@@ -42,8 +42,8 @@
     #assert np.all(abs(mu - samples.mean(axis=0)) < 1)
 
 
-    print 'final stepsize', sampler.stepsize.value
-    print 'final acceptance_rate', sampler.avg_acceptance_rate.value
+    print 'final stepsize', sampler.stepsize.get_value()
+    print 'final acceptance_rate', sampler.avg_acceptance_rate.get_value()
 
     print 'target cov', cov
     s = samples[:,0,:]
@@ -59,7 +59,7 @@
 def test_hmc():
     print ('HMC')
     sampler = _sampler_on_2d_gaussian(HMC_sampler.new_from_shared_positions, burnin=3000/20, n_samples=90000/20)
-    assert abs(sampler.avg_acceptance_rate.value - sampler.target_acceptance_rate) < .1
-    assert sampler.stepsize.value >= sampler.stepsize_min
-    assert sampler.stepsize.value <= sampler.stepsize_max
+    assert abs(sampler.avg_acceptance_rate.get_value() - sampler.target_acceptance_rate) < .1
+    assert sampler.stepsize.get_value() >= sampler.stepsize_min
+    assert sampler.stepsize.get_value() <= sampler.stepsize_max
 
--- a/pylearn/sampling/tests/test_mcmc.py	Tue Mar 08 12:50:37 2011 -0500
+++ b/pylearn/sampling/tests/test_mcmc.py	Wed Mar 16 20:20:02 2011 -0400
@@ -22,7 +22,7 @@
     position = shared(rng.randn(batchsize, 2).astype(theano.config.floatX))
     sampler = sampler_cls([position], gaussian_energy)
 
-    print 'initial position', position.value
+    print 'initial position', position.get_value(borrow=True)
     print 'initial stepsize', sampler.stepsize
 
     # DRAW SAMPLES
--- a/pylearn/shared/layers/tests/test_kouh2008.py	Tue Mar 08 12:50:37 2011 -0500
+++ b/pylearn/shared/layers/tests/test_kouh2008.py	Wed Mar 16 20:20:02 2011 -0400
@@ -60,7 +60,9 @@
     out = LogisticRegression.new(layer.output, n_out, 2)
     cost = out.nll(y).sum()
     #joint optimization except for one of the linear filters
-    out.w.value += 0.1 * rng.rand(*out.w.value.shape)
+    out.w.set_value((out.w.get_value(borrow=True) +
+                     0.1 * rng.rand(*out.w.get_value(borrow=True).shape)),
+            borrow=True)
     params = layer.params[:-2]
     mode = None
     updates = [(p, p - numpy.asarray(0.001, dtype=dtype)*gp) for p,gp in zip(params, tensor.grad(cost, params)) ]
--- a/pylearn/shared/layers/tests/test_sigmoidal_layer.py	Tue Mar 08 12:50:37 2011 -0500
+++ b/pylearn/shared/layers/tests/test_sigmoidal_layer.py	Wed Mar 16 20:20:02 2011 -0400
@@ -24,8 +24,8 @@
     updates = [(p, p - numpy.asarray(0.01, dtype=dtype)*gp) for p,gp in zip(params, tensor.grad(cost, params)) ]
     f = pfunc([x, y], cost, updates=updates)
 
-    w0 = layer.w.value.copy()
-    b0 = layer.b.value.copy()
+    w0 = layer.w.get_value(borrow=False)
+    b0 = layer.b.get_value(borrow=False)
 
     xval = numpy.asarray(rng.rand(bsize, n_in), dtype=dtype)
     yval = numpy.asarray(rng.randint(0,2,bsize), dtype='int64')
@@ -35,7 +35,7 @@
         print i, 'rval', fN
 
     assert f0 > 6
-    assert fN < 2 
+    assert fN < 2
 
-    assert numpy.all(w0 != layer.w.value)
-    assert numpy.all(b0 != layer.b.value)
+    assert numpy.all(w0 != layer.w.get_value(borrow=True))
+    assert numpy.all(b0 != layer.b.get_value(borrow=True))