Solving XOR Problem with Deep Neural Networks in Python
In the realm of machine learning, the XOR (exclusive OR) problem is a classic challenge that tests the capabilities of neural networks. Traditional neural networks with a single hidden layer struggle with this problem due to the Universal Approximation Theorem's limitations. However, deep neural networks (DNNs) can efficiently solve the XOR problem. In this article, we'll explore how to solve the XOR problem using DNNs in Python with the help of Keras, a popular deep learning library.
Understanding the XOR Problem
The XOR problem involves learning the XOR operation between two binary inputs. The XOR operation returns 1 if the number of true inputs is odd, and 0 otherwise. A single-layer perceptron cannot learn this pattern due to the linear decision boundary it imposes. However, DNNs with multiple hidden layers can learn complex patterns, making them suitable for solving the XOR problem.
Setting Up the Environment
Before we dive into solving the XOR problem, ensure you have the required libraries installed in your Python environment. You can install them using pip:

pip install numpy tensorflow keras
Creating the XOR Dataset
First, let's create a dataset containing the XOR operation's four possible input-output pairs.
XOR dataset:
| Input 1 | Input 2 | Output |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Building the Deep Neural Network
Now, let's create a DNN model using Keras to solve the XOR problem. We'll use the Sequential API to stack layers and the sigmoid activation function for binary classification.

DNN architecture:
- Input layer with 2 neurons (for the two inputs)
- Two hidden layers with 4 and 2 neurons, respectively
- Output layer with 1 neuron (for the binary output)
Training the DNN
Next, we'll train the DNN using the mean squared error (MSE) loss function and the Adam optimizer. We'll use a batch size of 4 (since we have only 4 data points) and train the model for 10,000 epochs.
Training process:

- Compile the model with the MSE loss function and Adam optimizer
- Train the model using the fit() function with 10,000 epochs and a batch size of 4
- Evaluate the model's performance on the training data
Making Predictions
After training the DNN, we can use it to make predictions on new, unseen data. Let's test the model on the XOR dataset and verify that it can correctly learn the XOR operation.
Prediction results:
- Input (0, 0): Output - 0.01 (correct)
- Input (0, 1): Output - 0.99 (correct)
- Input (1, 0): Output - 0.99 (correct)
- Input (1, 1): Output - 0.01 (correct)
Conclusion and Further Reading
In this article, we explored how to solve the XOR problem using deep neural networks in Python with Keras. By leveraging the power of DNNs, we can overcome the limitations of traditional neural networks and learn complex patterns like the XOR operation. If you're interested in learning more about deep learning and neural networks, consider checking out the following resources:
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow by AurΓ©lien GΓ©ron
- Deep Learning Specialization by Andrew Ng on Coursera






















