2016-10-20 4 views
3

I theano에 로지스틱 회귀 분석을 할 수있는 다음과 같은 코드를 가지고 있지만 차원 불일치 오류가 점점 계속 :Theano 로지스틱 회귀 차원 불일치

오류 theano가 제공
inputs = [[0,0], [1,1], [0,1], [1,0]] 
outputs = [0, 1, 0, 0] 

x = T.dmatrix("x") 
y = T.dvector("y") 
b = theano.shared(value=1.0, name='b') 

alpha = 0.01 
training_steps = 30000 

w_values = np.asarray(np.random.uniform(low=-1, high=1, size=(2, 1)), dtype=theano.config.floatX) 
w = theano.shared(value=w_values, name='w', borrow=True) 

hypothesis = T.nnet.sigmoid(T.dot(x, w) + b) 
cost = T.sum((y - hypothesis) ** 2) 
updates = [ 
    (w, w - alpha * T.grad(cost, wrt=w)), 
    (b, b - alpha * T.grad(cost, wrt=b)) 
] 

train = theano.function(inputs=[x, y], outputs=[hypothesis, cost], updates=updates) 
test = theano.function(inputs=[x], outputs=[hypothesis]) 

# Training 
cost_history = [] 

for i in range(training_steps): 
    if (i+1) % 5000 == 0: 
     print "Iteration #%s: " % str(i+1) 
     print "Cost: %s" % str(cost) 
    h, cost = train(inputs, outputs) 
    cost_history.append(cost) 

은 다음과 같습니다 그래서

Input dimension mis-match. (input[0].shape[1] = 4, input[1].shape[1] = 1) 
Apply node that caused the error: Elemwise{sub,no_inplace}(InplaceDimShuffle{x,0}.0, Elemwise{Composite{scalar_sigmoid((i0 + i1))}}[(0, 0)].0) 
Toposort index: 7 
Inputs types: [TensorType(float64, row), TensorType(float64, matrix)] 
Inputs shapes: [(1L, 4L), (4L, 1L)] 
Inputs strides: [(32L, 8L), (8L, 8L)] 
Inputs values: [array([[ 0., 1., 0., 0.]]), array([[ 0.73105858], 
     [ 0.70988924], 
     [ 0.68095791], 
     [ 0.75706749]])] 

을 문제는 y가 1x4로 처리되는 것입니다. 반면 가설 값은 4x1이므로 비용을 계산할 수 없습니다.

입력을 4x1로 재구성하려고했습니다. 다음 날 또 다른 차원의 관련 오류 제공

outputs = np.array([0, 1, 0, 0]).reshape(4,1)

: 때문에 코드에서

('Bad input argument to theano function with name "F:/test.py:32" at index 1(0-based)', 'Wrong number of dimensions: expected 1, got 2 with shape (4L, 1L).')

답변

1

hypothesisy가되면, 또 다른 한편으로 n_sample * 1로 모양의 행렬 벡터. 차원 불일치가 발생합니다. hypothesis을 평탄화하거나 y을 재구성 할 수 있습니다. 다음 코드가 작동합니다.

inputs = [[0,0], [1,1], [0,1], [1,0]] 
outputs = [0, 1, 0, 0] 
outputs = np.asarray(outputs, dtype='int32').reshape((len(outputs), 1)) 

x = T.dmatrix("x") 
# y = T.dvector("y") 
y = T.dmatrix("y") 
b = theano.shared(value=1.0, name='b') 

alpha = 0.01 
training_steps = 30000 

w_values = np.asarray(np.random.uniform(low=-1, high=1, size=(2, 1)), dtype=theano.config.floatX) 
w = theano.shared(value=w_values, name='w', borrow=True) 

hypothesis = T.nnet.sigmoid(T.dot(x, w) + b) 
# hypothesis = T.flatten(hypothesis) 
cost = T.sum((y - hypothesis) ** 2) 
updates = [ 
    (w, w - alpha * T.grad(cost, wrt=w)), 
    (b, b - alpha * T.grad(cost, wrt=b)) 
] 

train = theano.function(inputs=[x, y], outputs=[hypothesis, cost], updates=updates) 
test = theano.function(inputs=[x], outputs=[hypothesis]) 

# Training 
cost_history = [] 

for i in range(training_steps): 
    if (i+1) % 5000 == 0: 
     print "Iteration #%s: " % str(i+1) 
     print "Cost: %s" % str(cost) 
    h, cost = train(inputs, outputs) 
    cost_history.append(cost) 
+0

감사합니다. 당신은 또한 가설을 평평하게하는 대신에'y'를 재 형성하는 방법의 예를 제공 할 수 있습니까? – Simon

+0

안녕하세요 @ 사이먼, 나는 평평한 코드에 주석을 달고 모양을 바꾼 코드를 추가했습니다. –

+0

내가 int32 대신에 float32를 사용해야했던 모양 변경 코드를 시도했을 때 위의 질문과 동일한 오류가 발생했습니다. – Simon