无法训练神经网络解决XOR映射 [英] Cannot train a neural network solving XOR mapping

查看:76
本文介绍了无法训练神经网络解决XOR映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement a simple classifier for the XOR problem in Keras. Here is the code:

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]])))

我尝试更改时期数,学习率和其他参数.但是错误从第一个时期到最后一个时期都保持不变.

I tried changing the number of epochs, learning rate, and other parameters. But the error remains constant from the first to the last epoch.

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中训练此网络?

How do you train this network in Keras?

还有没有更好的库来实现神经网络?我尝试了PyBrain,但已被放弃,尝试了scikit-neuralnetwork,但是文档确实是很隐秘的,因此无法弄清楚如何对其进行培训.而且我严重怀疑Keras是否可以正常工作.

Also, is there a better library for implementing neural networks? I tried PyBrain, but it has been abandoned, tried scikit-neuralnetwork but the documentation is really cryptic so couldn't figure out how to train it. And I seriously doubt if Keras even works.

推荐答案

在您的示例中,您具有一个带有1个单位的Dense层,并带有softmax激活.这样一个单位的值将始终为1.0,因此没有信息可以从输入流到输出,并且网络将不执行任何操作.只有在需要生成n个大于2的n个类中的概率预测时,Softmax才真正有用.

In your example, you have a Dense layer with 1 unit with a softmax activation. The value of such a unit will always be 1.0, so no information can flow from your inputs to your outputs, and the network won't do anything. Softmax is only really useful when you need to generate a prediction of a probability among n classes, where n is greater than 2.

其他答案建议对代码进行更改以使其起作用.只需删除activation='softmax'就足够了.

The other answers suggest changes to the code to make it work. Just removing activation='softmax' may be enough.

Keras通常可以正常工作.

Keras does generally work.

这篇关于无法训练神经网络解决XOR映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