Building a Tiny CIFAR-10 CNN in C: Convolution, Pooling, and Backpropagation
Building a Tiny CIFAR-10 CNN in C: Convolution, Pooling, and Backpropagation

Building a Tiny CIFAR-10 CNN in C: Convolution, Pooling, and Backpropagation

After the handwritten digit softmax classifier, the next practical step is a convolutional neural network. This project does not use PyTorch, TensorFlow, or OpenCV. Instead, one C file reads CIFAR-10 binary data, runs convolution, ReLU, max pooling, a fully connected layer, softmax, and manual backpropagation.

This article is based on the local cifar10_tiny_cnn.c project. The goal is not to reach state-of-the-art CIFAR-10 accuracy. The goal is to make each part of a CNN visible as code you can inspect, compile, and modify.

1. What The CIFAR-10 Input Looks Like

Each CIFAR-10 image is a 32 x 32 color image with red, green, and blue channels. The input shape in the program is therefore 3 x 32 x 32. Each sample has one label from 10 classes.

The source fixes that shape with a few constants:

#define IMG_C 3
#define IMG_H 32
#define IMG_W 32
#define NCLASS 10

This is different from the handwritten digit softmax project. The digit project flattens a grayscale image into one vector. The CNN keeps the spatial layout first, so convolution filters can scan local regions and detect color, edge, and texture patterns.

2. The Tiny CNN Architecture

The current code uses 16 filters of size 3 x 3. Because the convolution is valid convolution, a 32 x 32 input becomes 30 x 30 after convolution. Then 2 x 2 max pooling reduces it to 15 x 15.

#define NF 16
#define K 3
#define CONV_H (IMG_H - K + 1)
#define CONV_W (IMG_W - K + 1)
#define POOL_H (CONV_H / 2)
#define POOL_W (CONV_W / 2)
#define FEAT (NF * POOL_H * POOL_W)

The full path is:

  • Input: 3 x 32 x 32
  • Convolution: 16 filters, 3 x 3, output 16 x 30 x 30
  • ReLU: clips negative activations to 0
  • Max pooling: 2 x 2, output 16 x 15 x 15
  • Fully connected layer: maps pooled features to 10 logits
  • Softmax: converts logits into class probabilities

3. Convolution Written Directly In C

The convolution layer is nested loops. Each filter scans every output location, then multiplies and accumulates over the 3 input channels and the 3 x 3 local window.

float sum = net->conv_b[f];
for (int c = 0; c < IMG_C; c++) {
    for (int r = 0; r < K; r++) {
        for (int t = 0; t < K; t++) {
            sum += net->conv_w[f][c][r][t] *
                   get_pixel(s->x, c, i + r, j + t);
        }
    }
}
conv[f][i][j] = sum > 0.0f ? sum : 0.0f;

The last line also applies ReLU. This plain implementation is useful because it exposes the relationship between filters, channels, local windows, and output positions.

4. Max Pooling Keeps The Strongest Local Response

The pooling layer compresses every 2 x 2 window into one value by keeping the maximum activation. The code also records where the maximum came from, because backpropagation only sends the gradient back to that winning position.

float best = conv[f][base_i][base_j];
int best_idx = 0;
for (int di = 0; di < 2; di++) {
    for (int dj = 0; dj < 2; dj++) {
        float v = conv[f][base_i + di][base_j + dj];
        if (v > best) {
            best = v;
            best_idx = di * 2 + dj;
        }
    }
}

This reduces the feature map size and gives the model a small amount of local translation tolerance.

5. Fully Connected Layer And Softmax

The pooled features are flattened into one vector and passed into a fully connected layer. Each class has its own weights, producing 10 logits.

for (int k = 0; k < NCLASS; k++) {
    float z = net->fc_b[k];
    for (int p = 0; p < FEAT; p++) {
        z += net->fc_w[k][p] * feat[p];
    }
    logits[k] = z;
}

Softmax normalizes these raw scores into probabilities. Training uses cross-entropy loss, and prediction selects the class with the highest probability.

6. What Backpropagation Updates

This program does not hide backpropagation inside a framework. It explicitly does four things:

  • subtracts the true label from the softmax probabilities to get output gradients
  • updates fully connected weights and biases
  • sends gradients back through max pooling and ReLU
  • updates every convolution filter from the matching input window
net->fc_w[k][p] -= LR * dlogits[k] * feat[p];
net->fc_b[k] -= LR * dlogits[k];
...
net->conv_w[f][c][r][t] -= LR * dw;
net->conv_b[f] -= LR * db;

This is the most valuable part of the project. CNN training is not magic: each layer computes its gradients and moves its parameters in a direction that lowers the loss.

7. Compile And Run Locally

The site publishes the source file, sample weights, sample predictions, and explanation notes. It does not publish the full CIFAR-10 dataset. Download the CIFAR-10 binary version from the official source, extract cifar-10-batches-bin, and run the program against that folder.

