Deep Learning PyTorch Course, Model Definition

Deep learning is a field of artificial intelligence and machine learning, based on artificial neural networks that mimic the human brain.
Thanks to the advancement of large datasets and powerful computing power, deep learning technology is receiving much more attention than in the past.
In particular, PyTorch is one of the deep learning frameworks, preferred by many researchers and developers due to its ease of use and flexibility.
This post will delve deeply into model definitions using PyTorch.

1. Understanding Deep Learning Models

A deep learning model is an algorithm that uses multiple layers of neural networks to perform predictions on input data.
The model consists of an input layer, hidden layers, and an output layer, each composed of nodes (neurons) of the neural network.

1.1 Basic Structure of Neural Networks

A basic neural network is composed of the following elements:

  • Input Layer: The layer that receives the data entering the model.
  • Hidden Layer: The layer that processes the input information, which can be stacked into multiple layers.
  • Output Layer: The layer that outputs the final prediction results.

2. Installing PyTorch

To use PyTorch, it must first be installed. You can install it using pip, the Python package management tool.
Please enter the following command in your terminal to install it:

pip install torch torchvision

3. Defining Models

The way to define a model in PyTorch is very intuitive. When defining the structure of a network, it typically involves
creating a custom module by inheriting from the torch.nn.Module class.
Additionally, various layers and functions can be utilized through the torch.nn module.

3.1 Simple Model Example

The example below shows code for defining a simple multi-layer perceptron (MLP) model. This model takes
a 784-dimensional vector as input and outputs a 10-dimensional vector (classifying digits 0-9).


import torch
import torch.nn as nn
import torch.optim as optim

# Model Definition
class MLP(nn.Module):
    def __init__(self):
        super(MLP, self).__init__()
        self.fc1 = nn.Linear(784, 128)  # Input layer
        self.fc2 = nn.Linear(128, 64)    # Hidden layer
        self.fc3 = nn.Linear(64, 10)      # Output layer
        self.relu = nn.ReLU()             # ReLU activation function
        self.softmax = nn.Softmax(dim=1)  # Softmax function

    def forward(self, x):
        x = self.fc1(x)          # Input layer -> Hidden layer 1
        x = self.relu(x)        # Activation
        x = self.fc2(x)          # Hidden layer 1 -> Hidden layer 2
        x = self.relu(x)        # Activation
        x = self.fc3(x)          # Hidden layer 2 -> Output layer
        return self.softmax(x)   # Softmax applied
    

In the code above, the MLP class defines the neural network model. It includes three linear layers and two ReLU activation layers. The model’s forward method defines how the data flows through the network.

3.2 Training the Model

After defining the model, it must be trained using data. For this, loss functions and optimization algorithms need to be set up.
Generally, for multi-class classification, cross-entropy loss function and Adam optimizer are commonly used.


# Setting the loss function and optimizer
model = MLP()
criterion = nn.CrossEntropyLoss()  # Loss function
optimizer = optim.Adam(model.parameters(), lr=0.001)  # Adam optimizer
    

Subsequently, the model will undergo many iterations to train. The code below shows an example of the entire training process.
It performs training for 10,000 iterations using the MNIST dataset.
It is important to note that the data is trained in mini-batches.


from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Dataset and loader settings
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
train_dataset = datasets.MNIST(root='./data', train=True, transform=transform, download=True)
train_loader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)

# Model Training
for epoch in range(5):  # Training for 5 epochs
    for i, (images, labels) in enumerate(train_loader):
        optimizer.zero_grad()    # Gradient initialization
        outputs = model(images.view(-1, 784))  # Flattening the images into a 784-dimensional vector
        loss = criterion(outputs, labels)  # Loss calculation
        loss.backward()         # Backpropagation
        optimizer.step()        # Weight update
        
        if (i+1) % 100 == 0:  # Logging every 100 batches
            print(f'Epoch [{epoch+1}/5], Batch [{i+1}/{len(train_loader)}], Loss: {loss.item():.4f}')
    

4. Various Model Architectures

