<jupyter_start><jupyter_text>Logistic Regression Table of ContentsIn this lab, we will cover logistic regression using PyTorch.    Logistic Function    Build a Logistic Regression Using nn.Sequential    Build Custom ModulesEstimated Time Needed: 15 min Preparation We'll need the following libraries:<jupyter_code># Import the libraries we need for this lab

import torch.nn as nn
import torch
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Set the random seed:<jupyter_code># Set the random seed

torch.manual_seed(2)<jupyter_output><empty_output><jupyter_text>Logistic Function Create a tensor ranging from -100 to 100:<jupyter_code>z = torch.arange(-100, 100, 0.1).view(-1, 1)
print("The tensor: ", z)<jupyter_output>The tensor:  tensor([[-100.0000],
        [ -99.9000],
        [ -99.8000],
        ...,
        [  99.7000],
        [  99.8000],
        [  99.9000]])<jupyter_text>Create a sigmoid object:<jupyter_code># Create sigmoid object

sig = nn.Sigmoid()<jupyter_output><empty_output><jupyter_text>Apply the element-wise function Sigmoid with the object:<jupyter_code># Use sigmoid object to calculate the 

yhat = sig(z)<jupyter_output><empty_output><jupyter_text>Plot the results:<jupyter_code>plt.plot(z.numpy(), yhat.numpy())
plt.xlabel('z')
plt.ylabel('yhat')<jupyter_output><empty_output><jupyter_text>Apply the element-wise Sigmoid from the function module and plot the results:<jupyter_code>yhat = torch.sigmoid(z)
plt.plot(z.numpy(), yhat.numpy())<jupyter_output><empty_output><jupyter_text>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:<jupyter_code># Create x and X tensor

x = torch.tensor([[1.0]])
X = torch.tensor([[1.0], [100]])
print('x = ', x)
print('X = ', X)<jupyter_output>x =  tensor([[1.]])
X =  tensor([[  1.],
        [100.]])<jupyter_text>Create a logistic regression object with the nn.Sequential model with a one-dimensional input:<jupyter_code># Use sequential function to create model

model = nn.Sequential(nn.Linear(1, 1), nn.Sigmoid())<jupyter_output><empty_output><jupyter_text>The object is represented in the following diagram:   In this case, the parameters are randomly initialized. You can view them the following ways:<jupyter_code># Print the parameters

print("list(model.parameters()):\n ", list(model.parameters()))
print("\nmodel.state_dict():\n ", model.state_dict())<jupyter_output>list(model.parameters()):
  [Parameter containing:
tensor([[0.2294]], requires_grad=True), Parameter containing:
tensor([-0.2380], requires_grad=True)]

model.state_dict():
  OrderedDict([('0.weight', tensor([[0.2294]])), ('0.bias', tensor([-0.2380]))])<jupyter_text>Make a prediction with one sample:<jupyter_code># The prediction for x

yhat = model(x)
print("The prediction: ", yhat)<jupyter_output>The prediction:  tensor([[0.4979]], grad_fn=<SigmoidBackward>)<jupyter_text>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:<jupyter_code># The prediction for X

yhat = model(X)
yhat<jupyter_output><empty_output><jupyter_text>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:<jupyter_code># Create and print samples

x = torch.tensor([[1.0, 1.0]])
X = torch.tensor([[1.0, 1.0], [1.0, 2.0], [1.0, 3.0]])
print('x = ', x)
print('X = ', X)<jupyter_output>x =  tensor([[1., 1.]])
X =  tensor([[1., 1.],
        [1., 2.],
        [1., 3.]])<jupyter_text>Create a logistic regression object with the nn.Sequential model with a two-dimensional input:<jupyter_code># Create new model using nn.sequential()

model = nn.Sequential(nn.Linear(2, 1), nn.Sigmoid())<jupyter_output><empty_output><jupyter_text>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:<jupyter_code># Print the parameters

