Neural Networks (MLP)

Neural Networks (MLP)#

Neural networks are a class of machine learning models designed around the idea of processing data through layers of interconnected neurons. In the simplest setting, data flows in one direction, from the input layer to the output layer. The input layer receives data, the intermediate layers transform it into increasingly meaningful representations, and the output layer produces the final result.

In this chapter, we will build our first neural network in PyTorch. The network will be a multilayer perceptron (MLP), which describes an architecture that consists of multiple layers of linear transformations interleaved with nonlinear activation functions. We will train it to classify 28×28 grayscale images of handwritten digits into ten classes. Training will use MNIST, a small and approachable dataset with 60,000 development images and 10,000 test images, a classic benchmark in machine learning.

You should already be comfortable with tensors, automatic differentiation, and basic Python classes. By the end of the chapter, you will be able to:

  • create reproducible training, validation, and test data loaders;

  • define a feedforward neural network with linear layers and nonlinear activation functions;

  • train it with a loss function and an optimizer;

  • evaluate its predictions with metrics and error analysis.

A neural network with only linear layers makes it an excellent first model, but not an ideal image architecture. The next chapter introduces convolutional networks, which preserve and exploit spatial structure.

MNIST