The example above defined a simple multi-layer perceptron model, but in real deep learning, a variety of model architectures are needed.
For the second example, we will look at how to define a convolutional neural network (CNN).

4.1 Defining Convolutional Neural Networks (CNN)

Convolutional neural networks, widely used for processing image data, are defined with the following structure.


class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)  # Convolutional layer
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)  # Max pooling layer
        self.fc1 = nn.Linear(64 * 7 * 7, 128)  # Linear layer
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))  # First convolution + activation + pooling
        x = self.pool(F.relu(self.conv2(x)))  # Second convolution + activation + pooling
        x = x.view(-1, 64 * 7 * 7)  # Flattening
        x = F.relu(self.fc1(x))  # Linear layer + activation
        x = self.fc2(x)  # Output layer
        return x
    

5. Conclusion

In this post, we explored the methods to define models in PyTorch.
The codes discussed above can be applied widely from basic neural networks to CNNs.
Understanding the basics of deep learning and deepening your understanding of model definitions will lay the groundwork for solving more complex problems.
I encourage you to continue learning and practicing various deep learning technologies to gain extensive experience.

6. References

– Official PyTorch Documentation: https://pytorch.org/docs/stable/index.html
– Introductory Book on Deep Learning: “Deep Learning” by Ian Goodfellow, Yoshua Bengio, Aaron Courville

Deep Learning PyTorch Course, What is Machine Learning

1. Definition of Machine Learning

Machine Learning is a subfield of artificial intelligence that enables computers to learn from data and perform specific tasks. Typically, machine learning is characterized by the use of algorithms that can learn without being explicitly programmed. This is very useful for recognizing patterns in data, making predictions, and automating decision-making.

2. Basic Principles of Machine Learning

Machine learning models generally operate through the following process:

  1. Data Collection: Collect the data to be used for learning.
  2. Data Preprocessing: Perform tasks such as handling missing values and normalization to improve the quality of the data.
  3. Model Selection: Choose a machine learning model that is suitable for the problem.
  4. Training: Train the selected model using the data.
  5. Evaluation: Assess the model’s performance and adjust it if necessary.
  6. Prediction: Use the trained model to make predictions on new data.

3. Types of Machine Learning

Machine learning can primarily be divided into three types:

  • Supervised Learning: Learns the relationship between input and output when given input and output data. This mainly includes regression and classification problems.
  • Unsupervised Learning: Focuses on finding the structure or patterns in data when there is no output data available. Clustering is a representative example.
  • Reinforcement Learning: An agent learns strategies to maximize rewards through interaction with the environment.

4. What is PyTorch?

PyTorch is an open-source machine learning library developed by Facebook, primarily used as a framework for deep learning. PyTorch provides dynamic computation graphs, enabling flexible and intuitive coding. This is one of the reasons it is popular among researchers and developers.

Main Features of PyTorch

  • Dynamic Computation Graph: The computation graph is generated as soon as the code is executed, allowing easy modification of the model structure.
  • Diverse Tensor Operations: Enables tensor operations similar to NumPy, making it easy to preprocess training data.
  • GPU Support: Allows fast execution of large-scale operations by utilizing GPUs.
  • Scalability: Custom layers and models can be easily defined, making it applicable for various deep learning research.

5. Hands-on Machine Learning with PyTorch

Now we will build a simple machine learning model using PyTorch. We will be using the Iris dataset to create a model that classifies the types of flowers.

5.1. Loading the Dataset

First, we install the required libraries and load the data.


import torch
import torch.nn as nn
import torch.optim as optim
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder
import numpy as np

    

5.2. Data Preprocessing

After loading the Iris dataset, we separate the features and labels and carry out data preprocessing.


# Load the Iris dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Split the Data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Normalize the Data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Convert to Tensors
X_train_tensor = torch.FloatTensor(X_train)
y_train_tensor = torch.LongTensor(y_train)
X_test_tensor = torch.FloatTensor(X_test)
y_test_tensor = torch.LongTensor(y_test)

    

5.3. Defining the Model

We define a simple neural network model consisting of an input layer, a hidden layer, and an output layer.


