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

Machine learning 我可以在theano中获得共享变量的形状信息吗?,machine-learning,neural-network,theano,deep-learning,Machine Learning,Neural Network,Theano,Deep Learning,似乎variable.shape会通知我 AttributeError: 'SharedVariable' object has no attribute 'shape' 而ano.tensor.shape(变量)将返回一个shape.0 我真的很困惑,为什么我不能得到关于它的形状信息?当我想要获得符号变量的形状信息时,也会出现同样的问题。这太奇怪了 x = T.matrix('x') # the data is presented as rasterized images y = T.i

似乎variable.shape会通知我

AttributeError: 'SharedVariable' object has no attribute 'shape'
而ano.tensor.shape(变量)将返回一个
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(符号变量)已被重塑为某种形状,如果我无法检索其形状信息,但仍然可以为其分配新形状,则if对我来说没有意义。

第一个错误可能是因为您试图对数据类型
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

非常感谢你。你能看看我的另一个问题吗?