Context
This was a project for a data science class, where we could choose from several projects. I picked this one for two reasons.
First, I wanted to understand convolution. 3Blue1Brown's video on convolution was what first got me interested in it.
Second, I was fascinated by neural networks and how they are now used almost everywhere, and I wanted a real grasp of how they work. Sebastian Lague's video helped me understand neural networks, but it didn't cover convolutional ones. Building one myself seemed like the best way to learn.
It was considered the most demanding project of the class, but since it brought together two things I was already curious about, it felt worth taking on.
The question
Is this galaxy round or elongated? From a small 31 × 31 pixel image, the network has to decide which of the two shapes it is looking at.
What I built
Everything is written from scratch in Python and NumPy, without machine-learning libraries:
- the convolution and the zero-padding around the image
- activation functions (ReLU, sigmoid and others) and their derivatives
- loss functions to measure how wrong a prediction is
- backpropagation and the weight updates
- a model class where I choose the number of layers, the kernel sizes and the padding
- the training loop
Backpropagation
The part that took me the longest to understand was backpropagation: not just the steps, but why each one is needed. I worked through the derivation by hand. To send the error back through a layer, you convolve it with the same kernel rotated by 180°.
Inside the trained network
Before the galaxies, I checked that everything worked on a simpler task: turning each image into its negative. Then I trained networks with one, two and three layers to classify the galaxies, and looked at what they had learned.