class IrisModel(nn.Module):
    def __init__(self):
        super(IrisModel, self).__init__()
        self.fc1 = nn.Linear(4, 10)  # 4 input features and 10 hidden nodes
        self.fc2 = nn.Linear(10, 3)   # 10 hidden nodes and 3 output nodes (types of flowers)

    def forward(self, x):
        x = torch.relu(self.fc1(x))  # Using ReLU as the activation function
        x = self.fc2(x)
        return x

model = IrisModel()

    

5.4. Training the Model

After defining the loss function and optimization technique, we train the model.


# Define Loss Function and Optimization Technique
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)

# Train the Model
num_epochs = 100
for epoch in range(num_epochs):
    model.train()
    
    # Forward Pass
    outputs = model(X_train_tensor)
    loss = criterion(outputs, y_train_tensor)
    
    # Backward Pass and Optimization
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if (epoch+1) % 10 == 0:
        print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')

    

5.5. Evaluating the Model

Using the trained model, we perform predictions on the test data and evaluate the accuracy.


# Evaluate the Model
model.eval()
with torch.no_grad():
    test_outputs = model(X_test_tensor)
    _, predicted = torch.max(test_outputs.data, 1)
    accuracy = (predicted == y_test_tensor).sum().item() / y_test_tensor.size(0)
    print(f'Accuracy: {accuracy:.2f}')

    

6. Conclusion

In this tutorial, we explored the basic concepts of machine learning and the process of building a simple machine learning model using PyTorch. Machine learning is utilized in various fields, and PyTorch serves as a powerful tool for this purpose. We hope you will conduct in-depth research on a wider range of topics in the future.

We wish the advancements in deep learning and machine learning will aid your research!

Deep Learning PyTorch Course, Machine Learning Learning Algorithms

Today, artificial intelligence (AI) and machine learning (ML) play a crucial role in various industries and research fields. In particular, deep learning has established itself as a powerful tool for learning and predicting complex data patterns. PyTorch is an open-source deep learning library that helps build these deep learning models easily and intuitively. In this course, we will closely examine the basic concepts and implementation methods of machine learning algorithms using PyTorch.

1. Overview of Machine Learning

Machine learning is a collection of algorithms that analyze and learn from data to make predictions or decisions. One important classification of machine learning is supervised learning. In supervised learning, input and corresponding correct answers (labels) are provided to train the model. Here, we will explain linear regression, a representative machine learning algorithm, as an example.

2. Linear Regression

Linear regression is a method for modeling the linear relationship between input features and outputs. Mathematically, it is expressed as follows:

y = wx + b

Here, y represents the predicted value, w represents the weight, x represents the input value, and b represents the bias. The goal during the learning process is to find the optimal w and b. To do this, a loss function is defined and minimized. Generally, Mean Squared Error (MSE) is used.

2.1. Implementing Linear Regression with PyTorch


import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt

# Data generation
np.random.seed(42)
x_numpy = np.random.rand(100, 1) * 10  # Random numbers from 0 to 10
y_numpy = 2.5 * x_numpy + np.random.randn(100, 1)  # y = 2.5x + noise

# Convert NumPy arrays to PyTorch tensors
x_train = torch.FloatTensor(x_numpy)
y_train = torch.FloatTensor(y_numpy)

# Define linear regression model
model = nn.Linear(1, 1)

# Define loss function and optimizer
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Learning process
num_epochs = 100
for epoch in range(num_epochs):
    model.train()

    # Calculate predicted values
    y_pred = model(x_train)

    # Calculate loss
    loss = criterion(y_pred, y_train)

    # Print elapsed loss
    if (epoch + 1) % 10 == 0:
        print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}')

    # Initialize gradients, backpropagate and update weights
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

# Visualizing predictions
plt.scatter(x_numpy, y_numpy, label='Data')
plt.plot(x_numpy, model(x_train).detach().numpy(), color='red', label='Prediction')
plt.legend()
plt.show()
    

The code above creates a linear regression model, trains it based on data, and finally visualizes the prediction results. As learning progresses, the loss decreases. This indicates that the model is successfully learning the patterns in the data.

