Back to blog
← View series: ibm ai engineering
← View series: ibm ai engineering
~/blog
8.2.2dropoutRegression_v2
Apr 1, 2026•3 min read•By Mohammed Vasim
AIMachine LearningLLMPyTorchTensorFlowGenerative AILangChainAI Agents
Using Dropout in Regression
Objective for this Notebook
1. Create the Model and Cost Function the PyTorch way.
2. Learn Batch Gradient Descent
Table of Contents
In this lab, you will see how adding dropout to your model will decrease overfitting.
Estimated Time Needed: 20 min
Preparation
We'll need the following libraries
python
# Import the libraries we need for the lab
import torch
import matplotlib.pyplot as plt
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from torch.utils.data import Dataset, DataLoader
torch.manual_seed(0)Make Some Data
Create polynomial dataset class:
python
# Create Data object
class Data(Dataset):
# Constructor
def __init__(self, N_SAMPLES=40, noise_std=1, train=True):
self.x = torch.linspace(-1, 1, N_SAMPLES).view(-1, 1)
self.f = self.x ** 2
if train != True:
torch.manual_seed(1)
self.y = self.f + noise_std * torch.randn(self.f.size())
self.y = self.y.view(-1, 1)
torch.manual_seed(0)
else:
self.y = self.f + noise_std * torch.randn(self.f.size())
self.y = self.y.view(-1, 1)
# Getter
def __getitem__(self, index):
return self.x[index], self.y[index]
# Get Length
def __len__(self):
return self.len
# Plot the data
def plot(self):
plt.figure(figsize = (6.1, 10))
plt.scatter(self.x.numpy(), self.y.numpy(), label="Samples")
plt.plot(self.x.numpy(), self.f.numpy() ,label="True Function", color='orange')
plt.xlabel("x")
plt.ylabel("y")
plt.xlim((-1, 1))
plt.ylim((-2, 2.5))
plt.legend(loc="best")
plt.show()Create a dataset object:
python
# Create the dataset object and plot the dataset
data_set = Data()
data_set.plot()Get some validation data:
python
# Create validation dataset object
validation_set = Data(train=False)Create the Model, Optimizer, and Total Loss Function (Cost)
Create a custom module with three layers. in_size is the size of the input features, n_hidden is the size of the layers, and out_size is the size. p is dropout probability. The default is 0 which is no dropout.
python
# Create the class for model
class Net(nn.Module):
# Constructor
def __init__(self, in_size, n_hidden, out_size, p=0):
super(Net, self).__init__()
self.drop = nn.Dropout(p=p)
self.linear1 = nn.Linear(in_size, n_hidden)
self.linear2 = nn.Linear(n_hidden, n_hidden)
self.linear3 = nn.Linear(n_hidden, out_size)
def forward(self, x):
x = F.relu(self.drop(self.linear1(x)))
x = F.relu(self.drop(self.linear2(x)))
x = self.linear3(x)
return xCreate two model objects: model had no dropout, and model_drop has a dropout probability of 0.5:
,[object Object],
,
,[object Object],[object Object],[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,
,[object Object],
,[object Object],
,[object Object],
,
,[object Object],[object Object],[object Object]