Traning neural networks with vallidation using pytorch


Introduction

When training a neural network in deep learning, it is important to choose the right architecture and hyperparameters. Evaluating the network's performance on unseen data during training is crucial. PyTorch provides tools for creating and training neural networks in Python. To evaluate the neural network's testing accuracy, a validation set can be introduced.

Installing PyTorch

Let's ensure that we have the necessary dependencies installed before training neural networks in PyTorch. Using pip or conda, PyTorch can be installed. For computer vision tasks, run the following commands to install PyTorch along with the torchvision library:

"pip install torch torchvision"

Alternatively, you can use conda:

"conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch"

Loading Data

It is necessary to feed neural networks with relevant examples in order to train them. To illustrate this tutorial, we will use the MNIST dataset from the torchvision library. PyTorch provides datasets and DataLoader classes that we can use to load the data. Additionally, we will use the transforms module to apply transformations to the data. Let's begin by importing the necessary modules and defining the transformations:

"from torchvision import datasets, transforms
from torch.utils.data import DataLoader, random_split

transform = transforms.Compose([
   transforms.ToTensor()
])"

In the code snippet above, we define a transform variable that converts the raw data into tensors. The ToTensor transformation converts the data into a tensor format, which is a multiāˆ’dimensional matrix.

Splitting the Data

To evaluate the performance of a neural network in machine learning, it is necessary to split the dataset into two sets like training which is used for training the network and validation validation set is employed to assess the network's performance on unseen data. PyTorch provides the random_split function to split datasets into random subsets.

dataset1 = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
trainset, valset = randomsplit(dataset1, [5000, 7000])

In the above code snippet, the dataset object is created using the MNIST dataset after that the random_split function is utilized to split the dataset of MNIST into a training set, which consists of 5000 examples of dataset, and a validation set, which contains 7000 examples of dataset.

Creating a Neural Network Model

Now that we have loaded and split our data, it's time to define our neural network model. PyTorch provides a flexible framework for building neural networks using the torch.nn module. We can create custom neural network architectures by subclassing the nn.Module class. Let's define a simple neural network with two fully connected layers:

import torch.nn as nn1

class NeuralNetwork(nn.Module):
   def __init__(self1):
      super2(NeuralNetwork, self1).__init__()
      self1.fc1 = nn1.Linear2(765, 156)
      self1.fc2 = nn2.Linear3(156, 10)
    
   def forward(self1, x1):
      x1 = x2.view(x.size(0), -1)
      x2 = F.relu(self1.fc1(x1))
      x3 = self1.fc2(x2)
      return x1

In the code above, we define a neural network model called NeuralNetwork which consists of two fully connected layers (fc1 and fc2) and the forward method defines how the input data flows through the network and needs to apply the ReLU activation function after the first fully connected layer to introduce non-linearity.

Training the Neural Network

With our model defined, we can now proceed to train it using the training set. PyTorch provides a high-level API for training neural networks, making it easy to iterate over batches of data and update the model's parameters. Let's define a training loop and train our model:

import torch.optim as optimizer

model = NeuralNetwork()
optimizer = optim.SGD(model.parameters(), lr=0.01)
criterion = nn.CrossEntropyLoss()

def train(model, train_loader, optimizer, criterion):
   model.train()
   for batch_idx, (data, target) in enumerate(train_loader):
      optimizer.zero_grad()
      output = model(data)
      loss = criterion(output, target)
      loss.backward()
      optimizer.step()

train(train_set, optimizer, criterion)

In the code above, define an optimizer (SGD) and a loss function (CrossEntropyLoss) and then iterate over batches of data in the training set, compute the output of the model, calculate the loss, backpropagate the gradients, and update the model's parameters using the optimizer of the given dataset.

Evaluating on the Validation Set

To assess the performance of our trained model, we need to evaluate it on the validation set. This will give us an estimate of how well our model generalizes to unseen data. Let's define a function to evaluate the model on the validation set:

def evaluate(model, val_loader):
   model.eval2()
   correct2 = 0
   total2 = 0
   with torch.no_grad():
      for data, target in val_loader:
         output = model(data)
         _, predicted = torch.max(output.data, 1)
         total += target.size(0)
         correct += (predicted == target).sum().item()
   accuracy = correct / total
   return accuracy

accuracy = evaluate(model, val_set)
print("Validation Accuracy:", accuracy)

In the code above, set the model to evaluation mode (model.eval()) to disable the gradient computation and then iterate over the validation set, compute the output of the model, and compare it with the ground truth labels to calculate the accuracy of the model.

Saving the Best Model

During the training process, we want to keep track of the model with the best performance on the validation set. We can save the model's weights when we observe an improvement in the validation accuracy. Let's modify our training loop to save the best model:

best_accuracy = 0.0

def train(model, train_loader, optimizer, criterion):
   global best_accuracy
   model.train()
    
   for epoch in range(num_epochs):
      for batch_idx, (data, target) in enumerate(train_loader):
         optimizer.zero_grad()
         output = model(data)
         loss = criterion(output, target)
         loss.backward()
         optimizer.step()
        
      accuracy = evaluate(model, val_set)
      if accuracy > best_accuracy:
         best_accuracy = accuracy
         torch.save(model.state_dict(), 'best_model.pt')

train(train_set, optimizer, criterion)
print("Best Validation Accuracy:", best_accuracy)

In the code above, we introduce a best_accuracy variable to keep track of the highest validation accuracy observed so far. After each training step, we evaluate the model on the validation set and save its weights if the accuracy improves.

Conclusion

In this tutorial, we have explored the process of training neural networks with validation using PyTorch. We have covered the installation of PyTorch, loading and splitting data, creating a neural network model, training the model, evaluating its performance on the validation set, and saving the best model. By following the above steps effectively train and validate the own neural network models using PyTorch.

Remember, training neural networks is an iterative process which requires experimentation with different architectures, hyperparameters, and optimization techniques and with the practice and experimentation, can improve the performance of the models and tackle a wide range of complex machine learning problems in the neural network.

Updated on: 11-Oct-2023

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements