Using a shared variable in a function

Hi I am following the tutorial on neural network where the author seems to be using shared variables everywhere. In my opinion, a shared variable in theanos is just a space in memory that can be used by a bunch of gpu and cpu. Anyway, I have two matrices that I declare as shared variables and I want to perform some kind of operation on them using a function. (Question 1) I would love it if anyone could explain why a function is useful versus a regular def function. Anyway, I customize my definition like this:

import theano
import theano.tensor as T
from theano import function
import numpy as np

class Transform:
    def __init__(self, dimg):
        dimg = dimg.astype(theano.config.floatX)
        self.in_t = theano.shared(dimg, name='dimg', borrow=True)

    def rotate(self, ox, oy, radians):
        value = np.zeros((2 * self.in_t.get_value().shape[0],
                          2 * self.in_t.get_value().shape[1]))
        out_t = theano.shared(value,
                              name='b',
                              dtype=theano.config.floatX),
                              borrow=True)    
        din = theano.tensor.dmatrix('a')
        dout = theano.tensor.dmatrix('b')

        def atest():
            y = x + y
            return y

        f = function(inputs=[],
                     givens={x: self.in_t,
                             y: self.out_t},
                     outputs=atest)    
        return f()

      

The problem is that I have no idea how to use shared variables in a regular function-output call. I understand that I can do updates via the function ([], .. update = (shared_var_1, upate_function)). But how can I access them in my normal function?

+3


source to share


1 answer


Teano is new to here, so I'm not sure if my answer will cover all the technical aspects.

Answering your first question : you need to declare the anano function instead of the def function, because theano is like a "language" inside python and calling theano.function

you compile some special C code to do your task under the hood. This is what makes Theano fast. From the doc:

It is good to think of it theano.function

as an interface to a compiler that builds a callable from a purely symbolic graph. One of the most important features of Theanos is that theano.function can optimize the graph and even compile some or all of them into native machine instructions.

About your second quetion , in order to access what is stored in your shared variable, you should use

shared_var.get_value()

      

Check these examples:

The value can be accessed and changed using .get_value()

and .set_value()

.

This code:

a = np.array([[1,2],[3,4]], dtype=theano.config.floatX)
x = theano.shared(a)
print(x)

      

Will output



<CudaNdarrayType(float32, matrix)>

      

But using get_value()

:

print(x.get_value())

      

It outputs

[[ 1.  2.]
 [ 3.  4.]]

      

Edit: use shared variables in functions

import theano
import numpy
a = numpy.int64(2)
y = theano.tensor.scalar('y',dtype='int64')
z = theano.tensor.scalar('z',dtype='int64')
x = theano.shared(a)
plus = y + z
theano_sum = theano.function([y,z],plus)
# Using shared variable in a function
print(theano_sum(x.get_value(),3))
# Changing shared variable value using a function
x.set_value(theano_sum(2,2))
print(x.get_value())
# Update shared variable value
x.set_value(x.get_value(borrow=True)+1)
print(x.get_value())

      

The output will be:

5
4
5

      

+4


source







All Articles