Concepts / Training and test data

Training and test data

MNIST is a classic handwritten-digit classification dataset.

  • Programming

From Mark to Category

A handwritten-digit classifier starts with an image and must place that image into one of ten categories: 0 through 9. MNIST provides a concrete dataset for this task. It contains handwritten digit images together with the labels that identify the digits represented by those images.

Key point

The central pairing is image plus label: the image is the input, and the label identifies which digit category the image belongs to.

Inside One Digit Image

For this classification problem, each MNIST image is grayscale and has dimensions of 28 × 28 pixels. The visible handwritten mark is therefore represented to the neural-network example as a two-dimensional arrangement of pixel positions, with a grayscale value associated with each position. The classifier does not receive the digit as an already named category. It receives the image representation and must assign the image to one of the categories from 0 through 9.

Digit imageone handwritten digit28 × 28 gridpixel positionsPixel positiongrayscale valueDigit category0 through 9represented ascontainsmust be assigned to
What does one handwritten digit look like to the neural network in memory, and how do pixel positions map to the visible image?

Four Parts of the Dataset

Keras and MNIST provide four related parts for the neural-network example: training images, training labels, test images, and test labels. The images are the handwritten-digit inputs. The labels are the corresponding digit answers. The training pair supplies examples for learning, while the test pair is kept as a separate set for measuring how the classifier performs.

Training imageshandwritten digit inputsTest imagesseparate digit inputsTraining labelscorresponding digit answersTest labelscorresponding digit answerspaired withpaired withsupports learningsupports performance measurement
Which images and labels are used to learn, which are held back for evaluation, and how are each image and its digit answer connected?
Dataset partContainsRole
Training imagesHandwritten digit imagesInputs used for learning
Training labelsDigit labels corresponding to the training imagesAnswers associated with the learning inputs
Test imagesHandwritten digit images in the separate test setInputs used for evaluation
Test labelsDigit labels corresponding to the test imagesAnswers used to measure performance

The four MNIST components and their roles in the neural-network example.

Following One Example

Worked example

Tracing an image-label pair

Suppose one MNIST image visibly represents a handwritten 7. Trace the information from the image to the classification task.

  1. 1
    Represent the input

    The handwritten mark is represented as a grayscale image with dimensions of 28 × 28 pixels.

  2. 2
    Keep the answer separate

    The corresponding label identifies the digit category as 7. The label is associated with the image; it is not another pixel in the image.

  3. 3
    Place the pair in a split

    If the pair belongs to the training set, the image and its label form part of the data used for learning. If the pair belongs to the test set, it forms part of the separate data used to measure performance.

  4. 4
    Classify

    The classifier receives the image and must place it into one of the ten categories from 0 through 9.

Result

A single example consists of an image representation and its corresponding digit label. The training or test role depends on which dataset split contains that pair.

What do you think happens?

A 28 × 28 grayscale image is paired with the label 3. What is the classifier expected to do with that image?

  • Treat 3 as one of the image's pixel positions

  • Place the image into the digit category 3

  • Remove the label before classification

Reveal answer
Answer:

Place the image into the digit category 3.

The image is the input representation, while the label is the corresponding digit answer. The classification task is to assign the image to one of the categories from 0 through 9.

Loading the Learning Problem

Keras supplies MNIST in a form that separates images from their corresponding labels and separates training data from test data. This gives the neural-network example a clear starting point: the training image data can be considered alongside the training labels, and the test image data can later be considered alongside the test labels. The data-loading step establishes the source and organization of the problem; it does not, by itself, show the complete construction of the neural network.

Keras MNISTdataset accessTraining images28 × 28 grayscale imagesTraining labelsdigit answersTest imagesseparate image setTest labelsdigit answersprovidesprovidesprovidesprovides
What happens when Keras loads MNIST, and how does the returned data flow into image and label variables for the neural-network example?
Best practice

When reading a neural-network example, identify the four data components before focusing on the network itself. Ask which variables represent images, which represent labels, and which pair belongs to training rather than testing.

Mistakes About the Split

  • Common mistake

    Treating the label as part of the image

    What it looks like

    Thinking that the digit label 7 is one of the grayscale pixel values in the 28 × 28 image

    Why it fails

    The image and its corresponding label are separate parts of the dataset.

    Do this instead

    Treat the image as the input representation and the label as the associated digit answer.

  • Common mistake

    Confusing training data with test data

    What it looks like

    Assuming that all MNIST images are used in exactly the same role

    Why it fails

    MNIST is divided into training and test images, with corresponding labels.

    Do this instead

    Keep the training image-label pair conceptually separate from the test image-label pair.

  • Common mistake

    Expecting the dataset-loading step to construct the whole neural network

    What it looks like

    Reading the Keras MNIST access step as if it already described the complete network

    Why it fails

    The data source establishes the problem's inputs and labels, but the complete neural-network construction is not shown at this stage.

    Do this instead

    First trace how the data enters the workflow; study the network elements separately afterward.

Check Your Understanding

EASY

Explain, in your own words, why a handwritten digit example needs both an image and a label. Then describe the difference between a training image-label pair and a test image-label pair.

Hints
  • Mention the 28 × 28 grayscale representation.
  • Explain what the label identifies.
  • State which pair supports learning and which pair supports performance measurement.
MEDIUM

A learner says, “The test label teaches the network what to do while it is learning.” Correct the statement using the roles of the four MNIST data components.

Hints
  • Contrast the training pair with the test pair.
  • Remember that test data is used to measure performance.

Key Takeaways

  1. 1
    MNIST is a classic handwritten-digit classification dataset.
  2. 2
    Each image is grayscale and has dimensions of 28 × 28 pixels.
  3. 3
    The classifier receives an image and assigns it to one of the ten categories from 0 through 9.
  4. 4
    Training images and training labels form the learning data; test images and test labels form the separate data used to measure performance.
  5. 5
    Keras supplies MNIST with the images and labels separated into training and test data.

At a glance

  • MNIST contains grayscale handwritten-digit images and their corresponding labels.
  • Each image is represented as a 28 × 28 pixel grid.
  • The classification categories are the digits 0 through 9.
  • Training image-label pairs support learning, while test image-label pairs support performance measurement.
  • Keras provides MNIST in a form that separates images from labels and training data from test data.

More concepts starting with T