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.
import torch
torch
is used to create tensors, which store all the numerical data in neural networks, such as:
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.
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.
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.
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 want to build.
We will explore that in the next article.
AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.
git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.
Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.
Give it a ⭐ star on Github