【问题标题】:Theano - Logistic Regression - Wrong number of dimensionsTheano - 逻辑回归 - 维数错误
【发布时间】:2016-11-29 09:21:47
【问题描述】:

我正在尝试运行 Theano 文档中的逻辑回归 example。代码如下:

import theano
import theano.tensor as T
import numpy

rng = numpy.random

N = 400 # training sample size
feats = 784 # number of input variables

# generate a dataset: D = (input_values, target_class)
D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
#D[1] = D[1].reshape(D[1].shape[0], 1)
training_steps = 10000

# Declare Theano symbolic variables
x = T.dmatrix("x")
y = T.dmatrix("y")

# initialize the weight vector w randomly
#
# this and the following bias variable b
# are shared so they keep their values
# between training iterations (updates)

w = theano.shared(rng.randn(feats), name="w")

# initialize the bias term
b = theano.shared(0., name="b")

print("Initial Model:")
#print(w.size())
print(b.get_value())

# Construct Theano expression graph
p_1 = 1/(1 + T.exp(-T.dot(x,w) - b)  ) # Probability that target = 1
prediction = p_1 > 0.5 # The prediction thresholded
xent = -y*T.log(p_1) - (1-y)*T.log(1 - p_1) # Cross-entropy loss function
cost = xent.mean() + 0.001* (w**2).sum()  # The cost to minimize, second term is regularization term
grad_w, grad_b = T.grad(cost, [w,b]) # Compute the gradient of the cost
                             # w.r.t weight vector w and
                             # bias term b
                             # (we shall return to this in a
                             # following section of this tutorial)

print(w.shape)
print(grad_w.shape)

# Compile
train = theano.function( inputs=[x,y],  outputs=[prediction, xent],   updates=[(w, w), (b, b)] )
predict = theano.function(inputs=[x], outputs=prediction)

# Train
for i in range(training_steps):
    pred, err = train(D[0], D[1])

print("Final model:")
print(w.get_value())
print(b.get_value())
print("target values for D:")
print(D[1])
print("prediction on D:")
print(predict(D[0]))

运行此代码时,我收到以下错误

Traceback (most recent call last):
  File "theano_log_reg.py", line 54, in <module>
    pred, err = train(D[0], D[1])
  File "D:\Programs\WinPython-64bit-3.4.4.4Qt5\python-3.4.4.amd64\lib\site-packages\theano\compile\function_module.py", line 788, in __call__
    allow_downcast=s.allow_downcast)
  File "D:\Programs\WinPython-64bit-3.4.4.4Qt5\python-3.4.4.amd64\lib\site-packages\theano\tensor\type.py", line 178, in filter
    data.shape))
TypeError: ('Bad input argument to theano function with name "theano_log_reg.py:49" at index 1 (0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (400,).')

我是 Theano 和 numpy 的新手,我无法弄清楚这个错误。任何帮助,将不胜感激。我正在使用最新版本的 Theano 运行 WinPython (Python 3.4)。

【问题讨论】:

  • 这是说它不能在两个矩阵上运行内积 b/c 他们的尺寸不允许它。
  • @eggie5 我得到了错误,我将 y 定义为矩阵而不是向量。感谢您的帮助!

标签: python numpy machine-learning theano logistic-regression


【解决方案1】:

知道了,就我而言,输出变量 y 应该是浮点向量 T.dvector("y"),但我将其定义为矩阵而不是 T.dmatrix("y")。这就是点积 y * T.log(p_!) 没有发生的原因。

【讨论】:

    猜你喜欢
    • 2017-03-02
    • 1970-01-01
    • 2015-09-20
    • 2016-02-21
    • 2019-03-05
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多