How to use keras for XOR

前端 未结 3 904
终归单人心
终归单人心 2021-01-05 15:57

I want to practice keras by code a xor, but the result is not right, the followed is my code, thanks for everybody to help me.

from keras.models import Seque         


        
3条回答
  •  耶瑟儿~
    2021-01-05 16:34

    If I increase the number of epochs in your code to 50000 it does often converge to the right answer for me, just takes a little while :)

    It does often get stuck, though. I get better convergence properties if I change your loss function to 'mean_squared_error', which is a smoother function.

    I get still faster convergence if I use the Adam or RMSProp optimizers. My final compile line, which works:

    model.compile(loss='mse', optimizer='adam')
    ...
    model.fit(train_data, label, nb_epoch = 10000,batch_size = 4,verbose = 1,shuffle=True,show_accuracy = True)
    

提交回复
热议问题