DEV Community

Cover image for Pytorch for Neural Networks Part 1: Writing Your First Neural Network in Pytorch
Rijul Rajesh
Rijul Rajesh

Posted on

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This module gives us access to various activation functions and other useful operations.


from torch.optim import SGD
Enter fullscreen mode Exit fullscreen mode

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):
Enter fullscreen mode Exit fullscreen mode

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__()
Enter fullscreen mode Exit fullscreen mode

Here, we define the constructor (__init__) for our neural network.

The line:

super().__init__()
Enter fullscreen mode Exit fullscreen mode

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 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

Top comments (0)