Gradient Descent from Scratch using Pytorch
A tutorial of gradient descent from scratch using pytorch
- Convert to dataset and dataloader
- A dataloader always gives a tuple of the training data and the label with it
- Create a model using NN.Linear and pass the input through it to generate to produce the output
- Calculate the loss
- Run the process of Gradient_Descent to find optimal weights and bias
- Now we can compare the results obtained by the model after the weights are updated and gradient_descent is run
![]()
This notebook is an implementation of a gradient descent using PYTORCH
gradient_descent is the foundation of all deep learning problems. It is super critical tp understand this
import numpy as np
import torch
import pandas as pd
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
from torch.utils.data import random_split
import torch.nn as nn
import torch.nn.functional as F
inp = np.arange(58,387,step = 5.5, dtype=np.float32).reshape(10,6)
inp
inp.dtype, inp.shape
actual = np.arange(6000,9000,step = 320.7,dtype=np.float32).reshape(10,1)
actual
actual.dtype, actual.shape
inp= torch.from_numpy(inp)
actual = torch.from_numpy(actual)
ds = TensorDataset(inp, actual)
# To divide the data into train and validation set we use random_split
# [8,2] splits the data into training and validation dataset
#tr_dataset, val_dataset = random_split(ds,[8,2])
# Dataloader helps to split data into batches and shuffling the data
train_loader = DataLoader(ds, shuffle=True)
# datatypes of dataset an train_loader
type(ds) , type(train_loader)
#Iteration to see what a dataloader provides
for data,label in train_loader:
print(data)
print(label)
# inputs are the number of columns in a tabular dataset
# outputs can be the number of outputs
input_size = 6
output_size = 1
model = nn.Linear(input_size,output_size)
# To look at the weight and bias use a parameter method
list(model.parameters())
# using the model to generate predictions
predictions = model(inp)
actual.shape, predictions.shape
# Now the loss function can be computed by directly using F.mse_loss
loss = F.mse_loss(predictions,actual)
# Models wts and bias can be updated automatically using a optimizer
opt = torch.optim.SGD(model.parameters(),lr = 1e-6)
opt
# Steps for nn implementation
num_epochs = 2000
for epoch in range(num_epochs):
for data,label in train_loader:
# preds
preds = model(data)
# calculate loss
loss = F.mse_loss(preds,label)
# gradient calculation
loss.backward()
# update wts and bias wrt loss
opt.step()
# gradients values are 0 again
opt.zero_grad()
if epoch%30==0:
print(loss)
# predictions after the weights are updated
model(inp)
# Actual values from the model
actual