How to estimate output using pytorch

In this series, i will walk through how to use pytorch for deep learning problems.

First let’s start with a simple example of finding parameters for a linear regression problem., and how to use the parameters to estimate output.

Linear regression problem is stated as below:

Y=mX+C (1)

where Y is output, X is input, m and C are slope and bias respectively.


Steps to estimate the output using pytorch :

  1. Import torch library
  2. Define a lienar model which has one input, one output. with parameters defined as in equation (1)
  3. Assign a tensor to input.
  4. Estimate output from model parameters provided by pytorch.

Software code: Use below code to get the estimated output.

“”” import library “””
import torch

from torch.nn import Linear

“””define linear model”””

model=Linear(in_features=1,out_features=1)

“”” print model parameters m-slope and c-bias “””

print(list(model.parameters()))

“”” assign tensor to input “””

x=torch.tensor([10.0])

“”” estimate output “””

yhat=model(x)