L4 Linear Regression
L4 Linear Regression
• height 16 210
• y=wx+b 20 240
Simple Linear Regression
• Coefficient: w 250
• Bias/intercept: b 200
y
Set of training points
Predicted Fit
100
50
0
0 5 10 15 20 25
x
Simple Linear Regression
import torch
# defining the parameters 'w' and 'b'
w = torch.tensor(3.0, requires_grad = True)
b = torch.tensor(1.0, requires_grad = True)
Simple Linear Regression
• Write a function to make predictions for y at any given value of x
• In PyTorch prediction step is called forward step.
# predict y_pred at x = 2
x = torch.tensor([[2.0]])
y_pred = forward(x)
print("prediction of y at 'x = 2' is: ", y_pred)
# making predictions at multiple values of x
x = torch.tensor([[3.0], [4.0]])
y_pred = forward(x)
print("prediction of y at 'x = 3 & 4' is: ", y_pred)
Simple Linear Regression
prediction of y at 'x = 2' is: tensor([[7.]], grad_fn=<AddBackward0>)
prediction of y at 'x = 3 & 4' is: tensor([[10.],
[13.]], grad_fn=<AddBackward0>)
Train a simple linear regression model
• In order to train a linear regression model, we need to define a cost function and
an optimizer.
• The cost function is used to measure how well our model fits the data,
• while the optimizer decides which direction to move in order to improve this fit.
• Training the model and updating the parameters after going through a single
iteration of training data is known as one epoch
• We should train the model for several epochs so that weights and biases can
learn the linear relationship between the input features and output labels.
• So far, used simple predictions with only a linear regression forward pass
• Now, train a linear regression model and update its learning parameters
Building a Linear Regression Model with
PyTorch
Building a Linear Regression Model with PyTorch
x y
2 20
4 40
Problem 1– Verify loss, w.grad, b.grad, new w, new b
class Linear_Regression(nn.Module):
def __init__(self, input_sample, output_sample):
# Inheriting properties from the parent calss
super(Linear_Regression, self).__init__()
self.linear = nn.Linear(input_sample, output_sample)
• Dataset : PyTorch’s TensorDataset is a Dataset wrapping tensors. By defining a length and way of
indexing, this also gives us a way to iterate, index, and slice along the first dimension of a tensor.
This will make it easier to access both the independent and dependent variables in the same line
as we train.
• Model: Instead of initializing the weights & biases manually, we can define the model using
torch.nn.Linear
• Optimizer: We can make use of the built Stochastic Gradient Descent optimizer (
torch.optim.SGD)
Optim Module
• The Optim module in PyTorch has pre-written codes for most of the
optimizers that are used while building a neural network.
• We just have to import them and then they can be used to build
models.
• Ex: to get SDG Optimizer supported in PyTorch
# importing the optim module
from torch import optim
SGD = optim.SGD(model.parameters(), lr=learning_rate)
PyTorch Deep Learning Model Life-Cycle
• Life-cycle for a deep learning model and the PyTorch API that we can use to
define models
• PyTorch provides the Dataset class that we can extend and customize to load our dataset.
• the constructor of our dataset object can load our data file (e.g. a CSV file).
• We can then override
• __len__() function that can be used to get the length of the dataset (number of rows or samples), and
• __getitem__() function that is used to get a specific sample by index.
• Note: When loading our dataset, we can also perform any required transforms, such as scaling or
encoding.
#dataset definition
class MyDataset(Dataset):
# load the dataset
def __init__(self, path):
# store the inputs and outputs
self.X = ...
self.y = ...
x = torch.tensor(
[12.4, 14.3, 14.5, 14.9, 16.1, 16.9, 16.5, 15.4, 17.0, 17.9, 18.8, 20.3, 22.4, 19.4, 15.5, 16.7,
17.3, 18.4, 19.2,
17.4, 19.5, 19.7, 21.2])
y = torch.tensor(
[11.2, 12.5, 12.7, 13.1, 14.1, 14.8, 14.4, 13.4, 14.9, 15.6, 16.4, 17.7, 19.6, 16.9, 14.0, 14.6,
15.1, 16.1, 16.8,
15.2, 17.0, 17.2, 18.6])
#23 values
#Find if CUDA is available to load the model and device on to the available device CPU/GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
Data read - DataLoader
class MyDataset(Dataset):
def __init__(self, X, Y):
self.X = X
self.Y = Y
def __len__(self):
return len(self.X)
dataset = MyDataset(x,y)
dataloader = DataLoader(dataset, batch_size=4, shuffle=True)
• A DataLoader instance can be created for the training dataset, test dataset, and
even a validation dataset.
• The random_split() function can be used to split a dataset into train and test sets.
• Once split, a selection of rows from the Dataset can be provided to a DataLoader,
along with the batch size and whether the data should be shuffled every epoch.
• Many layers are available, such as Linear for fully connected layers, Conv2d for
convolutional layers, and MaxPool2d for pooling layers.
• Activation functions can also be defined as layers, such as ReLU, Softmax, and
Sigmoid.
• Training the model involves enumerating the DataLoader for the training
dataset.
• First, a loop is required for the number of training epochs. Then an inner
loop is required for the mini-batches for stochastic gradient descent.
...
# enumerate epochs
for epoch in range(100):
# enumerate mini batches
for i, (inputs, targets) in enumerate(train_dl):
...
Step 3: Train the Model
Each update to the model involves the same general pattern comprised
of:
• Clearing the last error gradient.
• A forward pass of the input through the model.
• Calculating the loss for the model output.
• Backpropagating the error through the model.
• Update the model in an effort to reduce loss.
Step 3: Train the Model
...
# clear the gradients
optimizer.zero_grad()
# compute the model output
yhat = model(inputs)
# calculate loss
loss = criterion(yhat, targets)
# credit assignment
loss.backward()
# update model weights
optimizer.step()
Step 4: Evaluate the model
• Once the model is fit, it can be evaluated on the test dataset.
• This can be achieved by using the DataLoader for the test dataset and
• collecting the predictions for the test set, then
• comparing the predictions to the expected values of the test set and
calculating a performance metric.
...
for i, (inputs, targets) in enumerate(test_dl):
# evaluate the model on the test set
yhat = model(inputs)
...
Step 5: Make predictions
• A fit model can be used to make a prediction on new data.
• Ex: we have a single image or a single row of data and want to make a prediction.
• This requires that we wrap the data in a PyTorch Tensor data structure.
• A Tensor is the PyTorch array for holding data. It also allows to perform the automatic
differentiation tasks in the model graph, like calling backward() when training the model.
• The prediction too will be a Tensor, although we can retrieve the NumPy array by
detaching the Tensor from the automatic differentiation graph and calling the NumPy
function.
Step 5: Make predictions
...
# convert row to data
row = Variable(Tensor([row]).float())
# make prediction
yhat = model(row)
# retrieve numpy array
yhat = yhat.detach().numpy()
Building a Linear Class
import torch model = Linear_Regression(input_sample=1,
from torch import nn output_sample=1)
torch.manual_seed(42) print("printing the model parameters: ",
list(model.parameters()))
class Linear_Regression(nn.Module): x = torch.tensor([[2.0]])
def __init__(self, input_sample, output_sample): y_pred = model(x)
# Inheriting properties from the parent calss print("getting the prediction for x: ", y_pred)
x = torch.tensor([[3.0], [4.0]])
super(Linear_Regression, self).__init__()
y_pred = model(x)
self.linear = nn.Linear(input_sample, output_sample) print("prediction of y at 'x = 3 & 4' is: ", y_pred)
# define function to make predictions
def forward(self, x):
output = self.linear(x)
return output
Building a Linear Class - Output
printing the model parameters: [Parameter containing:
tensor([[0.7645]], requires_grad=True), Parameter containing:
tensor([0.8300], requires_grad=True)]
getting the prediction for x: tensor([[2.3591]],
grad_fn=<AddmmBackward0>)
prediction of y at 'x = 3 & 4' is: tensor([[3.1236],
[3.8882]], grad_fn=<AddmmBackward0>)
Multilinear regression model
• The multilinear regression model is a supervised learning algorithm
that can be used to predict the target variable y given multiple input
variables x.
• It is a linear regression problem where more than one input variables
x or features are used to predict the target variable y.
• A typical use case of this algorithm is predicting the price of a house
given its size, number of rooms, and age.