Theano - Logistic回归 - 维数错误

时间:2016-11-29 09:21:48

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

我正在尝试从Theano文档中运行Logistic回归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)。

1 个答案:

答案 0 :(得分:0)

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