6

我正在尝试为 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 数、学习率和其他参数。但是从第一个时期到最后一个时期,误差保持不变。

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 是否有效。

4

3 回答 3

5

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

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

Keras 通常确实有效。

于 2016-03-31T10:17:23.560 回答
0

在没有激活函数的情况下尝试网络中的最后一个感知器。我遇到了同样的问题,当您删除激活功能时它开始学习。

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

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

于 2016-01-11T10:19:06.653 回答
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]

不幸的是,我是机器学习的初学者,我不知道为什么我的代码有效而您的代码无效。

我使用了这段代码。

于 2016-02-24T23:58:27.220 回答