Published in


Let’s code a Neural Network from scratch — Part 1

Part 1, Part 2 & Part 3

NMIST data set — Picture from Adam Geitgey

How does our brain function?

Let’s first explore where ANN originates — our brains. At the sight of a speed limit sign “70” the photons bouncing from your headlights, to the panel and back into your retina produces a chemical reaction. Some of the neurons connected to your visual cortex would get excited and pass the signal to other neurons through a networks of dendrites which releases a chemical signal.

Neuron Signal @ GIF from DynamicScience
Handwritten “7”

Let’s write some code

With the basics out of the way let’s structure our program, you can replicate this in Processing by opening a tab for each of the following segment.

  1. Main tab: This will allow us to draw, ‘train’ and ‘test’ our neural network.
  2. Load data: This tab will allow us to load a set of labelled ‘flash cards’. Each flash card will have a handwritten number on it for the neural network to recognise, and a label on the back to tell us which number it actually is.
  3. Neuron: This will be the code for an individual perceptron as draw above.
  4. Network: This will consist of a number of arrays of neurons. One for the input layer, one for the hidden layer, and one for the output layer.
  5. Sigmoid: The purpose of this tab is to construct a ‘how to fire a neuron’ function. More detail on this in part 2.
  • Network: This is the initialiser, in here we setup a network with a given number of inputs, in this case this will be 196 (one for each pixel).
  • Respond: In here we respond to a card it gets shown, for now we’ll just copy the input of the card to the output of the card with no calculations.
  • Display: This is used to draw our 196 neurons as a 14x14 grid



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store