我想通過xor代碼來練習keras,但結果不對,後面是我的代碼,感謝大家幫助我。如何使用keras進行異或運算
from keras.models import Sequential
from keras.layers.core import Dense,Activation
from keras.optimizers import SGD
import numpy as np
model = Sequential()# two layers
model.add(Dense(input_dim=2,output_dim=4,init="glorot_uniform"))
model.add(Activation("sigmoid"))
model.add(Dense(input_dim=4,output_dim=1,init="glorot_uniform"))
model.add(Activation("sigmoid"))
sgd = SGD(l2=0.0,lr=0.05, decay=1e-6, momentum=0.11, nesterov=True)
model.compile(loss='mean_absolute_error', optimizer=sgd)
print "begin to train"
list1 = [1,1]
label1 = [0]
list2 = [1,0]
label2 = [1]
list3 = [0,0]
label3 = [0]
list4 = [0,1]
label4 = [1]
train_data = np.array((list1,list2,list3,list4)) #four samples for epoch = 1000
label = np.array((label1,label2,label3,label4))
model.fit(train_data,label,nb_epoch = 1000,batch_size = 4,verbose = 1,shuffle=True,show_accuracy = True)
list_test = [0,1]
test = np.array((list_test,list1))
classes = model.predict(test)
print classes
輸出
[[ 0.31851079] [ 0.34130159]] [[ 0.49635666] [0.51274764]]
「不正確」是什麼意思?你會得到什麼結果?你會期待什麼? – Daniel
謝謝,我想獲得列表classes = [a,b],一種方法爲1,b方法爲0,但事實是a,b就像一個隨機的,有兩個結果:[[0.31851079] [0.34130159]] [[0.49635666] [0.51274764]] –
請將此內容納入您的問題。這樣我們就不必重現你的練習以知道什麼是錯誤的...... – Daniel