Hi I'm following a neural net tutorial where the author seems to be using shared variables everywhere. From my understanding, a shared variable in theanos simply is a space in memory that can be shared by the gpu and cpu heap. Anyway, I have two matrices which I declare as shared variables and I want to perform some operation on them using function. (Question 1) I'd love it if someone could explain why function is usefull vs regular def function. Anyway, I'm setting up my definition like such:
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 the shared variables in a regular function-output call. I understand that I can do updates via function([],..update=(shared_var_1, upate_function)). But how do I access them in my regular function?
Theano beginner here, so I'm not sure that my answer will cover all the technical aspects.
Answering your first question: you need to declare theano function instead of def function, because theano is like a "language" inside python and invoking theano.function
you're compiling some ad-hoc C code performing your task under the hood. This is what makes Theano fast.
From the documentation:
It is good to think of
theano.function
as the interface to a compiler which builds a callable object from a purely symbolic graph. One of Theano’s most important features is that theano.function can optimize a graph and even compile some or all of it into native machine instructions.
About your second quetion, in order to access what's stored in your shared variable you should use
shared_var.get_value()
Check these examples:
The value can be accessed and modified by the
.get_value()
and.set_value()
methods.
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: to 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())
Will output:
5
4
5