Python 如何修复Theano v0.8教程代码中的尺寸错误

Python 如何修复Theano v0.8教程代码中的尺寸错误,python,theano,Python,Theano,我正在关注Theano的v0.8版本中的 它包括创建和培训后勤回归的示例,如下所示: rng = np.random N = 400 # training sample size feats = 784 # number of input vars D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2)) # generate a dataset training_steps = 10000 x, y = T.matrix

我正在关注Theano的v0.8版本中的

它包括创建和培训后勤回归的示例,如下所示:

rng = np.random 
N = 400 # training sample size
feats = 784 # number of input vars

D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2)) # generate a dataset

training_steps = 10000

x, y = T.matrix('x'), T.matrix('y')

w = theano.shared(rng.randn(feats), name='w') # init a weight vector randomly
b = theano.shared(0., name='b') # init bias variable
                                # both w and b are 'shared'

print "logistic regression: initial model:"
print w.get_value()
print b.get_value()

                                                # build expression graph
p_1 = 1/(1+T.exp(-T.dot(x,w)-b))                # Probability that target = 1
prediction = p_1 > 0.5                          # prediction threshold
xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1)   # Cross-entropy loss function
cost = xent.mean() + 0.01 * (w ** 2).sum()      # The cost to minimize
gw, gb = T.grad(cost, [w, b])                   # Cost gradient = func(w,b)

train = theano.function(                        # compile training function
    inputs=[x,y],
    outputs=[prediction, xent],
    updates=((w, w - 0.1 * gw), (b, b - 0.1 * gb)))

predict = theano.function(inputs=[x], outputs=prediction)

for i in range(training_steps):                 # do the training
    pred, err = train(D[0], D[1])
Theano正在抛出以下错误:

TypeError:(“名称为“”的无函数的输入参数不正确。” 索引1处的“/tut.py:206”(基于0)“尺寸编号错误: 预期为2,得到1,形状为(400,)。)


我有理由肯定修复是简单的(由于我在Theano的新手身份),并且可能涉及到一个重塑步骤。本教程没有很好的提示。建议?

在将
D[1]
用于theano函数之前,请尝试对其进行重塑,可能是这样的(我没有尝试过,如果它不起作用,请告诉我):

发生错误的原因是您使用
rng.randint(size=N,low=0,high=2)
在一维数组中初始化
D[1]
,但它传递给矩阵(二维)变量
y

另一个简单的解决方案是使用向量代替变量
y
的矩阵:

y = T.vector("y")

哪一行导致了错误?这个:pred,err=train(D[0],D[1])?您提到的教程使用T.vector表示“y”,但您使用的是T.matrix;标签(通常)总是向量的(至少在分类问题上是这样),我忽略了y labels变量的T.vector问题。谢谢你指出!我忽略了变量y的向量与矩阵问题。非常感谢。
y = T.vector("y")