如何赋值给的elementwise矩阵theano? numpy的和Theano之间的区别? [英] How to assign values elementwise to theano matrix ? Difference between Numpy and Theano?

查看:1189
本文介绍了如何赋值给的elementwise矩阵theano? numpy的和Theano之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来theano。我想更换,以加快计算过程中我与theano功能脚本numpy的功能。我不知道该怎么做。

I'm new to theano. I would like to replace the numpy functions in my scripts with theano functions in order to speed up the calculation process. I'm not sure how to do it.

我的最终目标是仿射变换应用到3D刚体,指定分数每次转换之后的形态,并做确定分数的参数一些优化。

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.

这里是什么我试图做一个例子。

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))

以上theano功能没有通过编译。我有以下错误消息。

The above theano function didn't pass compilation. 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)是存在于分配或更新或具有特定形状的elementwise初始化theano矩阵的方式,

(1) is there a way to assign or update or initialize a theano matrix with a specific shape elementwise,

(2)theano是密切相关的numpy的,什么是定义,优化和评估数学前pressions theano和numpy的之间的区别,

(2) as theano is closely related with numpy, what's the difference between theano and numpy in defining, optimizing, and evaluating mathematical expressions,

和(3)可在theano,我们可以定义,优化和不调用numpy的功能评估数学EX pressions仅使用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.

推荐答案

我不能回答你的问题1,2,3,因为我还没有前十分钟前使用theano。但是,来定义theano的功能,你似乎没有使用 DEF 建设;你想要做这样的事情更多:

I can't answer your questions 1, 2, 3, since I haven't used theano before ten minutes ago. But, to define the function in theano, you don't seem to use the def construction; 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

这篇关于如何赋值给的elementwise矩阵theano? numpy的和Theano之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