gcc -O2 -std=c11 cifar10_tiny_cnn.c -lm -o cifar10_tiny_cnn
./cifar10_tiny_cnn ./cifar-10-batches-bin 1 2000 1000

The four command-line arguments are:

  • data directory
  • number of epochs
  • training sample limit
  • test sample limit

Start with a small sample limit first. After the full flow works, increase the training sample count and epoch count gradually.

8. Reading The Output Files

The program writes two output files after a run:

  • model_weights.bin: saved model parameters from one local run
  • test_predictions.csv: prediction output for checking format and class distribution

The resource library only hosts small companion materials. The full training data stays with the official CIFAR-10 source instead of being duplicated on this site.

9. Project Boundaries

This tiny CNN is an educational implementation, not a production image classifier. Its limits are clear:

  • pure C single-sample training is much slower than modern deep learning frameworks
  • there is no mini-batching, data augmentation, regularization, or learning-rate schedule
  • the network is intentionally small, so accuracy will not match modern CIFAR-10 models
  • the main goal is to understand CNN forward propagation and backpropagation

If you have already read the handwritten digit softmax project, this article is the next step: moving from a linear classifier into an image model with local receptive fields.

10. Companion Resources

The resource library includes the C source, sample model weights, sample predictions, and CNN explanation PDF. Download the full CIFAR-10 dataset from its official source when running the project locally.

Search questions

FAQ

Who is this article for?

This article is for readers who want an intermediate-level guide to Building a Tiny CIFAR-10 CNN in C: Convolution, Pooling, and Backpropagation. It takes about 12 min and focuses on C, CNN, CIFAR-10, Backpropagation.

What should I read next?

The recommended next step is High-Entropy Traffic Defense Notes, so the article connects into a longer learning route instead of ending as an isolated note.

Does this article include runnable code or companion resources?

Yes. Use the run notes, resource cards, and download links on the page to reproduce the example or inspect the companion files.

How does this article fit into the larger site?

It is connected to the article context block, learning routes, resources, and project timeline so readers can move from concept to implementation.

Article context

AI Learning Project

A practical route from AI concepts to machine learning workflow, evaluation, neural networks, Python practice, handwritten digits, a CIFAR-10 CNN, adversarial traffic-defense notes, and AI security.

Level: Intermediate Reading time: 12 min
  • C
  • CNN
  • CIFAR-10
  • Backpropagation
Other language version 用 C 从零实现 CIFAR-10 Tiny CNN:卷积、池化和反向传播
Share summary Building a Tiny CIFAR-10 CNN in C: Convolution, Pooling, and Backpropagation

A source-based walkthrough of cifar10_tiny_cnn.c, covering CIFAR-10 binary input, 3x3 convolution, ReLU, max pooling, fully connected logits, softmax, backpropagation, and local commands.

Open share center

Companion resources

Leave a Reply

Project timeline

Published posts

  1. AI Basics Learning Roadmap Separate AI, machine learning, and deep learning before going into implementation details.
  2. Machine Learning Workflow Follow the practical path from data and features to training, prediction, and evaluation.
  3. Model Training and Evaluation Understand loss, overfitting, train/test splits, accuracy, recall, and F1.
  4. Neural Network Basics Move from perceptrons to activation, forward propagation, backpropagation, and training loops.
  5. NLP Basics: Understanding Bag of Words and TF-IDF An introduction to the most fundamental text representation methods in NLP: Bag of Words (BoW) and TF-IDF.
  6. RNN Basics: Handling Sequential Data with Memory Understand the core concepts of Recurrent Neural Networks (RNN), the role of hidden states, and their application in NLP.
  7. Transformer Self-Attention Read Q/K/V, scaled dot-product attention, multi-head attention, and positional encoding before exploring LLM internals.
  8. Python AI Mini Practice Run a small scikit-learn classification task and read the experiment output.
  9. Handwritten Digit Dataset Basics Read train.csv, test.csv, labels, and the flattened 28 by 28 pixel layout before training the classifier.
  10. Handwritten Digit Softmax in C Follow the C implementation from logits and softmax probabilities to confusion matrices and submission export.
  11. Handwritten Digit Playground Notes See how the offline classifier was adapted into a browser demo with drawing input and probability output.
  12. CIFAR-10 Tiny CNN Tutorial in C Build and train a small convolutional neural network for CIFAR-10 image classification, then read its loss and accuracy output.
  13. Building a Tiny CIFAR-10 CNN in C: Convolution, Pooling, and Backpropagation A source-based walkthrough of cifar10_tiny_cnn.c, covering CIFAR-10 binary input, 3x3 convolution, ReLU, max pooling, fully connected logits, softmax, backpropagation, and local commands.
  14. High-Entropy Traffic Defense Notes Study encrypted metadata leaks, entropy, traffic classifiers, and a defensive Python chaffing prototype.
  15. AI Security Threat Modeling Build a defense map with NIST adversarial ML, MITRE ATLAS, and OWASP LLM risks.
  16. Adversarial Examples and Robust Evaluation Evaluate clean and perturbed accuracy with an FGSM-style digits experiment.
  17. Data Poisoning and Backdoor Defense Study poison rate, trigger behavior, attack success rate, and training pipeline controls.
  18. Model Privacy and Extraction Defense Measure membership inference signal and surrogate fidelity against a local toy model.
  19. LLM, RAG, and Agent Security Separate instructions from data and enforce tool permissions against indirect prompt injection.

