Prediction
author: Juma Shafara date: "2024-08-08" title: Logistic Regression keywords: [Training Two Parameter, Mini-Batch Gradient Decent, Training Two Parameter Mini-Batch Gradient Decent] description: In this lab, we will cover logistic regression using PyTorch.

Objective
- How to create a logistic regression object with the nn.Sequential model.
Table of Contents
In this lab, we will cover logistic regression using PyTorch.
Estimated Time Needed: 15 min
Preparation
We'll need the following libraries:
Set the random seed:
Logistic Function
Create a tensor ranging from -100 to 100:
Create a sigmoid object:
Apply the element-wise function Sigmoid with the object:
Plot the results:
Apply the element-wise Sigmoid from the function module and plot the results:
Build a Logistic Regression with nn.Sequential
Create a 1x1 tensor where x represents one data sample with one dimension, and 2x1 tensor X represents two data samples of one dimension:
Create a logistic regression object with the nn.Sequential model with a one-dimensional input:
The object is represented in the following diagram:

In this case, the parameters are randomly initialized. You can view them the following ways:
Make a prediction with one sample:
Calling the object with tensor X performed the following operation (code values may not be the same as the diagrams value depending on the version of PyTorch) :

Make a prediction with multiple samples:
Calling the object performed the following operation:
Create a 1x2 tensor where x represents one data sample with one dimension, and 2x3 tensor X represents one data sample of two dimensions:
Create a logistic regression object with the nn.Sequential model with a two-dimensional input:
The object will apply the Sigmoid function to the output of the linear function as shown in the following diagram:

In this case, the parameters are randomly initialized. You can view them the following ways:
Make a prediction with one sample:
The operation is represented in the following diagram:

Make a prediction with multiple samples:
The operation is represented in the following diagram:

Build Custom Modules
In this section, you will build a custom Module or class. The model or object function is identical to using nn.Sequential.
Create a logistic regression custom module:
Create a 1x1 tensor where x represents one data sample with one dimension, and 3x1 tensor where \(X\) represents one data sample of one dimension:
Create a model to predict one dimension:
In this case, the parameters are randomly initialized. You can view them the following ways:
Make a prediction with one sample:
Make a prediction with multiple samples:
Create a logistic regression object with a function with two inputs:
Create a 1x2 tensor where x represents one data sample with one dimension, and 3x2 tensor X represents one data sample of one dimension:
Make a prediction with one sample:
Make a prediction with multiple samples:
Practice
Make your own model my_model as applying linear regression first and then logistic regression using nn.Sequential(). Print out your prediction.
Double-click here for the solution.
About the Author:
Hi, My name is Juma Shafara. Am a Data Scientist and Instructor at DATAIDEA. I have taught hundreds of peope Programming, Data Analysis and Machine Learning.
I also enjoy developing innovative algorithms and models that can drive insights and value.
I regularly share some content that I find useful throughout my learning/teaching journey to simplify concepts in Machine Learning, Mathematics, Programming, and related topics on my website jumashafara.dataidea.org.
Besides these technical stuff, I enjoy watching soccer, movies and reading mystery books.