为什么我不能正确使用 theano.tensor.argmax 和 theano.tensor.mean

Why I can't use theano.tensor.argmax and theano.tensor.mean correctly

我现在正在学习Theano,但总有一些problems.my代码如下:

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

我以为它会打印'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.neq(a,b)

结果显示:

Elemwise{neq,no_inplace}.0

我真的很陌生,不知道,我错过了什么吗?提前谢谢你..

T.argmax() 需要 Theano TensorVariable 类型。列出了 Theano 中使用的一些变量类型 here。不要让 "fully typed constructors" 这个名字吓到你。根据您要使用哪种类型的数据作为输入,更多地考虑它们。你在使用浮点矩阵吗?那么相关的TensorVariable类型大概就是"fmatrix." Are you dealing with batches of RGB image data?那么相关的TensorVariable类型大概就是"tensor4."

在您的代码中,我们试图将列表类型输入 T.argmax()。所以从上面的观点来看,那是行不通的。另外,请注意 type(T.argmax(a)) 是 theano.tensor.var.TensorVariable 类型。所以它期望一个 TensorVariable 作为输入,它也输出一个 TensorVariable 类型。 所以这不会 return 实际的 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 语法 here

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

当我执行时:

get_argmax([1,2,3,4,19,1])

它returns:

array(4)

那么我们到底做了什么?通过定义 Theano 变量并使用 theano.tensor 函数,我们构建了一个计算图。然后我们使用 theano.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])

将return:

[1,1,1,1]

一个关键的概念差异是我将 a、b 视为 theano TensorVariables,而不是为它们分配显式变量。

你会明白的,只要记住你需要根据 Theano TensorVariables 定义你的计算,然后实际上 "use it" 你必须使用 theano.function 编译它.