2016-11-29 85 views
0

我试图从Theano文档运行Logistic回归example。代码如下所示:Theano - Logistic回归 - 维数错误

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)。

+0

这是说,它不能运行在两个矩阵B/C他们的尺寸不允许它内部产品。 – eggie5

+0

@ eggie5我得到了错误,我将y定义为一个矩阵而不是向量。谢谢您的帮助! –

回答

0

明白了,在我的情况下,输出变量y应该是一个漂浮物的向量T.dvector("y"),但我定义为一个矩阵,而不是T.dmatrix("y")。这就是为什么dot产品y * T.log(p_!)没有发生。