3. Deep Neural Networks

In deep learning, multiple layers of artificial neural networks are used to learn more complex data patterns. Such deep learning models can be implemented using a simple Multi-Layer Perceptron (MLP) structure. An MLP consists of an input layer, hidden layers, and an output layer, with each layer made up of nodes. Each node is connected to the nodes of the previous layer, and nonlinearity is introduced through an activation function.

3.1. Implementing an MLP Model


class NeuralNetwork(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(NeuralNetwork, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)  # First hidden layer
        self.fc2 = nn.Linear(hidden_size, output_size)  # Output layer
        self.relu = nn.ReLU()  # Activation function

    def forward(self, x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out

# Prepare dataset
from sklearn.datasets import make_moons
x, y = make_moons(n_samples=1000, noise=0.2)

# Convert NumPy arrays to PyTorch tensors
x_train = torch.FloatTensor(x)
y_train = torch.FloatTensor(y).view(-1, 1)

# Define model, loss function, and optimizer
input_size = 2
hidden_size = 10
output_size = 1

model = NeuralNetwork(input_size, hidden_size, output_size)
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Learning process
num_epochs = 1000
for epoch in range(num_epochs):
    model.train()

    # Calculate predicted values
    y_pred = model(x_train)

    # Calculate loss
    loss = criterion(y_pred, y_train)

    # Initialize gradients, backpropagate and update weights
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    # Print elapsed loss
    if (epoch + 1) % 100 == 0:
        print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}')
    

The code above defines a basic Multi-Layer Perceptron model and demonstrates how to train it on a ‘make_moons’ dataset containing 1000 samples. ‘BCEWithLogitsLoss’ is a commonly used loss function for binary classification. You can observe that the loss decreases as the model learns.

4. Convolutional Neural Networks (CNN)

CNNs are primarily used for 2D data such as images. CNNs are composed of convolutional layers and pooling layers, which are effective in extracting features from images. Convolutional layers capture local characteristics from images, while pooling layers reduce the size of images to decrease computational workload.

4.1. Implementing a CNN Model


class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)  # First convolutional layer
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)  # Pooling layer
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)  # Second convolutional layer
        self.fc1 = nn.Linear(64 * 7 * 7, 128)  # First fully connected layer
        self.fc2 = nn.Linear(128, 10)  # Output layer
        
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))  # First convolution + pooling
        x = self.pool(F.relu(self.conv2(x)))  # Second convolution + pooling
        x = x.view(-1, 64 * 7 * 7)  # Flatten
        x = F.relu(self.fc1(x))  # First fully connected layer
        x = self.fc2(x)  # Output layer
        return x

# Load example data (MNIST)
import torchvision.transforms as transforms
from torchvision import datasets

transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
train_dataset = datasets.MNIST(root='./data', train=True, transform=transform, download=True)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)

# Define model, loss function, and optimizer
model = CNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Learning process
num_epochs = 5
for epoch in range(num_epochs):
    for images, labels in train_loader:
        optimizer.zero_grad()  # Initialize gradients
        outputs = model(images)  # Calculate predicted values
        loss = criterion(outputs, labels)  # Calculate loss
        loss.backward()  # Backpropagate
        optimizer.step()  # Update weights

    print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}')
    

The code above constructs a simple CNN model and demonstrates training it on the MNIST dataset. CNNs effectively learn features of images through convolution and pooling operations.

5. Conclusion

In this course, we have implemented linear regression in machine learning and various deep learning models using PyTorch. Deep learning is very useful for learning complex data, and PyTorch is a valuable tool in that process. I hope you experiment with various models using PyTorch and gain a deeper understanding of the data.

6. Additional Resources

If you would like to know more, please refer to the following materials:

© 2023 Deep Learning Institute. All rights reserved.

Deep Learning PyTorch Course, Machine Learning Training Program

In this article, I will explain the basic usage of PyTorch for deep learning and the training process of machine learning models in detail.
From the basics of deep learning to advanced topics, I aim to help you learn systematically with practical examples.

1. What is PyTorch?

