无法训练解决 XOR 映射的神经网络

Cannot train a neural network solving XOR mapping

我正在尝试为 Keras 中的 XOR 问题实现一个简单的分类器。这是代码:

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
import numpy

X = numpy.array([[1., 1.], [0., 0.], [1., 0.], [0., 1.], [1., 1.], [0., 0.]])
y = numpy.array([[0.], [0.], [1.], [1.], [0.], [0.]])
model = Sequential()
model.add(Dense(2, input_dim=2, init='uniform', activation='sigmoid'))
model.add(Dense(3, init='uniform', activation='sigmoid'))
model.add(Dense(1, init='uniform', activation='softmax'))
sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd)

model.fit(X, y, nb_epoch=20)
print()
score = model.evaluate(X, y)
print()
print(score)
print(model.predict(numpy.array([[1, 0]])))
print(model.predict(numpy.array([[0, 0]])))

我尝试更改轮数、学习率和其他参数。但是错误从第一个纪元到最后一个纪元保持不变。

Epoch 13/20
6/6 [==============================] - 0s - loss: 0.6667 
Epoch 14/20
6/6 [==============================] - 0s - loss: 0.6667
Epoch 15/20
6/6 [==============================] - 0s - loss: 0.6667
Epoch 16/20
6/6 [==============================] - 0s - loss: 0.6667
Epoch 17/20
6/6 [==============================] - 0s - loss: 0.6667
Epoch 18/20
6/6 [==============================] - 0s - loss: 0.6667
Epoch 19/20
6/6 [==============================] - 0s - loss: 0.6667
Epoch 20/20
6/6 [==============================] - 0s - loss: 0.6667

6/6 [==============================] - 0s

0.666666686535
[[ 1.]]
[[ 1.]]

你如何在 Keras 中训练这个网络?

还有,有没有更好的神经网络实现库?我试过 PyBrain,但它已被放弃,试过 scikit-neuralnetwork 但文档真的很神秘所以无法弄清楚如何训练它。我严重怀疑 Keras 是否有效。

尝试网络中最后一个没有激活函数的感知器。我有同样的问题,当你删除激活函数时它开始学习。

此外,您可以尝试将输出层拆分为 2 个神经元。并让 0 的输出为 [0,1],1 的输出为 [1,0]。

但是,删除激活函数应该可以解决问题。

这段代码对我有用:

import numpy as np
from keras.models import Sequential
from keras.layers.core import Activation, Dense
from keras.optimizers import SGD

X = np.array([[1, 1], [0, 0], [1, 0], [0, 1], [1, 1], [0, 0]], dtype='uint8')
y = np.array([[0], [0], [1], [1], [0], [0]], dtype='uint8')


model = Sequential()
model.add(Dense(2, input_dim=2))
model.add(Activation('sigmoid'))
model.add(Dense(1))
model.add(Activation('sigmoid'))

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd, class_mode="binary")

history = model.fit(X, y, nb_epoch=10000, batch_size=4, show_accuracy=True)

print
score = model.evaluate(X,y)
print
print score
print model.predict(np.array([[1, 0]]))
print model.predict(np.array([[0, 0]]))

# X vs y comparison
print
predictions = model.predict(X)
predictions = predictions.T
predictions = [1 if prediction >= 0.5 else 0 for prediction in predictions[0]]
print predictions
print [int(n) for n in y]

不幸的是,我是机器学习的初学者,我不知道为什么我的代码可以工作而你的不能。

I used this code.

在您的示例中,您有一个 Dense 层,其中包含 1 个具有 softmax 激活的单元。这样一个单元的值将始终为 1.0,因此没有信息可以从您的输入流向您的输出,网络也不会做任何事情。 Softmax 仅在需要对 n 类 中的概率进行预测时才真正有用,其中 n 大于 2.

其他答案建议更改代码以使其正常工作。只需删除 activation='softmax' 可能就足够了。

Keras 通常可以正常工作。