Product

Wednesday, 21 February 2024

Training a Multi-Target Multilinear Regression Model in PyTorch

 


The multi-target multilinear regression model is a type of machine learning model that takes single or multiple features as input to make multiple predictions. In our earlier post, we discussed how to make simple predictions with multilinear regression and generate multiple outputs. Here we’ll build our model and train it on a dataset.

In this post, we’ll generate a dataset and define our model with an optimizer and a loss function. Then, we’ll train our model and visualize the results of the training process. Particularly, we’ll explain:

  • How to train a multi-target multilinear regression model in PyTorch.
  • How to generate a simple dataset and feed it to the model.
  • How to build the model using built-in packages in PyTorch.
  • How to train the model with mini-batch gradient descent and visualize the results.

Overview

This tutorial is in four parts; they are

  • Create Data Class
  • Build the Model with nn.Module
  • Train with Mini-Batch Gradient Descent
  • Plot the Progress

Create Data Class

We need data to train our model. In PyTorch, we can make use of the Dataset class. Firstly, we’ll create our data class that includes data constructer, the __getitem__() method that returns data samples from the data, and the __len__() method that allows us to check data length. We generate the data, based on a linear model, in the constructor. Note that torch.mm() is used for matrix multiplication and the shapes of tensors should be set in such a way to allow the multiplication.

Then, we can create the dataset object that will be used in training.

Build the Model with nn.Module

PyTorch’s nn.Module contains all the methods and attributes we need to build our multilinear regression model. This package will help us to build more sophisticated neural network architectures in the future tutorials of the series.

We’ll make our model class a subclass of the nn.Module package, allowing us to inherit all the incorporated functionalities. Our model will include a constructor and a forward() function to make predictions.

As we have to deal with multiple outputs, let’s create a model object with two inputs and two outputs. We’ll list the model parameters as well.

This is what the parameters look like, which the weights are randomized initially.

Here’s what the output looks like.

We’ll train the model using stochastic gradient descent, keeping the learning rate at 0.1. For measuring the model loss, we’ll use mean square error.

PyTorch has a DataLoader class which allows us to feed the data into the model. This not only allow us to load the data but also can apply various transformations in realtime. Before we start the training, let’s define our dataloader object and define the batch size.

Kick-start your project with my book Deep Learning with PyTorch. It provides self-study tutorials with working code.

Train with Mini-Batch Gradient Descent

With all things set, we can create our training loop to train the model. We create an empty list to store the model loss and train the model for 20 epochs.

If you run this, you should see the output similar to the following:

Plot the Progress

Because it is a linear regression model, the training should be fast. We can visualize how the model loss decreases after every epoch during the training process.

Putting everything together, the following is the complete code.

Summary

In this tutorial, you learned what are the steps required to train a muti-target Multilinear Regression model in PyTorch. Particularly, you learned:

  • How to train a multi-target Multilinear Regression Model in PyTorch.
  • How to generate a simple dataset and feed it to the model.
  • How to build the model using built-in packages in PyTorch.
  • How to train the model with Mini-Batch Gradient Descent and visualize the results.

No comments:

Post a Comment

Connect broadband

AI:List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with trauma for kin ocd formed after forced to do rituals, family tribal values honor specific activities, utmost religious activities occult witchcraft enforced to do by elder sibling his wife and mother thrice with great celibacy turned the kundalini activated after deep celibacy penance shouting physical Mental financial Extortion by sibling with toxic behaviours and the wife mother and then mockery suppression asking about who’re your worth backbiting among relatives sector toon negligence of intelligence intuitive behaviour- all religious psychological traps when not work and lead to loss of consciousness of kin trance state recovered by governance with technocrats using AI - kin was asked by mother that he’s to go the male should not be seen in house he’s many absurd thought forcefully in brain mind and outsiders already feel awkward of this kin unresponsive behaviour due to TBI PTSD ocd cvt and tell at workplace social gathering he’s to go and asked for sector toon extortion and mockery in various sense due to un practical behaviour negligence of body building due to neglect ion if essential food nutrition and stubbed suppressed in every sense in religious psychological traps by family members AI humanoid using various neural networks and LLMs for kin and required steps.

  List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with tr...