Binary numbers are everywhere in computing, but humans think in decimal. Converting between the two is straightforward math: multiply each bit by its power of two and sum them up. But what if a neural network could learn this conversion on its own?

This question sparked a small experiment during my studies of artificial neural networks. Instead of hardcoding the math, I wanted to see if a single artificial neuron could discover the pattern through training. The result is Binary Decoder, a web-based demo where you toggle binary digits and watch the network produce the decimal equivalent.

What It Does

Binary Decoder takes a 4-bit binary input and outputs the corresponding decimal number (0-15). The interface shows four buttons representing bits 3, 2, 1, and 0. Click a button to flip its state. The network immediately computes and displays the result.

The magic happens when you press the “A” button. This triggers the training process. The network runs through thousands of training iterations, adjusting its internal weights until it learns the binary-to-decimal mapping.

The Adaline Architecture

Adaline stands for Adaptive Linear Neuron. It is one of the earliest neural network architectures, developed by Bernard Widrow in the 1960s. Unlike the perceptron, Adaline uses continuous values during training rather than binary outputs. This makes the learning process smoother and more stable.

The network in this project has a simple structure:

  • 4 input nodes - one for each bit of the binary number
  • 1 output node - the predicted decimal value
  • 4 weights - connections between inputs and output
  • 1 bias - an additional learnable parameter

The output is calculated as a weighted sum: multiply each input by its corresponding weight, add the bias, and apply an activation function.

Training with Gradient Descent

The network learns through a process called gradient descent. For each training example, it:

  1. Computes the predicted output
  2. Calculates the error (difference from the expected value)
  3. Computes how much each weight contributed to the error
  4. Adjusts weights in the direction that reduces error

The learning rate controls how big each adjustment is. Too high and the network overshoots. Too low and training takes forever. I set it to 0.1, which works well for this problem.

The training loop runs 5000 iterations over all 16 possible binary inputs. By the end, the network converges to weights close to 8, 4, 2, and 1 - exactly the powers of two that define binary place values.

Activation Functions

The project includes two activation functions: Sigmoid and ReLU.

Sigmoid squashes any input to a value between 0 and 1. It works well for classification problems where you want probability-like outputs. Its derivative is smooth, which helps gradient descent.

ReLU (Rectified Linear Unit) passes positive values unchanged and clamps negative values to zero. It is simpler and trains faster for many problems. Binary Decoder uses ReLU because we want the output to be the actual decimal number, not a probability.

The Vector Math

Under the hood, the network performs vector operations. I implemented a basic vector class that supports:

  • Dot product - multiply corresponding elements and sum
  • Scalar multiplication - multiply all elements by a constant
  • Subtraction - element-wise difference

These operations are the building blocks of neural network computation. The weight update formula is: new weights = old weights - learning rate * gradient.

Why This Matters

Binary Decoder is a toy example, but it demonstrates a powerful concept. The network does not know anything about binary numbers or place values. It starts with random weights and discovers the pattern through trial and error.

This is the foundation of all machine learning. Give a model examples, define what “correct” means, and let it figure out the rules. The same principle scales to image recognition, language translation, and countless other applications.

Running the Demo

The entire project runs in the browser with no dependencies. Open the HTML file and start clicking. The visualization updates in real time as you toggle bits and train the network.

Watch the output before and after training. Initially, the network produces nonsense. After training, it reliably converts any 4-bit binary number to its decimal equivalent.

Decodificador binario

rolandoandrade.github.io

What I Learned

Building this project taught me neural networks from the ground up. Implementing the math manually, without frameworks, forced me to understand every step. Why does gradient descent work? How do activation functions affect learning? What happens when you change the learning rate?

The simplicity of Adaline makes it a great starting point. Once you grasp how a single neuron learns, multi-layer networks become much less mysterious.

Back to Projects