简体   繁体   English

我应该如何在theano函数中分配一个numpy数组?

[英]How should I allocate a numpy array inside theano function?

Let's say I have a theano function: 假设我有一个theano函数:

def my_fun(x, y):
  # Create output array for example sake
  z = np.asarray(
    shape=(x.shape[0], y.shape[1]),
    dtype=theano.config.floatX
  )

  z = x + y

  # this is wrong, how should I convert this to a theano
  # tensor?
  return z

x = theano.tensor.dmatrix("x")
y = theano.tensor.dmatrix("y")

f = function(
  inputs=[x, y],
  outputs=[my_fun]
)

a = numpy.asarray([[1,2],[3,4]])
b = numpy.asarray([[1,2],[3,4]])

c = my_fun(a,b)
  1. How should I allocate tensors/ arrays or memory within the actual theano to be optimized when compiled by theano. 当由theano编译时,我应如何在实际theano中分配张量/数组或内存以进行优化。
  2. How should I convert that allocated tensor/ array whatever to a theano like variable to be returned? 我应该如何将分配的张量/数组转换为要返回的theano类变量? I've tried converting it to a shared variable in the function but that didn't work. 我试过将其转换为函数中的共享变量,但这没有用。

I'm sorry but I don't understand your specific questions but can comment on the code sample you provided. 抱歉,我不明白您的具体问题,但可以对您提供的代码示例发表评论。

Firstly, your comment above return z is incorrect. 首先,您在return z上方的评论不正确。 If x and y are Theano variables then z will also be a Theano variable after z = x + y . 如果xy是Theano变量,则zz = x + y之后也将是Theano变量。

Secondly, there is no need to pre-allocate memory, using numpy, for return variables. 其次,不需要使用numpy预先为返回变量分配内存。 So your my_fun can change to simply 因此,您的my_fun可以更改为

def my_fun(x, y):
  z = x + y
  return z

Thirdly, the output(s) of Theano functions need to be Theano variables, not Python functions. 第三,Theano函数的输出需要是Theano变量,而不是Python函数。 And the output needs to be a function of the inputs. 输出必须是输入的函数。 So your theano.function call needs to be changed to 因此,您的theano.function调用需要更改为

f = function(
  inputs=[x, y],
  outputs=[my_fun(x, y)]
)

The most important point to grasp about Theano, which can be a little difficult to get one's head around when starting out, is the difference between the symbolic world and the executable world. 关于Theano的最重要要点是,符号世界和可执行世界之间的区别,在开始时可能很难理解。 Tied in to that is the difference between Python expressions and Theano expressions. 与此相关的是Python表达式和Theano表达式之间的区别。

The modified my_fun above could be used like a symbolic function or like a normal executable Python function but it behaves differently for each. 上面修改后的my_fun可以像符号函数一样使用,也可以像普通的可执行Python函数一样使用,但每个函数的行为都不同。 If you pass in normal Python inputs then the addition operation occurs immediately and the return value is the result of the computation. 如果传入普通的Python输入,则加法运算将立即发生,并且返回值是计算结果。 So my_fun(1,2) returns 3 . 所以my_fun(1,2)返回3 If instead you pass in symbolic Theano variables then the addition operation does not take place immediately. 相反,如果您传递符号Theano变量,则加法运算不会立即进行。 Instead the function returns a symbolic expression that after later being compiled and executed will return the result of adding two inputs. 取而代之的是,该函数返回一个符号表达式,该符号表达式在稍后编译和执行后将返回添加两个输入的结果。 So the result of my_fun(theano.tensor.scalar(), theano.tensor.scalar()) is a Python object that represents a symbolic Theano computation graph. 因此, my_fun(theano.tensor.scalar(), theano.tensor.scalar())是一个代表对象Theano计算图的Python对象。 When that result is passed as the output to a theano.function it is compiled into something that is executable. 当将该结果作为输出传递到theano.function它将被编译为可执行文件。 Thean when the compiled function is executed, and given some concrete values for the inputs, you actually get the result you were looking for. 然后,当执行编译的函数并为输入指定一些具体值时,您实际上会得到想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM