view pylearn/shared/layers/tests/test_sigmoidal_layer.py @ 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 912be602c3ac
children
line wrap: on
line source

import numpy
from pylearn.shared.layers import SigmoidalLayer, LogisticRegression
from theano import tensor
from theano.compile import shared, pfunc

def test_w_random(dtype='float64'):
    if dtype == 'float64':
        x = tensor.dmatrix()
    else:
        x = tensor.fmatrix()
    y = tensor.lvector()
    rng = numpy.random.RandomState(23455)

    bsize=10
    n_in = 10
    n_hid = 12
    n_out = 2
    n_iter=100

    layer = SigmoidalLayer.new(rng, x, n_in, n_hid, squash_fn='tanh', dtype=dtype)
    out = LogisticRegression.new(layer.output, n_hid, 2)
    cost = out.nll(y).sum()
    params = out.params+layer.params
    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.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')
    f0 = f(xval, yval)
    for i in xrange(n_iter):
        fN = f(xval, yval)
        print i, 'rval', fN

    assert f0 > 6
    assert fN < 2

    assert numpy.all(w0 != layer.w.get_value(borrow=True))
    assert numpy.all(b0 != layer.b.get_value(borrow=True))