请告诉我我做错了什么,为什么准确性没有提高? 我尝试了所有事情,增加了层次,增加和减少了迭代次数,甚至尝试安装辍学(即使我没有在这里进行再培训),但是它没有成功:(
from __future__ import print_function
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.utils import np_utils
from keras.layers import Dropout
np.random.seed()
NB_EPOCH = 100
VERBOSE = 1
NB_CLASSES = 2
X_in = [[1,0],[1,1],[0,0],[0,1],[1,1],[0,0],[1,1]]
X_answer = [1,0,0,1,0,0,0]
X_in = np.asarray(X_in, dtype=np.float32)
X_answer = np.asarray(X_answer, dtype=np.float32)
X_answer = np_utils.to_categorical(X_answer, NB_CLASSES)
model = Sequential()
model.add(Dense(300, input_dim = 2, activation='relu'))
model.add(Dense(300, input_dim = 300, activation='softmax'))
model.add(Dense(2, input_dim = 300, activation='relu'))
#model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_in, X_answer, epochs=NB_EPOCH, verbose=VERBOSE)
答案 0 :(得分:2)
XOR运算只有4种可能的结果,我对您的来源进行了一些更改,因此现在可以正常使用,但是需要数百次迭代才能学习必要的知识:
#!/usr/bin/env python
import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.utils import np_utils
from keras.layers import Dropout
np.random.seed()
NB_EPOCH = 1000
VERBOSE = 1
NB_CLASSES = 2
X_in = [[1,0],[1,1],[0,0],[0,1]]
X_answer = [[0,1],[1,0],[1,0],[0,1]]
X_in = np.asarray(X_in, dtype=np.float32)
X_answer = np.asarray(X_answer, dtype=np.float32)
#X_answer = np_utils.to_categorical(X_answer, NB_CLASSES)
model = Sequential()
model.add(Dense(10, input_dim = 2, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(2, activation='softmax'))
#model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_in, X_answer, nb_epoch=NB_EPOCH, verbose=VERBOSE)
print model.predict( X_in )
结果是:
Epoch 995/1000
4/4 [==============================] - 0s - loss: 0.1393 - acc: 1.0000
Epoch 996/1000
4/4 [==============================] - 0s - loss: 0.1390 - acc: 1.0000
Epoch 997/1000
4/4 [==============================] - 0s - loss: 0.1387 - acc: 1.0000
Epoch 998/1000
4/4 [==============================] - 0s - loss: 0.1385 - acc: 1.0000
Epoch 999/1000
4/4 [==============================] - 0s - loss: 0.1383 - acc: 1.0000
Epoch 1000/1000
4/4 [==============================] - 0s - loss: 0.1380 - acc: 1.0000
[[ 0.00492113 0.9950788 ]
[ 0.99704748 0.0029525 ]
[ 0.99383503 0.00616499]
[ 0.00350395 0.99649602]]
实际上与所需的[0,1],[1,0],[1,0],[0,1]
(X_answer)
答案 1 :(得分:0)
这里有很多问题。不,这并非不可能(*)
退出与您的问题(**)无关。您使用的是softmax,然后是relu?我觉得这很奇怪。您为什么要强制分类?当您有这么小的输入示例(10个样本,但隐藏层上有300个参数)时,为什么要使用如此大的网络?
来自here,这是一个与keras进行xor的最小示例:
X = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([[0],[1],[1],[0]])
model = Sequential()
model.add(Dense(8, input_dim=2))
model.add(Activation('tanh'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
sgd = SGD(lr=0.1)
model.compile(loss='binary_crossentropy', optimizer=sgd)
model.fit(X, y, show_accuracy=True, batch_size=1, nb_epoch=1000)
print(model.predict_proba(X))
(*)您使用的是多层感知器,但标题吸引了我的注意,因为(相对)著名的证据表明,没有隐藏层的NN无法学习xor。 Proof example here
(**)当您拥有深层网络时,Dropout有助于普遍化。当训练大型模型以检测图像中的人类等复杂的高维结构时,这一点至关重要。尝试适应xor会让您的生活更加困难。