如何修复 Theano v0.8 教程代码中的维度错误
How to fix dimensional error in Theano v0.8 tutorial code
我正在关注 Theano v0.8 版本中的 tutorial。
它包括一个创建和训练逻辑回归的示例,如下所示:
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: ('Bad input argument to theano function with name
"./tut.py:206" at index 1(0-based)', 'Wrong number of dimensions:
expected 2, got 1 with shape (400,).')
我有理由相信修复很简单(由于我在 Theano 的新手身份),并且可能涉及重塑步骤。本教程没有很好的提示。建议?
在theano函数中使用它之前尝试重塑D[1]
,也许是这样的(我没有尝试过,如果它不起作用告诉我):
pred, err = train(D[0], np.reshape(D[1],(400,1))
错误发生是因为您使用 rng.randint(size=N, low=0, high=2)
在一维数组中初始化 D[1]
但它传递给矩阵(二维)变量 y
或
另一个简单的解决方案是对变量使用向量而不是矩阵 y
:
y = T.vector("y")
我正在关注 Theano v0.8 版本中的 tutorial。
它包括一个创建和训练逻辑回归的示例,如下所示:
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: ('Bad input argument to theano function with name "./tut.py:206" at index 1(0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (400,).')
我有理由相信修复很简单(由于我在 Theano 的新手身份),并且可能涉及重塑步骤。本教程没有很好的提示。建议?
在theano函数中使用它之前尝试重塑D[1]
,也许是这样的(我没有尝试过,如果它不起作用告诉我):
pred, err = train(D[0], np.reshape(D[1],(400,1))
错误发生是因为您使用 rng.randint(size=N, low=0, high=2)
在一维数组中初始化 D[1]
但它传递给矩阵(二维)变量 y
或
另一个简单的解决方案是对变量使用向量而不是矩阵 y
:
y = T.vector("y")