OverviewCase study 03
(03)Case studyBachelor's thesis — 2022

NeCNN

My bachelor's thesis: can evolution design the classifier of a neural network?

Client
Bachelor's thesis, University of Innsbruck
Role
Research & code
Year
2022
Stack
Python, PyTorch, neat-python
Fig. 01 — The feature extractor stays frozen, the classifier evolves.2022
01

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.

02

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.

03

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)

CIFAR-10 with a frozen ResNet-18, 50 networks per generation, 50 generations. Data: the NEAT statistics of this run in the repository.
04

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.

Fig. 02 — Fitness as a landscape: generation by generation, the population climbs higher.
Fig. 03 — No two candidates are wired alike; each one is trained on its own.
05

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%.

DatasetNetworkOriginalEvolved
MNISTResNet-1899.16%98.80%
MNISTResNet-5099.09%98.68%
CIFAR-10ResNet-1892.62%92.61%
CIFAR-10ResNet-5093.43%93.40%
ImagenetteResNet-1897.71%92.31% – 93.53%
ImagenetteResNet-5098.96%96.05% – 96.08%
smallNORBResNet-1887.83%86.99%
smallNORBResNet-5088.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.

Four datasetsMNIST, CIFAR-10, Imagenette and smallNORB, each with ResNet-18 and ResNet-50.
2,500 networks per run50 generations of 50 candidates, each one trained with PyTorch.
Open sourceCode, configurations and logs of every run are on GitHub.
Next projectThis website
Form / Function

Personal website — 2026