PyTorch is an open-source machine learning library based on Python, primarily used for research and development in deep learning.
PyTorch supports flexible neural network building and powerful GPU acceleration, enabling researchers and engineers to experiment and optimize quickly.

2. Installing PyTorch

To install PyTorch, you first need to have Python installed.
Then, you can use the following command to install PyTorch via pip:


pip install torch torchvision torchaudio

3. Basic Concepts of Deep Learning

Deep learning is a field of machine learning that utilizes artificial neural networks to automatically learn features from data.
The main concepts we will cover are as follows:

  • Neural Network
  • Backpropagation
  • Loss Function
  • Optimization

3.1 Neural Network

A neural network consists of an input layer, hidden layers, and an output layer.
Each layer is composed of nodes, and the connections between nodes have weights.
These weights are updated through the learning process.

3.2 Backpropagation

Backpropagation is a technique for adjusting weights by calculating the gradient from the loss function.
This helps improve the model’s predictions.

3.3 Loss Function

The loss function measures the difference between the model’s predictions and the actual values.
This function evaluates the model’s performance and indicates the directions for improvement during the optimization process.

3.4 Optimization

Optimization is the process of minimizing the loss function,
applying lightweight architectures and efficient learning techniques to improve the model’s accuracy.

4. Building a Basic Neural Network Model with PyTorch

Let’s actually build a simple neural network model using PyTorch.

4.1 Preparing the Dataset

First, we will create a model to classify handwritten digits using the MNIST dataset.
The MNIST dataset contains images of digits from 0 to 9.
We can use the datasets provided by torchvision in PyTorch.


import torch
import torchvision
import torchvision.transforms as transforms

# Load MNIST dataset
transform = transforms.Compose([
    transforms.ToTensor(), 
    transforms.Normalize((0.5,), (0.5,))
])

trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

testset = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False)

4.2 Defining the Neural Network Model

The process of defining a neural network model is as follows:


import torch.nn as nn
import torch.nn.functional as F

