python - Theano学习与门

标签 python machine-learning neural-network theano

我编写了一个简单的神经网络来学习与门。我试图理解为什么我的成本永远不会降低并且预测变量始终为 0.5:

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]]来学习或门,我会得到正确的预测,并正确地降低成本

最佳答案

您的模型具有形式

<w, x>

因此它不能建立任何不跨越原点的分离。这样的方程只能表达通过点 (0,0) 的线,并且显然将 AND 门((1, 1) 与其他任何东西分开)的线不会穿过原点。您必须添加偏差项,因此您的模型是

<w, x> + b

关于python - Theano学习与门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40059460/

相关文章:

neural-network - 避免虚拟变量陷阱和神经网络

Python 有限自动机库

tensorflow - 如何修复图像识别中的错误猜测

python - 遗传算法/用神经网络打蛇没有改善

matlab - G和GHAT需要是相同的分类树

python - ValueError:形状 (100,784) 和 (4,6836) 未对齐:784 (dim 1) != 4 (dim 0)

python - 如何在tensorflow中打印梯度的总和值?

python - 使用python3从风格为列表和元组的txt文件中获取信息

python - "name ' word_tokenize ' is not defined"in python 字数频率

python - 从字符串中提取所有名词