Python 为什么我能';t正确使用a no.tensor.argmax和a no.tensor.mean

Python 为什么我能';t正确使用a no.tensor.argmax和a no.tensor.mean,python,theano,Python,Theano,我现在正在学习Theano,但总有一些问题。我的代码如下: import theano from numpy import * import theano.tensor as T a = [1,2,3,4] b = [7,8,9,10] print T.argmax(a) import theano from numpy import * import theano.tensor as T a = [1,2,3,4] b = [7,8,9,10] print T.neq(a,b) 我以为

我现在正在学习Theano,但总有一些问题。我的代码如下:

import theano 
from numpy import *
import theano.tensor as T
a = [1,2,3,4]
b = [7,8,9,10]
print T.argmax(a)
import theano 
from numpy import *
import theano.tensor as T
a = [1,2,3,4]
b = [7,8,9,10]
print T.neq(a,b)
我以为它会打印“4”的索引,但结果是:

argmax
此外,当我使用T.neq()时,如下所示:

import theano 
from numpy import *
import theano.tensor as T
a = [1,2,3,4]
b = [7,8,9,10]
print T.argmax(a)
import theano 
from numpy import *
import theano.tensor as T
a = [1,2,3,4]
b = [7,8,9,10]
print T.neq(a,b)
结果表明:

Elemwise{neq,no_inplace}.0
我真的是个新手,不知道我错过了什么吗?提前谢谢你。

T.argmax()需要一个Theano TensorVariable类型。下面列出了序号中使用的一些变量类型。不要让“全类型构造函数”这个名字吓到你。更多地从您希望用作输入的数据类型的角度来考虑它们。你在使用浮点矩阵吗?那么相关的TensorVariable类型可能是“fmatrix”。您正在处理成批的RGB图像数据吗?那么相关的TensorVariable类型可能是“tensor4”

在您的代码中,我们试图将列表类型输入到T.argmax()。所以从上面的观点来看,这是行不通的。另外,请注意类型(T.argmax(a))是theano.tensor.var.TensorVariable类型。因此,它需要一个TensorVariable作为输入,并输出一个TensorVariable类型。所以这不会返回实际的argmax

好吧,那什么有用呢?我们如何在Theano中进行计算

让我们首先确定要处理的数据类型。这将是我们将要构建的计算图的起点。在本例中,看起来我们需要处理数组或向量。Theano有一个ivector类型,它是一个整数向量,或一个fvector类型,它是一个float32值向量。让我们继续使用数据并执行ivector,因为我们有整数值:

x = T.ivector('input')
这一行刚刚创建了一个TensorVariable x,它表示我们想要的输入类型,一个整数数组

现在,让我们为x元素的argmax定义一个TensorVariable:

y = T.argmax(x)
到目前为止,我们已经构建了一个计算图,它需要一个整数数组作为输入,并将输出该数组的argmax。然而,为了真正做到这一点,我们必须将其编译成一个函数:

get_argmax = theano.function([x], y)
可以找到theano.function语法

将此函数视为现在实际执行我们使用x和y定义的计算

当我执行:

get_argmax([1,2,3,4,19,1])
它返回:

array(4)
那我们到底做了什么?通过定义无变量和使用无张量函数,我们构建了一个计算图。然后,我们使用ano.function编译一个函数,该函数实际对我们指定的实际输入执行该计算

结束语:如何进行不等于运算

a = T.ivector('a')
b = T.ivector('b')
out = T.neq(a,b)
get_out = theano.function([a,b], out)
print get_out([1,2,3,4], [7,8,9,10])
将返回:

[1,1,1,1]
其中一个关键的概念上的区别是,我将a,b视为无张量变量,而不是将它们指定为显式变量

你会得到它的窍门,只要记住,你需要用无张量变量来定义你的计算,然后要真正“使用它”,你必须使用无函数来编译它