2016-11-29 7 views
0

Ich versuche die Logistische Regression example aus der Theano Dokumentation zu starten. Der Code ist unten dargestellt:Theano - Logistische Regression - Falsche Anzahl der Dimensionen

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

Während dieser Code ausgeführt wird, bekomme ich folgende Fehler

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,).') 

Ich bin neu in Theano und numpy, ich bin nicht in der Lage, diesen Fehler Figur. Jede Hilfe wäre willkommen. Ich verwende WinPython (Python 3.4) mit der neuesten Version von Theano.

+0

Es wird gesagt, dass es das innere Produkt auf zwei Matrizen b/c nicht ausführen konnte, ihre Dimensionen erlaubten es nicht. – eggie5

+0

@ eggie5 Ich habe den Fehler, ich definierte y als Matrix anstelle von Vektor. Danke für die Hilfe! –

Antwort

0

In meinem Fall soll die Ausgangsvariable y ein Vektor von Schwimmern T.dvector("y") sein, aber ich habe stattdessen eine Matrix als T.dmatrix("y") definiert. Deshalb ist das Punktprodukt y * T.log(p_!) nicht passiert.

Verwandte Themen