Menu

1 / 5

Overfitting vs Underfitting in Practice: 4 Experiments on MNIST

DEV Community: pytorch·Zoricic·1 day ago
#w0CpuNXd
#devtopytorch#devto#dev#model#loss
Long article (2361 words) — no AI summary yet.
Reading 0:00
15s threshold

"The model is overfitting" is one of the first things you hear when you start training neural networks. Definitions only go so far. The clearest way to see it is to train the same network several ways and put the loss charts side by side.

This post walks through that experiment. Same dataset, same training code. The main things that change are how many neurons the model has and how much data it sees. At the end, I try one common fix, dropout, on the overfitting model.

The setup

The dataset is MNIST: 28×28 grayscale images of handwritten digits 0–9. There are 60,000 images for training and 10,000 separate images for testing.

The model is as small as it gets: one hidden layer.

from torch import nn

model = nn . Sequential ( nn . Linear ( 784 , hidden ) , # 784 pixels in, `hidden` neurons nn . ReLU ( ) , nn . Linear ( hidden , 10 ) # 10 outputs, one score per digit )

Enter fullscreen mode

Exit fullscreen mode

Each image is flattened to 784 numbers, and the model outputs 10 scores.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.