Published resources

  1. Python AI practice code guide The article includes a runnable scikit-learn classification script.
  2. digit_softmax_classifier.c The C source for the handwritten digit softmax classifier.
  3. train.csv.zip Compressed handwritten digit training set with 42000 labeled samples.
  4. test.csv.zip Compressed handwritten digit test set with 28000 unlabeled samples.
  5. sample_submission.csv The official submission format example for checking the final output columns.
  6. submission.csv The prediction file generated by the current C project.
  7. digit-playground-model.json The compact softmax demo model and sample set used by the browser playground.
  8. digit-sample-grid.svg A small handwritten digit preview grid extracted from the training set.
  9. Handwritten digit project bundle Contains the source file, compressed datasets, submission files, browser model, and preview grid.
  10. cifar10_tiny_cnn.c source Single-file C tiny CNN with CIFAR-10 loading, convolution, pooling, softmax, and backpropagation.
  11. model_weights.bin sample weights Model weights generated by one local small-sample run.
  12. test_predictions.csv sample predictions Sample test prediction output from the CIFAR-10 tiny CNN.
  13. CNN project explanation PDF Companion explanation material for the CNN project.
  14. Virtual Mirror redacted code skeleton A redacted mld_chaffing_v2.py control-flow skeleton with secrets, node topology, and target lists removed.
  15. Virtual Mirror stress-test template A redacted CSV template for CPU, memory, peak threads, pulse rate, latency, and error measurements.
  16. Virtual Mirror classifier-evaluation template A CSV template for TP, FN, FP, TN, accuracy, precision, recall, F1, ROC-AUC, entropy, and JS divergence.
  17. Virtual Mirror resource notes Notes explaining why the public resources include only redacted code, test templates, and architecture context.
  18. AI Security Lab README Setup, safety boundaries, and quick-run commands for the AI Security series.
  19. AI Security Lab full bundle Includes safe toy scripts, result CSVs, risk register, attack-defense matrix, and architecture diagram.
  20. AI security risk register CSV risk register template for AI threat modeling and release review.
  21. AI attack-defense matrix Maps attack surface, toy demo, metric, and defensive control into one CSV table.
  22. AI Security Lab architecture diagram Shows threat modeling, robustness, data integrity, model privacy, and RAG guardrails.
  23. FGSM digits robustness script FGSM-style perturbation and accuracy-drop experiment for a local digits classifier.
  24. Data poisoning and backdoor toy script Demonstrates poison rate, trigger behavior, and attack success rate on digits.
  25. Model privacy and extraction toy script Outputs membership AUC, target accuracy, surrogate fidelity, and surrogate accuracy.
  26. RAG prompt injection guard toy script Uses a deterministic toy agent to demonstrate external-data demotion and tool-policy blocking.
  27. Deep Learning topic share card A 1200x630 SVG card for sharing the Deep Learning / CNN topic hub.
  28. Machine Learning From Scratch share card A 1200x630 SVG card for the K-means, Iris, and ML workflow topic hub.
  29. Student AI Projects share card A 1200x630 SVG card for handwritten digits, C classifiers, and browser demos.
  30. CNN convolution scan animation An 8-second Remotion animation showing how a 3x3 convolution kernel scans an input and builds a feature map.

Current route

  1. AI Basics Learning Roadmap Learning path step
  2. Machine Learning Workflow Learning path step
  3. Model Training and Evaluation Learning path step
  4. Neural Network Basics Learning path step
  5. Transformer Self-Attention Learning path step
  6. LLM Visualizer Learning path step
  7. Python AI Mini Practice Learning path step
  8. Handwritten Digit Dataset Basics Learning path step
  9. Handwritten Digit Softmax in C Learning path step
  10. Handwritten Digit Playground Notes Learning path step
  11. CIFAR-10 Tiny CNN Tutorial in C Learning path step
  12. High-Entropy Traffic Defense Notes Learning path step
  13. AI Security Threat Modeling Learning path step
  14. Adversarial Examples and Robust Evaluation Learning path step
  15. Data Poisoning and Backdoor Defense Learning path step
  16. Model Privacy and Extraction Defense Learning path step
  17. LLM, RAG, and Agent Security Learning path step

Next notes

  1. Add more image-classification and error-analysis cases
  2. Turn common metrics into a quick reference
  3. Add more AI security defense experiment notes