A simple and educational example of using Torch7 convolutional neural networks for handwritten digit recognition on the USPS dataset.
Sample training data: Each image contains 1,100 examples of a digit (33 columns × 34 rows, 16×16 pixels each)
- Torch7 machine learning library
imagetorch packageqtluatorch package (optional, for data visualization)
To set up the git pre-commit hook that automatically formats Lua code with StyLua:
- Install StyLua (see installation guide)
- Run the setup script:
./setup-hooks.sh
The pre-commit hook will automatically format any staged Lua files before each commit.
Simply run the main script:
th usps_cnn.luaThe script will:
- Load the USPS handwritten digit dataset (10 classes, 16×16 pixel images)
- Create and train a convolutional neural network
- Test the network and display classification errors
Training the network
----------------------
Index Label Prediction
23 7 9
47 7 1
67 7 9
...
Test error 0.034 ( 34 out of 1000 )
Typical test error: 3-4% (results vary due to stochastic gradient descent)
The CNN processes 16×16 grayscale images through the following layers:
Input: 16×16×1 image
↓
Convolutional Layer: 6 filters (5×5) → 12×12×6
↓
Spatial Sub-Sampling: 2×2 pooling → 6×6×6
↓
Reshape to vector: 216 features
↓
Tanh activation: bounded to [-1, 1]
↓
Fully Connected: 216 → 10 outputs (one per digit)
↓
LogSoftMax: log-probabilities for each class
Dataset Split:
- Training: 1,000 samples per digit (10,000 total)
- Testing: 100 samples per digit (1,000 total)
Layer-by-layer breakdown:
The first layer of the network is a set of local filters that are applied convolutionally across the image. This is followed by a sub-sampling layer to reduce data dimensionality and introduce a bit of translation invariance. After that, a non-linear transfer function (hyperbolic tangent) keeps the responses bounded to [-1,1]. We then have a linear layer with 10 outputs (one for each digit). Finally, a LogSoftMax operation produces log-probabilities that can be used with the negative log-likelihood criterion.
Classification Results: Test error typically around 3-4%
Note that training the network with stochastic gradient descent will give slightly different results each time due to randomness in sample selection during training.
This example was kept intentionally simple for educational purposes. Here are several ways to extend it:
- Normalize data before training
- Use learning rate dependent on number of neurons in layer
- Use a validation dataset to avoid over-fitting
- Show current error during training
- Display wrongly classified examples at test time
- Jointly train multiple networks (e.g., one for digits and another for letters)
- Share first layer parameters between networks using Torch's
cloneandsharemethods
Author: Hugo Penedones
Mentors/Collaborators: David Grangier, Ronan Collobert (During 2010 Summer internship at NEC Labs, Princeton, US and at Idiap Research Institute, Switzerland)
Y. LeCun, L. Bottou, G. Orr and K. Muller: Efficient BackProp, in Orr, G. and Muller K. (Eds), Neural Networks: Tricks of the trade, Springer, 1998
This project demonstrates a simple but effective approach to handwritten digit recognition using convolutional neural networks with Torch7.


