from torch import nn
import torch
Clear Linear
In this lab, we will review how to make a prediction for Linear Regression with Multiple Output.
Keywords
Training Two Parameter, Mini-Batch Gradient Decent, Training Two Parameter Mini-Batch Gradient Decent
Objective
- How to make a prediction using multiple samples.
Class Linear
Set the random seed:
1) torch.manual_seed(
<torch._C.Generator at 0x7fee040d21b0>
Set the random seed:
class linear_regression(nn.Module):
def __init__(self,input_size,output_size):
super(linear_regression,self).__init__()
self.linear=nn.Linear(input_size,output_size)
def forward(self,x):
=self.linear(x)
yhatreturn yhat
create a linear regression object, as our input and output will be two we set the parameters accordingly
=linear_regression(1,10)
model1.0])) model(torch.tensor([
tensor([ 0.7926, -0.3920, 0.1714, 0.0797, -1.0143, 0.5097, -0.0608, 0.5047,
1.0132, 0.1887], grad_fn=<AddBackward0>)
we can use the diagram to represent the model or object
we can see the parameters
list(model.parameters())
[Parameter containing:
tensor([[ 0.5153],
[-0.4414],
[-0.1939],
[ 0.4694],
[-0.9414],
[ 0.5997],
[-0.2057],
[ 0.5087],
[ 0.1390],
[-0.1224]], requires_grad=True),
Parameter containing:
tensor([ 0.2774, 0.0493, 0.3652, -0.3897, -0.0729, -0.0900, 0.1449, -0.0040,
0.8742, 0.3112], requires_grad=True)]
we can create a tensor with two rows representing one sample of data
=torch.tensor([[1.0]]) x
we can make a prediction
=model(x)
yhat yhat
tensor([[ 0.7926, -0.3920, 0.1714, 0.0797, -1.0143, 0.5097, -0.0608, 0.5047,
1.0132, 0.1887]], grad_fn=<AddmmBackward>)
each row in the following tensor represents a different sample
=torch.tensor([[1.0],[1.0],[3.0]]) X
we can make a prediction using multiple samples
=model(X)
Yhat Yhat
tensor([[ 0.7926, -0.3920, 0.1714, 0.0797, -1.0143, 0.5097, -0.0608, 0.5047,
1.0132, 0.1887],
[ 0.7926, -0.3920, 0.1714, 0.0797, -1.0143, 0.5097, -0.0608, 0.5047,
1.0132, 0.1887],
[ 1.8232, -1.2748, -0.2164, 1.0184, -2.8972, 1.7091, -0.4722, 1.5222,
1.2912, -0.0561]], grad_fn=<AddmmBackward>)
the following figure represents the operation, where the red and blue represents the different parameters, and the different shades of green represent different samples.
Back to top