Practicals · Day 1
Feedforward neural networks
Can a neural network classify a clothing image that it did not see during training? The solution notebook answers this question with Fashion-MNIST.
Day 1 files
day1_solution.ipynb in Jupyter. Run the cells in order. The first section includes the package-installation command if it is needed.The activity question
Can a dense neural network learn to assign an unseen clothing image to the correct Fashion-MNIST category?
You will compare two models. The first is a linear classifier that maps pixels directly to ten class scores. The second is a multilayer perceptron (MLP) with two hidden layers. The comparison asks whether the hidden layers improve performance on images that were not used to update the model.
Dataset overview
| Observation | One 28 by 28 greyscale image of an item of clothing. |
| Input | The 784 pixel values in the image. |
| Target | One clothing category, recorded as a code from 0 to 9. |
| Classes | Ten balanced categories, with the same number of examples from each class in a given split. |
| Total | 18,000 images in the classroom dataset. |
How the data are split
The three splits have different jobs. Keeping these jobs separate prevents the final test data from influencing the model choice.
| Split | Images | Per class | Role in the activity |
|---|---|---|---|
| Training | 12,000 | 1,200 | Supplies the batches used to update model parameters. |
| Validation | 3,000 | 300 | Compares the linear classifier and MLP. It is also used to read the training curves. |
| Test | 3,000 | 300 | Provides one final result after the model and procedure have been chosen. |
Data dictionary
The dataset is a NumPy .npz file. It contains seven named arrays. Each image array has one matching label vector.
| Array | Shape | What it contains | How it is used |
|---|---|---|---|
x_train | 12,000 × 28 × 28 | The training images. Pixel values run from 0 to 255. | Input to the models during training. |
y_train | 12,000 labels | The correct category code for each training image. | Used to calculate the training loss. |
x_validation | 3,000 × 28 × 28 | Images kept out of the parameter updates. | Input for model comparison and error analysis. |
y_validation | 3,000 labels | The correct category code for each validation image. | Used to calculate validation loss, accuracy and the confusion matrix. |
x_test | 3,000 × 28 × 28 | The final held-out images. | Input for the final evaluation only. |
y_test | 3,000 labels | The correct category code for each test image. | Used once to calculate final loss and accuracy. |
class_names | 10 names | The clothing name attached to each category code. | Used for table headings, figure titles and interpretation. |
Class label dictionary
| Code | Class | Code | Class |
|---|---|---|---|
0 | T-shirt/top | 5 | Sandal |
1 | Trouser | 6 | Shirt |
2 | Pullover | 7 | Sneaker |
3 | Dress | 8 | Bag |
4 | Coat | 9 | Ankle boot |
Source and preparation
The source is Fashion-MNIST from Zalando Research, released under the MIT licence. The original dataset contains 60,000 training images and 10,000 test images.
The classroom dataset uses a balanced subset selected with a fixed seed. The 12,000 training images and 3,000 validation images come from the original training release. The 3,000 test images come from the original test release. The images and labels are unchanged. The notebook rescales pixel values from 0–255 to 0–1 before they enter a model.
What this activity asks you to do
- display examples and identify classes that may be difficult to separate;
- calculate a simple baseline that does not use the image pixels;
- follow one batch from images to logits, probabilities and predictions;
- compare the parameter counts of a linear classifier and an MLP;
- work through one complete parameter update;
- train both models and compare their validation loss and accuracy;
- inspect the confusion matrix and the most confident errors;
- select a model from validation evidence and evaluate it once on the test data.
Learning outcomes
- explain the different roles of training, validation and test data;
- state the shape and meaning of the image and label tensors in a batch;
- distinguish logits, probabilities, predictions, loss and accuracy;
- explain what happens during a neural-network training step;
- compare two models using validation evidence rather than training fit alone;
- read a loss curve, confusion matrix and set of inspected errors;
- report one final result and give a clear limitation.
What the evidence does not establish
- The classroom subset is smaller than the full Fashion-MNIST training set, so its scores should not be treated as full-dataset benchmark results.
- Fashion-MNIST images are small, centred and greyscale. The result does not describe colour photographs or images collected in different conditions.
- The activity compares two feedforward models. It does not show that either is the best possible architecture for image classification.
- A high softmax probability does not guarantee that a prediction is correct.