question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get a simple XOR problem network to work, answer always array([0])

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:22 (6 by maintainers)

github_iconTop GitHub Comments

39reactions
logloglogcommented, May 11, 2015

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.

8reactions
dcgrigsbycommented, Nov 13, 2015

Here’s an XOR net that works with the new API:

import numpy as np
from keras.models import Sequential
from keras.layers.core import Activation, Dense
from keras.optimizers import SGD

X = np.zeros((4, 2), dtype='uint8')
y = np.zeros(4, dtype='uint8')

X[0] = [0, 0]
y[0] = 0
X[1] = [0, 1]
y[1] = 1
X[2] = [1, 0]
y[2] = 1
X[3] = [1, 1]
y[3] = 0

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, verbose=0)

print model.predict(X)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Basics of Deep Learning by solving XOR ...
In our X-OR problem, output is either 0 or 1 for each input sample. So, it is a two class or binary classification...
Read more >
My Neural Network Doesn't Work [XOR problem]
I'm trying to make a neural network for solving XOR problem.But I couldn't make it.Always giving false results.Maybe I'm making a mistake in ......
Read more >
Is it normal that a Neural Network sometimes doesn't learn Xor?
I've implemented a neural network and I'm training it to compute Xor. 1 out of x times it fails to learn, where x...
Read more >
How Neural Networks Solve the XOR Problem
The XOR functionAttempt #1: The Single Layer Perceptron ... in the case of a simple classifier, an output of say -2.5 or 8...
Read more >
#006 PyTorch - Solving the famous XOR problem using Linear ...
Highlights: One of the most historical problems in the Neural Network arena is the classic XOR problem where predicting the output of the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found