# Pytorch for Neural Networks Part 1: Writing Your First Neural Network in Pytorch

> Source: <https://dev.to/rijultp/pytorch-for-neural-networks-part-1-writing-your-first-neural-network-in-pytorch-37lc>
> Published: 2026-05-29 21:16:24+00:00

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.

``` python
import torch
```

`torch`

is used to create **tensors**, which store all the numerical data in neural networks, such as:

``` python
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.

``` python
import torch.nn.functional as F
```

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

``` python
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.

``` python
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](https://github.com/HexmosTech/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](https://github.com/HexmosTech/git-lrc)
