keras: Can't get a simple XOR problem network to work, answer always array([0])
I am trying to implement a XOR-problem solving network and can’t seem to get it to work. Here’s my code:
model = Sequential()
model.add(Dense(2,2))
model.add(Activation('sigmoid'))
model.add(Dense(2,1))
model.add(Activation('softmax'))
X = numpy.array([[0,0],[0,1],[1,0],[1,1]])
y = numpy.array([[0],[1],[1],[0]])
model.compile(loss='categorical_crossentropy', optimizer='sgd')
model.fit(X, y, nb_epoch=5, batch_size=32)
No matter what input I try, the answer to predict_classes
is always array([0])
, with predict_proba
result always being array([[ 1.]])
.
I have tried other setups, with tanh activation, loss='mean_absolute_error'
, optimizer='rmsprop'
, nb_epoch=20, batch_size=16
, but there was no difference.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 22 (6 by maintainers)
Commits related to this issue
- Added PReLU activation layer (#112) * added prelu * added test * updated layer tests * update docstring * add kwargs — committed to hubingallin/keras by divyashreepathihalli a year ago
- Added PReLU activation layer (#112) * added prelu * added test * updated layer tests * update docstring * add kwargs — committed to kernel-loophole/keras by divyashreepathihalli a year ago
If I weren’t interested in learning how to use Keras, I wouldn’t have raised this issue. If you don’t have the time to help, then you shouldn’t spend it writing a passive-aggressive retort either.
Here’s an XOR net that works with the new API:
I don’t have the time to get your code working, but in any case solving XOR with Keras sounds like using a Merlin rocket engine to make a grilled cheese sandwich.
If you are genuinely interested in learning how to use Keras, I recommend you check out the examples page, the examples folder (6 scripts solving real problems), and the intro “30 seconds to Keras”.
If you are genuinely seeking a XOR operator, I recommend you use the
^
Python operator.XOR example:
Don’t throw XOR to the dust bin so fast. It is still a great example for certain instances. I use XOR as an example in a class to show my students how I can extract the weights from Keras and calculate the XOR output on the classroom whiteboard. I do not have a big enough whiteboard for MINST!
I have several examples of XOR and how to extract/calculate the weights on a very small (2-hidden layer network).
https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class03_tensor_flow.ipynb
This worked for me