python - Theano中变量的形状有什么区别

标签 python numpy theano

你能告诉我 Theano 中的形状 0、(?,)、(1,?)、(?,?) 有什么区别吗? 为什么我定义的数组为

arr = np.array([1,2,3])

是 (3,) 的数组?我如何定义 (3,1) 数组?

此外,我编写的代码如下:

import theano.tensor as T
from theano import shared
import numpy as np
from theano import function


class hiddenLayer():
    """ Hidden Layer class
    """
    def __init__(self, inputs, n_in, n_out, act_func):
        rng = np.random
        self.W = shared(np.asarray(rng.uniform(low=-4*np.sqrt(6. / (n_in + n_out)),
                                               high=4*np.sqrt(6. / (n_in + n_out)),
                                               size=(n_in, n_out)),
                                   dtype=T.config.floatX),
                        name='W')
        self.inputs = inputs
        self.b = shared(np.zeros(n_out, dtype=T.config.floatX), name='b')
        self.x = T.dvector('x')
        self.z = T.dot(self.x, self.W) + self.b
        self.ac = function([self.x], self.z)

a = hiddenLayer(np.asarray([1, 2, 3], dtype=T.config.floatX), 3, 3, T.tanh)
print a.ac(a.inputs, a.z)

为什么报错:

  'Expected an array-like object, but found a Variable: '
TypeError: ('Bad input argument to theano function at index 1(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')

非常感谢!

最佳答案

您尝试将 a.z 传递给 a.ac(),而 a.z 实际上是结果 a.ac(x)!

相反,您可能想要这样做:

a.ac(a.inputs)
# array([  8.61379147, -13.0183053 ,  -4.41056323])

符号变量a.z的值在a.xa.Wa.b全部被求值之前是不确定的。 theano.function 的语法工作原理如下:

find_x = theano.function([<inputs needed to compute x>], <output x>)

当你真正想要调用find_x()时,你只需要给它方括号中的内容,theano.function的第二个参数将是find_x() 的返回值。

关于python - Theano中变量的形状有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23634582/

相关文章:

解释器中的python多处理池断言错误

python - 为什么这一行会产生错误?

c# - 用 IronPython 对象替换 .NET 对象实例

python - 如何向量化这个二维矩阵运算?

python - 如何将二进制网格图像转换为二维数组?

python - 计算图的顶点对上函数的双重求和值

python - 简单千层面网络输出很慢

python - 求解线性规划的 Theano 示例

python - 传递给 grpc_tools.protoc 的命令行参数是什么

python - Django Wagtail CMS 迁移 : Cannot resolve bases for [<ModelState: 'app.CustomPage' >