XOR gate
Neural networks
TensorFlow
Machine learning
Artificial intelligence

Problems implementing an XOR gate with Neural Nets in Tensorflow

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

The XOR problem is a classic neural-network lesson because it looks trivial and still breaks a single-layer perceptron. In TensorFlow, the usual issue is not that the framework cannot learn XOR, but that the model is too simple or the output and loss configuration do not match the task.

Why XOR Breaks a Single Linear Layer

XOR is not linearly separable. There is no single straight decision boundary that separates the positive cases (0, 1) and (1, 0) from the negative cases (0, 0) and (1, 1).

That means this kind of model is insufficient:

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

It can only learn a linear boundary followed by a sigmoid. XOR needs at least one hidden layer with a non-linear activation.

A Minimal Working TensorFlow Example

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="float32")
10
11y = np.array([[0.0], [1.0], [1.0], [0.0]], dtype="float32")
12
13model = tf.keras.Sequential([
14    tf.keras.layers.Input(shape=(2,)),
15    tf.keras.layers.Dense(4, activation="tanh"),
16    tf.keras.layers.Dense(1, activation="sigmoid"),
17])
18
19model.compile(
20    optimizer=tf.keras.optimizers.Adam(learning_rate=0.05),
21    loss="binary_crossentropy",
22    metrics=["accuracy"],
23)
24
25model.fit(x, y, epochs=500, verbose=0)
26print(model.predict(x, verbose=0))

This small network is enough to learn XOR reliably in most runs.

The Usual Failure Modes

1. No Hidden Layer

This is the most common issue. Without a hidden non-linear transformation, the model cannot represent XOR.

2. Wrong Output or Loss Pairing

If the output layer uses sigmoid, binary_crossentropy is the natural loss for this binary task. If you switch to logits without adjusting the loss or thresholding logic, training becomes confusing quickly.

3. Training Too Briefly

XOR uses only four samples, but the network still needs enough updates to settle into a good solution. If training stops after only a few epochs, the output may look random and make it seem like the architecture is broken.

4. Bad Initialization or Learning Rate

With a tiny dataset, optimization can be sensitive. An extremely small learning rate can make training look stalled. An overly large one can bounce around the solution.

Why This Example Still Matters

XOR is not about solving a practical business problem. It is a sanity check for understanding representation power:

  • linear models solve linearly separable tasks
  • hidden non-linear layers expand what the model can represent
  • correct loss and activation choices matter

That is why XOR remains a standard teaching example even though modern deep learning systems solve far larger problems.

Inspecting the Learned Behavior

After training, predictions should be close to:

text
0, 1, 1, 0

They may not be exact integers because the model outputs probabilities. A result like:

text
[[0.01], [0.98], [0.98], [0.02]]

is a success.

Common Pitfalls

The biggest mistake is assuming TensorFlow itself is the problem when a single dense layer fails. The limitation is mathematical, not library-specific.

Another mistake is using a hidden layer but forgetting a non-linear activation. Two purely linear layers composed together are still just a linear model.

A third issue is judging success by raw probabilities without applying a threshold. For binary classification, predictions near 0 and 1 are what matter.

Summary

  • XOR cannot be solved by a single linear layer.
  • Use at least one hidden layer with a non-linear activation.
  • Match the output layer and loss function correctly for binary classification.
  • Give the model enough epochs to converge on the tiny dataset.
  • If XOR fails, check architecture and loss setup before blaming TensorFlow.

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.