Search code examples
pythontheano

How to change value of a shared variable in Theano?


I define the following class:

class test:

    def __init__(self):
        self.X = theano.tensor.dmatrix('x')
        self.W = theano.shared(value=numpy.zeros((5, 2), dtype=theano.config.floatX), name='W', borrow=True)
        self.out = theano.dot(self.X, self.W)

    def eval(self, X):
        _eval = theano.function([self.X], self.out)
        return _eval(X)

After that I try to change the value of the W matrix and calculate with the new value. I do it in the following way:

m = test()
W = np.transpose(np.array([[1.0, 2.0, 3.0, 4.0, 5.0], [2.0, 2.0, 3.0, 3.0, 3.0]]))
dn.W = theano.shared(value=W, name='W', borrow=True)
dn.eval(X)

The results that I get corresponds to the value of W that has been set in the __init__ (all elements are zeros).

Why the class does not see the new value of W that I set explicitly after the initialization?


Solution

  • You just created a new shared variable to python variable dn.W, however the theano's internal compute graph is still linked to the old shared variable.

    To change value stored in existing shared variable:

    W = np.transpose(np.array([[1.0, 2.0, 3.0, 4.0, 5.0], [2.0, 2.0, 3.0, 3.0, 3.0]]))
    dn.W.set_value(W))
    

    NOTE If you want to use results from a function call to update shared variable, the better way is to use updates argument of theano.function. This eliminates unnecessary memory transfer if shared variable is stored in GPU.