machine learning
neural networks
tensorflow
tflearn
xor problem

tflearn / tensorflow does not learn xor

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

If a TensorFlow or TFLearn model does not learn XOR, the usual cause is not that TensorFlow is broken. The usual cause is that the network architecture is too simple, the activation choice is inappropriate, or training settings are too weak for a non-linearly separable problem.

Why XOR Is Special

XOR is the classic example of a function that a single linear layer cannot represent. The outputs are:

  • '0, 0 goes to 0'
  • '0, 1 goes to 1'
  • '1, 0 goes to 1'
  • '1, 1 goes to 0'

Those four points cannot be separated with one straight decision boundary. That is why a single-layer perceptron fails.

If your model has no hidden layer or uses only linear transformations, it should fail. That is expected.

A Minimal TensorFlow Model That Can Learn XOR

The simplest fix is to add a small hidden layer with a non-linear activation.

python
1import numpy as np
2import tensorflow as tf
3
4X = np.array([
5    [0.0, 0.0],
6    [0.0, 1.0],
7    [1.0, 0.0],
8    [1.0, 1.0],
9], dtype=np.float32)
10
11y = np.array([[0.0], [1.0], [1.0], [0.0]], dtype=np.float32)
12
13model = tf.keras.Sequential([
14    tf.keras.layers.Dense(4, activation="tanh", input_shape=(2,)),
15    tf.keras.layers.Dense(1, activation="sigmoid"),
16])
17
18model.compile(
19    optimizer=tf.keras.optimizers.Adam(learning_rate=0.05),
20    loss="binary_crossentropy",
21    metrics=["accuracy"],
22)
23
24model.fit(X, y, epochs=500, verbose=0)
25print(model.predict(X))

This works because the hidden layer introduces non-linearity, allowing the model to separate the XOR pattern in transformed feature space.

Why Simple Models Fail

A model like this is not enough:

python
bad_model = tf.keras.Sequential([
    tf.keras.layers.Dense(1, activation="sigmoid", input_shape=(2,))
])

That is still just logistic regression over the two inputs. It can learn linearly separable problems, but XOR is not one of them.

In older TFLearn code, the same underlying issue appears if the network is too shallow or if activations are effectively linear.

Training Settings Also Matter

Even with a correct architecture, XOR can still look like it is “not learning” if training is weak.

Common causes include:

  • too few epochs
  • a poor learning rate
  • bad activation choices
  • random initialization that needs more training
  • using mean squared error when binary cross-entropy is a better fit

For such a tiny dataset, a model may need surprisingly many epochs relative to the amount of data, simply because there are only four training examples.

Interpreting TFLearn Failures

Older TFLearn examples often fail for one of these reasons:

  • hidden layer too small or missing
  • linear activation in the wrong place
  • training loop too short
  • outdated defaults or code patterns

The core lesson is architectural, not library-specific. Whether the code uses TFLearn or tf.keras, XOR needs at least one non-linear hidden layer.

Sanity-Check the Data and Output Shapes

For a small toy problem, it is worth printing the data and predicted values directly instead of relying only on loss curves.

python
pred = model.predict(X)
for inputs, output, expected in zip(X, pred, y):
    print(inputs, float(output[0]), float(expected[0]))

This makes it obvious whether the model is actually separating the four cases or just drifting around 0.5.

Common Pitfalls

A common mistake is trying to solve XOR with one layer and expecting optimization to magically discover a non-linear boundary.

Another mistake is using a hidden layer but training for too few epochs and concluding the library cannot learn the problem.

It is also easy to mix loss functions and activations poorly. For binary XOR output, a sigmoid output plus binary cross-entropy is a sensible default.

Finally, do not overread a toy problem. XOR is useful for understanding representational limits, but success on XOR does not say much about performance on real-world datasets.

Summary

  • XOR is not linearly separable, so a single linear layer cannot learn it.
  • Add at least one hidden layer with a non-linear activation such as tanh or relu.
  • Use sensible training settings such as enough epochs and binary cross-entropy.
  • TFLearn and TensorFlow failures on XOR are usually architecture or training issues, not framework bugs.
  • Print predictions directly on the four XOR inputs to verify what the model actually learned.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.