python - How should I allocate a numpy array inside theano function? -
let's have theano function:
def my_fun(x, y): # create output array example sake z = np.asarray( shape=(x.shape[0], y.shape[1]), dtype=theano.config.floatx ) z = x + y # wrong, how should convert theano # tensor? return z x = theano.tensor.dmatrix("x") y = theano.tensor.dmatrix("y") f = function( inputs=[x, y], outputs=[my_fun] ) = numpy.asarray([[1,2],[3,4]]) b = numpy.asarray([[1,2],[3,4]]) c = my_fun(a,b) - how should allocate tensors/ arrays or memory within actual theano optimized when compiled theano.
- how should convert allocated tensor/ array whatever theano variable returned? i've tried converting shared variable in function didn't work.
i'm sorry don't understand specific questions can comment on code sample provided.
firstly, comment above return z incorrect. if x , y theano variables z theano variable after z = x + y.
secondly, there no need pre-allocate memory, using numpy, return variables. my_fun can change simply
def my_fun(x, y): z = x + y return z thirdly, output(s) of theano functions need theano variables, not python functions. , output needs function of inputs. theano.function call needs changed to
f = function( inputs=[x, y], outputs=[my_fun(x, y)] ) the important point grasp theano, can little difficult one's head around when starting out, difference between symbolic world , executable world. tied in difference between python expressions , theano expressions.
the modified my_fun above used symbolic function or normal executable python function behaves differently each. if pass in normal python inputs addition operation occurs , return value result of computation. my_fun(1,2) returns 3. if instead pass in symbolic theano variables addition operation not take place immediately. instead function returns symbolic expression after later being compiled , executed return result of adding 2 inputs. result of my_fun(theano.tensor.scalar(), theano.tensor.scalar()) python object represents symbolic theano computation graph. when result passed output theano.function compiled executable. thean when compiled function executed, , given concrete values inputs, result looking for.
Comments
Post a Comment