Neural Networks in PyTorch

Basics of nn.Module and nn.Parameter

The ‘nn.Module’ is a base class in PyTorch for all neural network modules. It includes the trainable parameters and defines the forward method for performing forward-pass computations. Thus, it is responsible for parameter management and submodule management, Serialization, and Loading.

On the other hand, nn.Parameter is the subclass of the torch.Tensor that is responsible for parameter initialization, optimization, and access. The nn.Parameter tensors are defined as attributes within the nn.Module subclass. The nn.Parameter tensors behave like regular tensors but are recognized as model parameters by PyTorch’s Autograd system.

Building Neural Network using PyTorch

Let’s create simple neural network model using the Iris dataset, which is popular dataset for classification tasks. The Iris dataset contains measurements of iris flowers, including sepal length, sepal width, petal length, and petal width, along with their corresponding species (setosa, Versicolor, or Virginia).

Defining Neural Network Architecture

  • We are first loading the Iris dataset and split it into training and testing sets.
  • After this, we define simple neural network architecture. Here, we are defining the input layer (fc1) that contains the linear transformation (nn.Linear) to map the input features to the hidden layer.
  • Then, we have to apply the ReLU activation function (nn.ReLU) to introduce non-linearity in the model.

Python




import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
 
# Load the Iris dataset
iris = load_iris()
X, y = iris.data, iris.target
 
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 
# Standardize the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
 
# Define the neural network architecture
class SimpleNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)  # Input layer
        self.relu = nn.ReLU()                          # Activation function
        self.fc2 = nn.Linear(hidden_size, output_size) # Output layer
         
    def forward(self, x):
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        return x


Training a Neural Network on a Simple Dataset

After defining the architecture, we have to train the model. In the following code snippet, we set a random seed for reproducibility, and define the input, hidden, and output sizes of the neural network architecture. After this, we instantiate the neural network model, define the loss function (CrossEntropyLoss) and optimizer (Adam), convert the training data to PyTorch tensors, and train the model for a fixed number of epochs.

During training, we perform forward pass computations to obtain predicted outputs and calculate the loss between predicted and actual labels. Also, we have to update model parameters using the optimizer.

Python




# Set random seed for reproducibility
torch.manual_seed(42)
 
 
# Define the input size, hidden size, and output size of the neural network
input_size = X.shape[1]
hidden_size = 10
output_size = len(iris.target_names)
 
 
# Instantiate the neural network
model = SimpleNN(input_size, hidden_size, output_size)
 
 
# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)
 
 
# Convert datto PyTorch tensors
X_train_tensor = torch.FloatTensor(X_train)
y_train_tensor = torch.LongTensor(y_train)
 
 
# Train the model
num_epochs = 100
for epoch in range(num_epochs):
    # Forward pass
    outputs = model(X_train_tensor)
    loss = criterion(outputs, y_train_tensor)
    
    # Backward pass and optimization
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    # Print the loss every 10 epochs
    if (epoch+1) % 10 == 0:
        print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')


Evaluating the Trained Model

Now, our Model has been trained. We will evaluate the model on the test dataset. For this, we have to convert the test dataset from NumPy Arrays into the PyTorch Sensors using the torch.FloatTensor() and torch.LongTensor(). Then, we pass the test input X_test_tensor through the trained model to obtain the output. At last, these are compared with the actual predicted labels y_test_tensor to calculate the accuracy.

Python




# Evaluate the model
with torch.no_grad():
    X_test_tensor = torch.FloatTensor(X_test)
    y_test_tensor = torch.LongTensor(y_test)
    outputs = model(X_test_tensor)
    _, predicted = torch.max(outputs, 1)
    accuracy = (predicted == y_test_tensor).sum().item() / len(y_test_tensor)
    print(f'Accuracy on the test set: {accuracy:.2f}')


Output:

Epoch [10/100], Loss: 0.7783
Epoch [20/100], Loss: 0.5399
Epoch [30/100], Loss: 0.3921
Epoch [40/100], Loss: 0.2934
Epoch [50/100], Loss: 0.2166
Epoch [60/100], Loss: 0.1639
Epoch [70/100], Loss: 0.1284
Epoch [80/100], Loss: 0.1050
Epoch [90/100], Loss: 0.0902
Epoch [100/100], Loss: 0.0800

Start learning PyTorch for Beginners

Machine Learning helps us to extract meaningful insights from the data. But now, it is capable of mimicking the human brain. This is done using neural networks, which contain the various interconnected layers of nodes containing the data. This data is passed to forward layers. Subsequently, the model learns from the data and predicts output for the new data.

PyTorch helps us to create and train these neural networks that act like our brains and learn from the data.

Table of Content

  • What is Pytorch?
  • Why use PyTorch?
  • How to install Pytorch ?
  • PyTorch Basics
  • Autograd: Automatic Differentiation in PyTorch
  • Neural Networks in PyTorch
  • Working with Data in PyTorch
  • Intermediate Topics in PyTorch
  • Validation and Testing
  • Frequently Asked Questions

Similar Reads

What is Pytorch?

PyTorch is an open-source machine learning library for Python developed by Facebook’s AI Research Lab (FAIR). It is widely used for building deep learning models and conducting research in various fields like computer vision, natural language processing, and reinforcement learning. One of the key features of PyTorch is its dynamic computational graph, which allows for more flexible and intuitive model construction compared to static graph frameworks. PyTorch also offers seamless integration with other popular libraries like NumPy, making it easier to work with tensors and multidimensional arrays....

Why use PyTorch?

It supports tensor computation: Tensor is the data structure that is similar to the networks, array. It is an n-dimensional array that contains the data. We can perform arbitrary numeric computation on these arrays using the APIs. It provides Dynamic Graph Computation: This feature allows us to define the computational graphs dynamically during runtime. This makes it more flexible than the static computation graphs approach in which where the graph structure is fixed and defined before execution, It provides the Automatic Differentiation: The Autograd package automatically computes the gradients that are crucial for training the model using optimization algorithms. Thus, we can perform operations on tensors without manually calculating gradients. It has Support for Python: It has native support for the Python programming language. Thus, we can easily integrate with existing Python workflows and libraries. This is the reason why it is used by the machine learning and data science communities. It has its production environment: PyTorch has the TorchScript which is the high-performance environment for serializing and executing PyTorch models. You can easily compile PyTorch models into a portable intermediate representation (IR) format. Due to this, we can deploy the model on various platforms and devices without requiring the original Python code....

How to install Pytorch ?

To install PyTorch, you can use the pip package manager, which is the standard tool for installing Python packages. You can install PyTorch using the following command:...

PyTorch Basics

PyTorch Tensors: Creation, Manipulation, and Operations...

Autograd: Automatic Differentiation in PyTorch

...

Neural Networks in PyTorch

Now, we will shift our focus on Autograd which is one of the most important topics in the PyTorch basics. The Autograd Module of PyTorch provides the automatic calculation of the gradients. It means that we do not need to calculate the gradients explicitly. You might be thinking what gradient is. So, the gradient represents the rate of change of functions with respect to parameters. This helps us to identify the difference between the predicted outputs and actual labels....

Working with Data in PyTorch

...

Intermediate Topics in PyTorch

Basics of nn.Module and nn.Parameter...

Validation and Testing

...

Conclusion

...

Frequently Asked Questions

...

Contact Us