我可以在theano中获得共享变量的形状信息吗?

Can I have the shape information of the shared variable in theano?

好像variable.shape会通知我

AttributeError: 'SharedVariable' object has no attribute 'shape'

而 theano.tensor.shape(变量)将 return 我 shape.0

我真的很困惑,为什么我不能得到关于它的形状信息?当我想获取符号变量的形状信息时,也会出现同样的问题。真是太奇怪了。

x = T.matrix('x')   # the data is presented as rasterized images
y = T.ivector('y')  # the labels are presented as 1D vector of
                        # [int] labels

layer0_input = x.reshape((batch_size, 1, 28, 28))

在上面的示例中,x(符号变量)已被重塑为某种形状,如果我无法检索其形状信息,同时仍可以为其分配新形状,这对我来说没有意义。

第一个错误可能是由于您试图在数据类型 SharedVariable 上计算 shape 属性,而不是在实际的共享变量上计算。

否则得到shape.0是完全正常的:这是表示形状的符号表达式,先验未知。一旦你用数据进行评估,你就会看到形状:

import theano
import theano.tensor as T
import numpy as np
s = theano.shared(np.arange(2 * 3 * 5).reshape(2, 3, 5))
print(s.shape)  # gives you shape.0
print(s.shape.eval())  # gives you an array containing 2, 3, 5

a = T.tensor3()
print(a.shape)  # gives you shape.0
print(a.shape.eval({a: np.arange(2 * 3 * 5).reshape(2, 3, 5).astype(theano.config.floatX)}))  # gives 2, 3, 5