# Define the neural network
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(28 * 28, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = x.view(-1, 28 * 28)  # Flatten the input
        x = F.relu(self.fc1(x))  # Apply ReLU activation
        x = self.fc2(x)           # Output layer
        return x

4.3 Setting Up the Loss Function and Optimizer

We set up the loss function and optimizer for training the model:


import torch.optim as optim

# Create the model
model = Net()

# Set the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

4.4 Training the Model

Now it’s time to train the model. We will set the number of epochs and write the code to update the model’s weights for each batch:


for epoch in range(5):  # Train for 5 epochs
    for inputs, labels in trainloader:
        optimizer.zero_grad()   # Initialize gradients
        outputs = model(inputs) # Model prediction
        loss = criterion(outputs, labels) # Calculate loss
        loss.backward()         # Compute gradients
        optimizer.step()        # Update weights

    print(f'Epoch {epoch + 1}, Loss: {loss.item():.4f}')

4.5 Evaluating the Model

Let’s evaluate how well the model has learned.
We can calculate the accuracy using the test dataset:


correct = 0
total = 0
with torch.no_grad():
    for inputs, labels in testloader:
        outputs = model(inputs)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print(f'Accuracy: {100 * correct / total:.2f}%')

5. Conclusion

In this article, we explored the basic concepts of deep learning and the training process of models using PyTorch.
We learned how to build a simple neural network model and how to confirm the model’s performance through training and evaluation.
To advance further, it would be beneficial to study deep learning architectures, various optimization techniques, and hyperparameter tuning.

Deep Learning PyTorch Course, Logistic Regression and Linear Regression

In this course, we will explore two important regression analysis techniques, logistic regression and linear regression, which are fundamental concepts in deep learning. In this process, we will implement both models using PyTorch and examine how each technique is utilized.

Table of Contents

  1. 1. Linear Regression
  2. 2. Logistic Regression
  3. 3. Conclusion

1. Linear Regression

Linear regression is a statistical method that models the relationship between input variables and output variables using a straight line. It performs predictions by finding the optimal line for a given set of data points.

1.1 Linear Regression Model Equation

The linear regression model can be represented by the following equation:

y = β0 + β1*x1 + β2*x2 + ... + βn*xn

Here, y is the predicted value, β0 is the intercept, and β1, β2, ..., βn are the regression coefficients. These regression coefficients are learned from the data.

1.2 Implementing Linear Regression in PyTorch

Now, let’s implement the linear regression model using PyTorch. We will create a simple linear regression model with the code below.


import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt

# Generate data
x_data = np.array([[1], [2], [3], [4], [5]])
y_data = np.array([[2], [3], [5], [7], [11]])

# Convert to tensor
x_tensor = torch.Tensor(x_data)
y_tensor = torch.Tensor(y_data)

# Define linear regression model
model = nn.Linear(1, 1)

# Define loss function and optimizer
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Train the model
for epoch in range(100):
    model.train()
    
    optimizer.zero_grad()
    y_pred = model(x_tensor)
    loss = criterion(y_pred, y_tensor)
    loss.backward()
    optimizer.step()

# Visualize the results
plt.scatter(x_data, y_data, color='blue', label='Actual Data')
plt.plot(x_data, model(x_tensor).detach().numpy(), color='red', label='Regression Line')
plt.legend()
plt.title('Linear Regression Prediction')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

The code above is an example of training a linear regression model using a simple dataset. After training the model, we can visualize and compare the actual data with the predicted values.

2. Logistic Regression

Logistic regression is a linear model designed to handle classification problems. It is primarily used in binary classification, where it applies a sigmoid function (logistic function) to the linear combination of inputs, converting the output into a probability value between 0 and 1.

2.1 Logistic Regression Model Equation

The logistic regression model can be represented by the following equations:

y = 1 / (1 + e^(-z))
z = β0 + β1*x1 + β2*x2 + ... + βn*xn

Here, z is the linear combination, and y is the probability of the class.

2.2 Implementing Logistic Regression in PyTorch

Now, let’s implement the logistic regression model using PyTorch. The code below is an example solving a simple binary classification problem.


# Generate data (binary classification)
from sklearn.datasets import make_classification
import torch.nn.functional as F

# Create binary classification dataset
X, y = make_classification(n_samples=100, n_features=2, n_classes=2, n_informative=2, n_redundant=0, random_state=42)
X_tensor = torch.Tensor(X)
y_tensor = torch.Tensor(y).view(-1, 1)

# Define logistic regression model
class LogisticRegressionModel(nn.Module):
    def __init__(self):
        super(LogisticRegressionModel, self).__init__()
        self.linear = nn.Linear(2, 1)

    def forward(self, x):
        return torch.sigmoid(self.linear(x))

# Define model, loss function, and optimizer
model = LogisticRegressionModel()
criterion = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Train the model
for epoch in range(100):
    model.train()
    
    optimizer.zero_grad()
    y_pred = model(X_tensor)
    
    loss = criterion(y_pred, y_tensor)
    loss.backward()
    optimizer.step()

# Predictions
model.eval()
with torch.no_grad():
    y_pred = model(X_tensor)

# Visualize predictions
predicted_classes = (y_pred.numpy() > 0.5).astype(int)
plt.scatter(X[y[:, 0] == 0][:, 0], X[y[:, 0] == 0][:, 1], color='blue', label='Class 0')
plt.scatter(X[y[:, 0] == 1][:, 0], X[y[:, 0] == 1][:, 1], color='red', label='Class 1')
plt.scatter(X[predicted_classes[:, 0] == 1][:, 0], X[predicted_classes[:, 0] == 1][:, 1], color='green', label='Predicted')
plt.legend()
plt.title('Logistic Regression Prediction')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.show()

The code above is an example of training a logistic regression model to solve a simple binary classification problem. After training the model, we can visualize and compare the actual classes with the predicted classes.

3. Conclusion

In this course, we covered logistic regression and linear regression. Both models were implemented using PyTorch with a simple dataset, highlighting the differences in the types of problems they address and their significance. These techniques form the foundation of machine learning and deep learning and are widely used in solving real-world problems. I hope this course broadens your understanding.