Note
Go to the endto download the full example code.
Defining a Neural Network in PyTorch#
Created On: Apr 17, 2020 | Last Updated: Feb 06, 2024 | Last Verified: Nov 05, 2024
Deep learning uses artificial neural networks (models), which arecomputing systems that are composed of many layers of interconnectedunits. By passing data through these interconnected units, a neuralnetwork is able to learn how to approximate the computations required totransform inputs into outputs. In PyTorch, neural networks can beconstructed using thetorch.nn package.
Introduction#
PyTorch provides the elegantly designed modules and classes, includingtorch.nn, to help you create and train neural networks. Annn.Module contains layers, and a methodforward(input) thatreturns theoutput.
In this recipe, we will usetorch.nn to define a neural networkintended for theMNISTdataset.
Setup#
Before we begin, we need to installtorch if it isn’t alreadyavailable.
pipinstalltorch
Steps#
Import all necessary libraries for loading our data
Define and initialize the neural network
Specify how data will pass through your model
[Optional] Pass data through your model to test
1. Import necessary libraries for loading our data#
For this recipe, we will usetorch and its subsidiariestorch.nnandtorch.nn.functional.
importtorchimporttorch.nnasnnimporttorch.nn.functionalasF
2. Define and initialize the neural network#
Our network will recognize images. We will use a process built intoPyTorch called convolution. Convolution adds each element of an image toits local neighbors, weighted by a kernel, or a small matrix, thathelps us extract certain features (like edge detection, sharpness,blurriness, etc.) from the input image.
There are two requirements for defining theNet class of your model.The first is writing an __init__ function that referencesnn.Module. This function is where you define the fully connectedlayers in your neural network.
Using convolution, we will define our model to take 1 input imagechannel, and output match our target of 10 labels representing numbers 0through 9. This algorithm is yours to create, we will follow a standardMNIST algorithm.
classNet(nn.Module):def__init__(self):super(Net,self).__init__()# First 2D convolutional layer, taking in 1 input channel (image),# outputting 32 convolutional features, with a square kernel size of 3self.conv1=nn.Conv2d(1,32,3,1)# Second 2D convolutional layer, taking in the 32 input layers,# outputting 64 convolutional features, with a square kernel size of 3self.conv2=nn.Conv2d(32,64,3,1)# Designed to ensure that adjacent pixels are either all 0s or all active# with an input probabilityself.dropout1=nn.Dropout2d(0.25)self.dropout2=nn.Dropout2d(0.5)# First fully connected layerself.fc1=nn.Linear(9216,128)# Second fully connected layer that outputs our 10 labelsself.fc2=nn.Linear(128,10)my_nn=Net()print(my_nn)
We have finished defining our neural network, now we have to define howour data will pass through it.
3. Specify how data will pass through your model#
When you use PyTorch to build a model, you just have to define theforward function, that will pass the data into the computation graph(i.e. our neural network). This will represent our feed-forwardalgorithm.
You can use any of the Tensor operations in theforward function.
classNet(nn.Module):def__init__(self):super(Net,self).__init__()self.conv1=nn.Conv2d(1,32,3,1)self.conv2=nn.Conv2d(32,64,3,1)self.dropout1=nn.Dropout2d(0.25)self.dropout2=nn.Dropout2d(0.5)self.fc1=nn.Linear(9216,128)self.fc2=nn.Linear(128,10)# x represents our datadefforward(self,x):# Pass data through conv1x=self.conv1(x)# Use the rectified-linear activation function over xx=F.relu(x)x=self.conv2(x)x=F.relu(x)# Run max pooling over xx=F.max_pool2d(x,2)# Pass data through dropout1x=self.dropout1(x)# Flatten x with start_dim=1x=torch.flatten(x,1)# Pass data through ``fc1``x=self.fc1(x)x=F.relu(x)x=self.dropout2(x)x=self.fc2(x)# Apply softmax to xoutput=F.log_softmax(x,dim=1)returnoutput
4. [Optional] Pass data through your model to test#
To ensure we receive our desired output, let’s test our model by passingsome random data through it.
# Equates to one random 28x28 imagerandom_data=torch.rand((1,1,28,28))my_nn=Net()result=my_nn(random_data)print(result)
Each number in this resulting tensor equates to the prediction of thelabel the random tensor is associated to.
Congratulations! You have successfully defined a neural network inPyTorch.
Learn More#
Take a look at these other recipes to continue your learning: