2016-10-15 29 views
0

我写了一个简单的神经网络来学习AND门。我想知道为什么我从来没有成本的降低,预测总是0.5:Theano学习与门

import numpy as np 
import theano 
import theano.tensor as T 

inputs = [[0,0], [1,1], [0,1], [1,0]] 
outputs = [[0], [1], [0], [0]] 

x = theano.shared(value=np.asarray(inputs), name='x') 
y = theano.shared(value=np.asarray(outputs), name='y') 

alpha = 0.1 

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

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

train = theano.function(inputs=[], outputs=[], updates=updates) 
test = theano.function(inputs=[], outputs=[output]) 
calc_cost = theano.function(inputs=[], outputs=[cost]) 

for i in range(60000): 
    if (i+1) % 10000 == 0: 
     print(i+1) 
     print(calc_cost()) 
    train()  

print(test()) 

输出始终是相同的:

10000 
[array(1.0)] 
20000 
[array(1.0)] 
30000 
[array(1.0)] 
40000 
[array(1.0)] 
50000 
[array(1.0)] 
60000 
[array(1.0)] 

[array([[ 0.5], 
     [ 0.5], 
     [ 0.5], 
     [ 0.5]])] 

它似乎总是预测0.5无论输入因学习

期间费用不偏离1。如果我切换输出,[[0], [1], [1], [1]]学习或门,我得到了正确的预测,并正确地降低成本

回答

1

您的模型形式

<w, x> 

因此不能建立哪个不穿过原点任何分离的。这样的等式只能表达经过点(0,0)的线,并且显然将与门((1,1)与其他任何东西)分开的线不会穿过原点。你必须添加偏置项,因此你的模型是

<w, x> + b 
+0

为什么不改变标签时相同的模型工作,学习或门?我认为这也会有一个负面倾斜的分隔符,不会越过原点? – Simon

+0

如果您认为正在使用分离器会给出正确的“负值”,或门分离器可能会跨越原点。它不会是最好的,但足够体面使用。 – lejlot