---
title: "Binary Decoder"
year: "2019"
motto: "Binary Decoder Adaline Network"
description: "Adaline network that decodes binary numbers."
stack: ["javascript"]
website: "https://lab.rolandoandrade.me/binary-decoder/"
url: "https://rolandoandrade.me/projects/binary-decoder/"
author: "Rolando Andrade"
---

# Binary Decoder

> Binary Decoder Adaline Network

Adaline network that decodes binary numbers.

Year: 2019. Stack: javascript. Website: https://lab.rolandoandrade.me/binary-decoder/

Repositories:

- [github](https://github.com/RolandoAndrade/binary-decoder)
- [gitlab](https://gitlab.com/RolandoAndrade/binary-decoder)

Turning binary into decimal is simple math. You multiply each bit by its power of two and add the results. Computers use binary, and people think in decimal. So I asked a different question: could a neural network learn this conversion on its own?

The question came up while I was studying artificial neural networks. I did not want to write the math into the code. I wanted to see if a single artificial neuron could find the pattern through training. The result is Binary Decoder. It is a web demo where you flip binary digits and watch the network give you the decimal number.

## Four bits, one answer

Binary Decoder takes a 4 bit binary input and returns the matching decimal number, from 0 to 15. The screen shows four buttons for bits 3, 2, 1 and 0. Click a button to flip its bit. The network computes and shows the result right away.

The interesting part starts when you press the "A" button. That starts the training. The network runs thousands of training rounds and adjusts its internal weights until it learns how binary maps to decimal. Weights are the numbers that decide how much each input counts.

## One neuron called Adaline

Adaline stands for Adaptive Linear Neuron. It is one of the earliest neural network designs, created by Bernard Widrow in the 1960s. The perceptron, an older design, learns from yes or no outputs. Adaline learns from continuous values instead. That makes learning smoother and more stable.

The network in this project is small:

- **4 input nodes**, one for each bit of the binary number
- **1 output node**, the predicted decimal value
- **4 weights**, the connections between the inputs and the output
- **1 bias**, one more number the network learns

The output is a weighted sum. The network multiplies each input by its weight, adds the bias and applies an activation function.

## Learning from its errors

The network learns with gradient descent. Gradient descent means taking small steps in the direction that makes the error smaller. For each training example, the network does four things:

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 sets the size of each step. If it is too high, the network jumps past the answer. If it is too low, training takes forever. I set it to 0.1, which works well for this problem.

The training loop runs 5000 rounds over all 16 possible binary inputs. By the end, the weights land close to 8, 4, 2 and 1. Those are exactly the powers of two behind binary place values.

## Sigmoid or ReLU

The project includes two activation functions, Sigmoid and ReLU. An activation function shapes the neuron's final output.

**Sigmoid** squeezes any input into a value between 0 and 1. It fits classification problems, where you want an output that looks like a probability. Its derivative is smooth, which helps gradient descent.

**ReLU** (Rectified Linear Unit) keeps positive values as they are and turns negative values into zero. It is simpler and trains faster for many problems. Binary Decoder uses ReLU because the output must be the real decimal number, not a probability.

## Vectors under the hood

Inside, the network works with vectors, which are lists of numbers. I wrote a basic vector class that supports three operations:

- **Dot product** multiplies matching elements and adds them up
- **Scalar multiplication** multiplies every element by one number
- **Subtraction** takes the difference element by element

These operations are the building blocks of neural network math. To update the weights, the network takes the old weights and subtracts the learning rate times the gradient.

## The network finds the rule

Binary Decoder is a toy example, but it shows a big idea. The network knows nothing about binary numbers or place values. It starts with random weights and finds the pattern by trial and error.

This is the base of all machine learning. You give a model examples, define what "correct" means, and let it find the rules. The same idea scales to image recognition, language translation and many other uses.

## Try it in the browser

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

Compare the output before and after training. At first, the network gives nonsense. After training, it converts any 4 bit binary number to the right decimal number.

[https://lab.rolandoandrade.me/binary-decoder/](https://lab.rolandoandrade.me/binary-decoder/)

## Math by hand first

Writing the math by hand, without frameworks, made me understand every step of a neural network. Why does gradient descent work? How do activation functions change learning? What happens when you change the learning rate?

Adaline is simple, which makes it a great place to start. Once you see how one neuron learns, networks with many layers stop looking like magic.

[Book a call with Rolando](https://rolandoandrade.me/pricing/)
