← All tutorials

04 / DEEP LEARNING

Teach a network
the XOR problem.

XOR is a tiny classification problem that a single linear decision boundary cannot solve. Build a small neural network with a hidden layer, train it, and compare CoolBox implementations in Python, Go, Rust, and C++.

10 MINPYTHON · GO · RUST · C++INTERMEDIATE

Before you begin

Follow the setup guide for the language you choose: Python, Go, Rust, or C++. CoolBox describes this deep-learning library as an educational implementation for simple neural-network applications; it is not a GPU training framework.

1. Make the XOR examples

Each input has two binary features. XOR returns 1 when exactly one feature is 1, and 0 when both values match. The documented API represents each sample as a Tensor with shape [1, features].

XOR DATASET
from ml_core import deep_learning as dl

inputs = [
    dl.Tensor([1, 2], [0.0, 0.0]),
    dl.Tensor([1, 2], [0.0, 1.0]),
    dl.Tensor([1, 2], [1.0, 0.0]),
    dl.Tensor([1, 2], [1.0, 1.0]),
]

targets = [
    dl.Tensor([1, 1], [0.0]),
    dl.Tensor([1, 1], [1.0]),
    dl.Tensor([1, 1], [1.0]),
    dl.Tensor([1, 1], [0.0]),
]

2. Build the network

Connect a dense hidden layer and ReLU activation to a one-unit output layer with a sigmoid activation. Use mean squared error as the loss, matching the repository’s Python example.

NETWORK MODEL
from ml_core import deep_learning as dl

network = dl.NeuralNetwork()
network.add_layer(dl.DenseLayer(2, 4))
network.add_layer(dl.ReLULayer())
network.add_layer(dl.DenseLayer(4, 1))
network.add_layer(dl.SigmoidLayer())
network.set_loss(dl.MSELoss())

3. Train and predict

Train on the four samples, then pass one input into predict. The predicted value should move toward the corresponding target as training converges; exact results can vary with initialization.

Choose a language to see a complete CoolBox-backed XOR example. The API is available in all four languages shown here; use the matching installation guide before compiling or running it.

PYTHON · COOLBOX BINDING
from ml_core import deep_learning as dl

inputs = [
    dl.Tensor([1, 2], [0.0, 0.0]),
    dl.Tensor([1, 2], [0.0, 1.0]),
    dl.Tensor([1, 2], [1.0, 0.0]),
    dl.Tensor([1, 2], [1.0, 1.0]),
]
targets = [
    dl.Tensor([1, 1], [0.0]),
    dl.Tensor([1, 1], [1.0]),
    dl.Tensor([1, 1], [1.0]),
    dl.Tensor([1, 1], [0.0]),
]

network = dl.NeuralNetwork()
network.add_layer(dl.DenseLayer(2, 4))
network.add_layer(dl.ReLULayer())
network.add_layer(dl.DenseLayer(4, 1))
network.add_layer(dl.SigmoidLayer())
network.set_loss(dl.MSELoss())
network.train(inputs, targets, epochs=1000, batch_size=4)

prediction = network.predict(inputs[1])
print("Prediction for [0, 1]:", prediction.data()[0])
Diagram of a two-input, four-unit ReLU hidden layer, and one-output sigmoid neural network, with the XOR truth table.
Network architecture and XOR target values. This is a structural diagram, not a screenshot of a training run; predictions vary with initialization and convergence.

What to try next

Try the documented Adam optimizer or the multiclass example linked in the deep-learning guide . For the full feature list and current limitations, consult that guide before adapting it to a larger problem.