NeCNN
My bachelor's thesis: can evolution design the classifier of a neural network?
Overview
Convolutional neural networks recognise images in two steps: convolutional layers extract features, and a classifier at the end decides what is in the picture. In my bachelor’s thesis I let evolution build that classifier.
Question
Usually a person decides how the classifier is built: how many layers, how many neurons. Can evolution do that instead — and how close does it get?
NEAT (NeuroEvolution of Augmenting Topologies) starts with a few connections and lets networks grow: mutations add neurons and connections, crossover combines two parents, and only the fittest candidates pass on their genes.
Approach
The feature extractor is a pretrained network and stays frozen. Only the classifier on top evolves: NEAT decides its structure, PyTorch its weights.
Each generation has 50 candidates. Every candidate is turned into a PyTorch network, trained for two epochs with gradient descent and judged by its loss: fitness = 1 / (1 + loss). After 50 generations, the best one is tested on images it has never seen. Below is a real run — CIFAR-10 with ResNet-18 — generation by generation.
What you see
One run from the thesis. Every generation, 50 networks are trained and scored; the picture shows the best one. On the left are the 512 features the frozen ResNet-18 extracts from an image, on the right the ten classes. Everything in between was built by evolution.
Generation 50 — Growth
The network keeps growing, to 135 neurons and 1,163 connections — yet its fitness only rises from 0.988 to 0.991. A fully connected layer would have 5,120 connections.
- Hidden neurons
- 135
- Connections
- 1,163
- fully connected: 5,120
- Fitness
- 0.991
- 1 = perfect on the training images
Best networkAverage of all 50Fitness = 1 / (1 + loss)
Implementation
NEAT describes a network as a list of genes — nodes and connections, wired however evolution likes. PyTorch expects layers.
A converter sorts the nodes of a genome by depth into layers and turns each layer into a linear layer with a mask, so only the connections that exist in the genome are trained. Because the feature extractor never changes, its outputs are computed once per dataset and stored; each candidate only trains its own small classifier. I also changed NEAT’s crossover: a child can inherit connections from both parents, not only from the fitter one, and takes along just the nodes those connections need.
Result
The evolved classifiers came close to the original ones but did not beat them — with far fewer connections.
On MNIST and CIFAR-10 they stayed within half a percentage point, on smallNORB within one and a half; on Imagenette they fell three to five points short. The classifier evolved for CIFAR-10 on ResNet-18 uses 1,163 connections — a fully connected layer has 5,120 — and reaches 92.61% instead of 92.62%.
| Dataset | Network | Original | Evolved |
|---|---|---|---|
| MNIST | ResNet-18 | 99.16% | 98.80% |
| MNIST | ResNet-50 | 99.09% | 98.68% |
| CIFAR-10 | ResNet-18 | 92.62% | 92.61% |
| CIFAR-10 | ResNet-50 | 93.43% | 93.40% |
| Imagenette | ResNet-18 | 97.71% | 92.31% – 93.53% |
| Imagenette | ResNet-50 | 98.96% | 96.05% – 96.08% |
| smallNORB | ResNet-18 | 87.83% | 86.99% |
| smallNORB | ResNet-50 | 88.92% | 87.61% |
Test accuracy from the logs in the repository: one run per configuration, two for Imagenette. The network is the frozen feature extractor; the original classifier is its single linear layer.