Pytorch for Neural Networks Part 1: Writing Your First Neural Network in Pytorch
In my previous series of articles, we mainly explored the theory behind various neural network concepts . In this new series, we will focus on putting that knowledge into practice using code . This will be a fun way to turn what we have learned into something more practical. We will start with the basics and build things step by step. For this article, we will be using the following modules. Importing PyTorch import torch torch is used to create tensors , which store all the numerical data in neural networks, such as: raw input data weights biases import torch.nn as nn This module helps us define and build neural network components. It also allows us to make weights and biases part of the neural network. import torch.nn.functional as F This module gives us access to various activation functions and other useful operations. from torch.optim import SGD SGD , which stands for Stochastic Gradient Descent , is an optimization algorithm used to fit the neural network to data. Creating a Neural Network Now let us begin building our neural network. When creating a neural network in PyTorch, we usually start by creating a class. class MyBasicNN ( nn . Module ): Here, we create a class named MyBasicNN . This class inherits from a PyTorch class called nn.Module . By inheriting from nn.Module , our class gains all the functionality needed to behave like a neural network in PyTorch. Initializing the Neural Network Next, we define the initialization method. class MyBasicNN ( nn . Module ): def __init__ ( self ): super (). __init__ () Here, we define the constructor ( __init__ ) for our neural network. The line: super (). __init__ () calls the initialization method of the parent class nn.Module . This ensures that all the necessary PyTorch functionality is properly set up for our neural network. What Comes Next? The next step is to initialize the weights and biases for our neural network. Before doing that, we first need an example problem so we know what kind of neural network we