How to build a neural network using PyTorch

August 19, 2025

Starting my second Kaggle competition, I noticed the tutorial provided by the competition hosts uses Pytorch to create Long Short Term Memory (LSTM) and Graph Neural Network (GNN) models. Pytorch is an open-source framework that is used to build and train deep learning models. (Alternative frameworks include TensorFlow or Keras). A neural network is basically a huge function that is loosely built on the concept of neurons in a brain. Each layer of neurons passes on information to the next layer of neurons. The neurons in each layer modify that information they receive based on parameters (specifically, weights and biases). Then, the neural network outputs a prediction.

I’ve never programmed a basic neural network, but this quick exercise will give me a better understanding of how they work before trying to apply LSTMs and GNNs to a real world problem. It will also give me more familiarity with the PyTorch framework.

I watched the video But what is a neural network? from 3Blue1Brown to get a more granular understanding of how neural networks work. Then, I watched Pytorch in 100 seconds by Fireship, which explained Pytorch’s uses and gave a very quick explanation of how to build a neural network. Perfect combo!

You can follow along by watching both videos first, or just read through my explanation below. My Colab notebook is HERE.

Objective: Create a basic neural network that takes images from the famous MNIST dataset and predicts the handwritten number present in each image.

1. Import the necessary libraries

Import libraries code

2. Explore the dataset

Load dataset

Size of dataset

Format type of values in dataset

🔍 The ‘.shape’ attribute tells me the dimensions of the data. If the data is in the form ‘(data, target)’ and I know I’m looking for image data, I can expect the first value in the tuple to have multiple dimensions. I see that the shape of the first value in the tuple is [1, 28, 28]. PyTorch tensor format represents images as [number of channels, image height in pixels, image width in pixels]. That looks like image data! I originally printed the value of the first tuple, but it was a very, very long tensor, so I only included the shape in the final code.

First value

🔍 However, I did print the second tuple value. The target is the handwritten number represented by the image.

See the image Image

3. Create the neural network class

Create nn class

4. Create the preprocessing and loading pipeline

Create preprocessing/loading pipeline

5. Run the neural network

Run neural network

After running the code block, look at the “Predicted digit for first image” vs “Actual digit for first image”. Are they congruent? Mine aren’t. The model needs to be trained. This means that it needs to compare its predictions to the targets. Then it needs to optimize its parameters to get the smallest difference possible between its predictions and the targets. Building the most basic version of the model itself was a great first step! In my next post, I’ll talk about adding code to allow the model to actually train and learn.