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)。
這是說,它不能運行在兩個矩陣B/C他們的尺寸不允許它內部產品。 – eggie5
@ eggie5我得到了錯誤,我將y定義爲一個矩陣而不是向量。謝謝您的幫助! –