简体   繁体   English

如何将元素值元素分配给theano矩阵? Numpy和Theano的区别?

[英]How to assign values elementwise to theano matrix ? Difference between Numpy and Theano?

I'm new to theano. 我是theano的新手。 I would like to replace the numpy functions in my scripts with theano functions in order to speed up the calculation process. 我想用theano函数替换脚本中的numpy函数,以加快计算过程。 I'm not sure how to do it. 我不知道怎么做。

My final goal is to apply affine transformation to 3D rigid body, assign a score to the conformation after each transformation, and do some optimization on the parameters determining the scores. 我的最终目标是将仿射变换应用于3D刚体,在每次变换后为构象指定分数,并对确定分数的参数进行一些优化。

Here's an example of what I'm trying to do. 这是我正在尝试做的一个例子。

import numpy as numpy 
import theano 
import theano.tensor as T 

pi = 3.141592653
deg2rad = lambda angle: (angle/180.)*pi 

# generate 3D transformation matrix for rotation around x axis by angle 

def rotate_x_axis_numpy(angle):  # my old numpy function 
    a    = deg2rad(angle)
    cosa = np.cos(a)
    sina = np.sin(a)
    R    = np.identity(4)
    R[1][1] = cosa; R[1][2] = -sina
    R[2][1] = sina; R[2][2] =  cosa
    return R    

angle_var = T.dscalar()

def rotate_x_axis_expr(angle): # new theano function expression I expected to work  
    a    = T.deg2rad(angle)
    cosa = T.cos(a)
    sina = T.sin(a)   
    R    = theano.shared(np.identity(4))
    R[1][1] = cosa; R[1][2] = -sina
    R[2][1] = sina; R[2][2] =  cosa
    return R

rotate_x_axis_theano = theano.function([angle_var], rotate_x_axis_expr(angle_var))

The above theano function didn't pass compilation. 上面的theano函数没有通过编译。 I've got the following error message. 我有以下错误消息。

---------------------------------------------------------------------------
TypeError     Traceback (most recent call last)<ipython-input-85-8d98ae1d1c9b> in <module>()
      17     return R
      18 
 ---> 19 rotate_x_axis_theano = theano.function([angle_var],rotate_x_axis_expr(angle_var))

<ipython-input-85-8d98ae1d1c9b> in rotate_x_axis_expr(angle)
      12   
      13 
 ---> 14     R[1][1] = cosa; R[1][2] = -sina
      15     R[2][1] = sina; R[2][2] =  cosa
      16 

TypeError: 'TensorVariable' object does not support item assignment

In general, my questions are 一般来说,我的问题是

(1) is there a way to assign or update or initialize a theano matrix with a specific shape elementwise, (1)有没有办法分配或更新或初始化具有特定形状元素的theano矩阵,

(2) as theano is closely related with numpy, what's the difference between theano and numpy in defining, optimizing, and evaluating mathematical expressions, (2)由于theano与numpy密切相关,theano和numpy在定义,优化和评估数学表达式方面的区别是什么,

and (3) can theano replace numpy in the sense that we can use theano functions solely in defining, optimizing, and evaluating mathematical expressions without calling numpy functions. (3)theano可以替代numpy,因为我们可以仅仅在定义,优化和评估数学表达式时使用theano函数而不需要调用numpy函数。

I can't answer your questions 1, 2, 3, since I haven't used theano before ten minutes ago. 我不能回答你的问题1,2,3,因为我在十分钟前没有使用过theano。 But, to define the function in theano, you don't seem to use the def construction; 但是,要在theano中定义函数,您似乎不使用def构造; you want to do something more like this: 你想做更像这样的事情:

angle_var = T.dscalar('angle_var')
a    = T.deg2rad(angle_var)
cosa = T.cos(a)
sina = T.sin(a)   

R = theano.shared(np.identity(4))
R = T.set_subtensor(R[1,1],  cosa)
R = T.set_subtensor(R[1,2], -sina)
R = T.set_subtensor(R[2,1],  sina)
R = T.set_subtensor(R[2,2],  cosa)

rotate_x_axis_theano = theano.function([angle_var], R)

Doesn't help much with speed though, for a scalar angle at least: 对速度没有多大帮助,至少是一个标量角:

In [368]: timeit rotate_x_axis_theano(10)
10000 loops, best of 3: 67.7 µs per loop

In [369]: timeit rotate_x_axis_numpy(10)
The slowest run took 4.23 times longer than the fastest. This could mean that an intermediate result is being cached
10000 loops, best of 3: 22.7 µs per loop

In [370]: np.allclose(rotate_x_axis_theano(10), rotate_x_axis_numpy(10))
Out[370]: True

Just for getting the theano function posted above work, my version is: 只是为了让上面发布的theano功能工作,我的版本是:

angle_var = T.dscalar()

def rotate_x_axis_expr(angle):
    a    = T.deg2rad(angle)
    cosa = T.cos(a)
    sina = T.sin(a)   

    R = theano.shared(np.identity(4))
    R = T.set_subtensor(R[1,1],  cosa)
    R = T.set_subtensor(R[1,2], -sina)
    R = T.set_subtensor(R[2,1],  sina)
    R = T.set_subtensor(R[2,2],  cosa)

    return R

rotate_x_axis = theano.function([angle_var],rotate_x_axis_expr(angle_var))

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

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