How to reuse Theano function with different shared variables without rebuilding graph?

喜欢而已 提交于 2019-12-05 09:25:00

You can use the givens keyword in theano.function for that. Basically, you do the following.

m1 = theano.shared(name='m1', value = np.zeros((3,2)) )
m2 = theano.shared(name='m2', value = np.zeros((3,2)) )

x1 = theano.tensor.dmatrix('x1')
x2 = theano.tensor.dmatrix('x2')

y = (x1*x2).sum(axis=0)
f = theano.function([],y,givens=[(x1,m1),(x2,m2)],on_unused_input='ignore')

then to loop through values you just set the value of the shared variables to the value you'd like. You have to set the on_unused_input to 'ignore' to use functions with no arguments in theano, by the way. Like this:

array1 = array([[1,2,3],[4,5,6]])
array2 = array([[2,4,6],[8,10,12]])

for i in range(10):
    m1.set_value(i*array1)
    m2.set_value(i*array2)
    print f()

It should work, at least that's how I've been working around it.

Currently it is not easily possible to reuse a Theano function with different shared variable.

But you have alternative:

  1. Is it really a bottleneck? In the example, it is, but I suppose it is a simplified case. The only way to know is to profile it.
  2. You compile 1 Theano function with the first shared variable. Then you can call the get_value/set_value on those shared variables before calling the Theano function. This way, you won't need to recompile the Theano function.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!