print("list(model.parameters()):\n ", list(model.parameters()))
print("\nmodel.state_dict():\n ", model.state_dict())<jupyter_output>list(model.parameters()):
  [Parameter containing:
tensor([[ 0.1939, -0.0361]], requires_grad=True), Parameter containing:
tensor([0.3021], requires_grad=True)]

model.state_dict():
  OrderedDict([('0.weight', tensor([[ 0.1939, -0.0361]])), ('0.bias', tensor([0.3021]))])<jupyter_text>Make a prediction with one sample:<jupyter_code># Make the prediction of x

yhat = model(x)
print("The prediction: ", yhat)<jupyter_output>The prediction:  tensor([[0.6130]], grad_fn=<SigmoidBackward>)<jupyter_text>The operation is represented in the following diagram:  Make a prediction with multiple samples:<jupyter_code># The prediction of X

yhat = model(X)
print("The prediction: ", yhat)<jupyter_output><empty_output><jupyter_text>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:<jupyter_code># Create logistic_regression custom class

class logistic_regression(nn.Module):
    
    # Constructor
    def __init__(self, n_inputs):
        super(logistic_regression, self).__init__()
        self.linear = nn.Linear(n_inputs, 1)
    
    # Prediction
    def forward(self, x):
        yhat = torch.sigmoid(self.linear(x))
        return yhat<jupyter_output><empty_output><jupyter_text>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:<jupyter_code># Create x and X tensor

x = torch.tensor([[1.0]])
X = torch.tensor([[-100], [0], [100.0]])
print('x = ', x)
print('X = ', X)<jupyter_output><empty_output><jupyter_text>Create a model to predict one dimension:<jupyter_code># Create logistic regression model

model = logistic_regression(1)<jupyter_output><empty_output><jupyter_text>In this case, the parameters are randomly initialized. You can view them the following ways:<jupyter_code># Print parameters 

print("list(model.parameters()):\n ", list(model.parameters()))
print("\nmodel.state_dict():\n ", model.state_dict())<jupyter_output><empty_output><jupyter_text>Make a prediction with one sample:<jupyter_code># Make the prediction of x

yhat = model(x)
print("The prediction result: \n", yhat)<jupyter_output><empty_output><jupyter_text>Make a prediction with multiple samples:<jupyter_code># Make the prediction of X

yhat = model(X)
print("The prediction result: \n", yhat)<jupyter_output><empty_output><jupyter_text>Create a logistic regression object with a function with two inputs:<jupyter_code># Create logistic regression model

model = logistic_regression(2)<jupyter_output><empty_output><jupyter_text>Create a 1x2 tensor where x represents one data sample with one dimension, and 3x2 tensor X represents one data sample of one dimension:<jupyter_code># Create x and X tensor

x = torch.tensor([[1.0, 2.0]])
X = torch.tensor([[100, -100], [0.0, 0.0], [-100, 100]])
print('x = ', x)
print('X = ', X)<jupyter_output><empty_output><jupyter_text>Make a prediction with one sample:<jupyter_code># Make the prediction of x

yhat = model(x)
print("The prediction result: \n", yhat)<jupyter_output><empty_output><jupyter_text>Make a prediction with multiple samples:<jupyter_code># Make the prediction of X

yhat = model(X)
print("The prediction result: \n", yhat)<jupyter_output><empty_output><jupyter_text>Practice Make your own model my_model as applying linear regression first and then logistic regression using nn.Sequential(). Print out your prediction.<jupyter_code># Practice: Make your model and make the prediction

X = torch.tensor([-10.0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12510860121633363, 'fraction_numerical': 0.026312523271689214, 'mean_word_length': 5.875426621160409, 'pattern_counts': {'":': 0, '<': 108, '<?xml version=': 0, '>': 108, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '28', 'dir': 'jupyter-structured-clean-dedup', 'id': '5', 'license': 'MIT', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2643, 'n_words': 757, 'path': 'IBM_AI/4_Pytorch/5.1logistic_regression_prediction_v2.ipynb', 'repo_name': 'merula89/cousera_notebooks', 'size': '8057'}