2016-10-20 9 views
3

私は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) 

エラーtheanoが与えるです:だから

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_標本* 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

関連する問題