Deep Learning PyTorch Course, What is Deep Learning

Deep learning is a field of artificial intelligence (AI) and machine learning (ML) that uses algorithms to learn features from data by mimicking the structure of the human brain. The focus is on enabling computers to recognize and make judgments similarly to humans through this learning process.

1. History of Deep Learning

The concept of deep learning dates back to the 1940s and 1950s. During this period, a neural network technique called the Perceptron was proposed, which was one of the simple models that machines could learn from. However, due to initial limitations, deep learning did not receive much attention for a while.

As the 1990s approached, advancements in multi-layer perceptrons and backpropagation algorithms occurred. After 2000, deep learning began to gain attention once again as the amount of data exploded and advancements in GPUs were made. In particular, the popularity of deep learning surged when AlexNet was introduced at the ImageNet competition in 2012.

2. Basic Concepts of Deep Learning

Deep learning uses artificial neural networks composed of multiple layers. The nodes in each layer transform the features of the input data and pass them to the next layer. The output from the final output layer is used as the prediction.

2.1 Structure of Artificial Neural Networks

Artificial neural networks have the following basic structure:

  • Input Layer: The layer where the model receives data.
  • Hidden Layer: Located between the input layer and output layer, it performs various functions.
  • Output Layer: Generates the final results of the model.

2.2 Activation Function

An activation function is a function that introduces non-linearity to the results computed at each node before passing them to the next layer. Common activation functions include:

  • Sigmoid: The output range is between 0 and 1.
  • ReLU (Rectified Linear Unit): Values less than 0 are converted to 0, and the remaining values are output as they are.
  • Softmax: Primarily used for multi-class classification problems.

3. Introduction to PyTorch

PyTorch is a widely used open-source library for implementing deep learning models. It is suitable for both research and production, featuring powerful flexibility and dynamic computation graphs. Additionally, due to its excellent compatibility with Python, it is favored by many researchers and developers.

3.1 Advantages of PyTorch

  • Dynamic Computation Graph: Allows for changes to the network structure during training, making experimentation and adjustments easier.
  • Flexible Tensor Operations: Tensors can be easily used in a manner similar to NumPy.
  • Rich Community: Many users and a variety of tutorials and examples are available.

4. Example of Image Classification using Deep Learning

Now let’s implement a deep learning model using PyTorch through a simple example. In this example, we will create a model to classify handwritten digits using the MNIST dataset.

4.1 Installing Required Libraries

    
    pip install torch torchvision
    
    

4.2 Preparing the Dataset

The MNIST dataset consists of images of handwritten digits. The following code can be used to load the dataset.

    
    import torch
    from torchvision import datasets, transforms

    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5,), (0.5,))
    ])

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

4.3 Defining the Model

Next, we define a simple artificial neural network model.

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

    class SimpleNN(nn.Module):
        def __init__(self):
            super(SimpleNN, self).__init__()
            self.fc1 = nn.Linear(28 * 28, 128)
            self.fc2 = nn.Linear(128, 64)
            self.fc3 = nn.Linear(64, 10)

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

    model = SimpleNN()
    
    

4.4 Defining the Loss Function and Optimizer

To compute and update the loss of the model, we define the loss function and optimizer.

    
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
    
    

4.5 Training the Model

To train the model, we define the training loop.

    
    epochs = 5
    for epoch in range(epochs):
        for images, labels in trainloader:
            optimizer.zero_grad()  # Zero the gradients
            output = model(images)  # Forward pass
            loss = criterion(output, labels)  # Calculate loss
            loss.backward()  # Backward pass
            optimizer.step()  # Update weights

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

4.6 Evaluating the Model

To evaluate the performance of the model, we can use the test dataset.

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

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

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

5. Conclusion

Deep learning is bringing innovation across many fields, and PyTorch is a very powerful tool for its implementation. In this course, we covered the basic concepts of deep learning and implemented a simple model using PyTorch. I hope to build skills through more diverse projects in the future.

Deep Learning PyTorch Course, Problems and Solutions of Deep Learning

Deep Learning is a field of Artificial Intelligence and Machine Learning that involves learning patterns from data to create predictive models. In recent years, it has gained attention in various fields due to the advancements in big data and computing power, particularly in areas like computer vision, natural language processing, and speech recognition. However, deep learning models can encounter several issues during the design and training processes. This document will explore the main issues in deep learning, potential solutions, and example code utilizing PyTorch.

1. Issues in Deep Learning

1.1. Overfitting

Overfitting refers to the phenomenon where a model fits the training data too well, resulting in a decrease in generalization performance for new data. This typically occurs when the data is insufficient or the model is too complex.

1.2. Data Imbalance

In classification problems where the number of data points is imbalanced across classes, the model may only fit well to the class with abundant data, potentially leading to poor performance on the class with fewer data points.

1.3. Learning Rate and Convergence Issues

Choosing an appropriate learning rate is crucial for model training. If the learning rate is too high, the loss function may diverge, while a learning rate that is too low can slow down convergence, making training inefficient.

1.4. Lack of Interpretability

Deep learning models are often seen as black box models, which makes it difficult to interpret their internal operations or prediction results, causing trust issues in fields such as business and healthcare.

1.5. Resource Consumption

Training large-scale models requires significant computational resources and memory, leading to economic costs and energy consumption issues.

2. Solutions to Issues

2.1. Methods to Prevent Overfitting

Various methods are used to prevent overfitting. Some of these include:

  • Regularization: Using L1 and L2 regularization techniques to reduce model complexity.
  • Dropout: Randomly omitting certain neurons during training to prevent the model from becoming overly reliant on specific neurons.
  • Early Stopping: Stopping training when performance on validation data starts to decrease.

2.2. Solutions to Data Imbalance

Techniques to address data imbalance may include:

  • Resampling: Oversampling the class with fewer data or undersampling the class with more data.
  • Cost-sensitive Learning: Training the model to assign higher costs to errors in specific classes.
  • SMOTE (Synthetic Minority Over-sampling Technique): Synthesizing samples of the minority class to increase the volume of data.

2.3. Improving Learning Speed and Optimization

To speed up learning, adaptive learning rate algorithms (e.g., Adam, RMSProp) can be used, as well as batch normalization to stabilize training.

2.4. Ensuring Interpretability

Techniques such as LIME and SHAP can be used to provide interpretations of model predictions, enhancing model interpretability.

2.5. Increasing Resource Efficiency

Model compression or lightweight networks (e.g., MobileNet, SqueezeNet) can be employed to reduce model size and decrease execution time.

3. PyTorch Example

Below is an example of building and training a simple neural network using PyTorch. This example implements a model that classifies handwritten digits from the MNIST dataset.

3.1. Importing Required Libraries


import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torchvision import datasets
from torch.utils.data import DataLoader
    

3.2. Setting Hyperparameters


# Setting hyperparameters
batch_size = 64
learning_rate = 0.001
num_epochs = 5
    

3.3. Preparing Data


# Preparing the dataset
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
    

3.4. Defining the Model


# Defining the neural network model
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(28 * 28, 128)  # Input layer
        self.fc2 = nn.Linear(128, 64)        # Hidden layer
        self.fc3 = nn.Linear(64, 10)         # Output layer

    def forward(self, x):
        x = x.view(-1, 28 * 28)  # Flatten
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x

model = SimpleNN()
    

3.5. Setting Loss Function and Optimizer


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

3.6. Training the Model


# Training the model
for epoch in range(num_epochs):
    for images, labels in train_loader:
        optimizer.zero_grad()  # Reset gradients
        outputs = model(images)  # Predictions
        loss = criterion(outputs, labels)  # Calculate loss
        loss.backward()  # Backpropagation
        optimizer.step()  # Update weights
    print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
    

3.7. Evaluating the Model


# Evaluating the model
model.eval()  # Switch to evaluation mode
correct = 0
total = 0
with torch.no_grad():
    for images, labels in test_loader:
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print(f'Accuracy of the model on the test images: {100 * correct / total:.2f}%')
    

3.8. Conclusion

In this tutorial, we discussed various issues and solutions related to deep learning, and implemented a simple neural network model using PyTorch. To successfully operate deep learning models, it is essential to understand the characteristics of the problem and to appropriately combine various techniques to derive the optimal model.

As deep learning technology continues to evolve, it is expected to become more integrated into our lives. Continuous research and application are essential, and we hope that many developers will tackle various challenges in this process.

Deep Learning PyTorch Course, Advantages of Using Deep Learning

Deep learning is a field of machine learning, which models and predicts data through artificial neural networks. Having achieved innovative advancements in many areas, deep learning shows excellent performance, particularly in image recognition, natural language processing, and recommendation systems. This course will cover the concepts and advantages of deep learning in detail using PyTorch.

1. Basic Concepts of Deep Learning

Deep learning uses artificial neural networks composed of multiple layers to learn data characteristics. In this process, the algorithm learns the relationship between input data and the correct label. The main components of deep learning are as follows:

  • Neural Network Structure: Consists of an input layer, hidden layers, and an output layer.
  • Activation Function: A function that determines the output of a neuron, with various forms such as Sigmoid and ReLU.
  • Loss Function: Measures the difference between the model’s predictions and the actual values, and learning occurs in the direction that minimizes this difference.
  • Optimization Algorithm: A method for updating weights, such as Gradient Descent.

2. What is PyTorch?

PyTorch is a flexible and powerful deep learning framework developed by Facebook. PyTorch supports dynamic computation graphs, which provides the advantage of intuitively constructing and debugging models. It also offers APIs that make it easy to define various neural network components, making it popular among both researchers and developers.

2.1 Key Features of PyTorch

  • Ease of Use: The Pythonic syntax allows for intuitive code writing.
  • Dynamic Computation Graph: The graph can change at runtime, making it easy to handle iterative tasks or conditionals.
  • GPU Acceleration: With CUDA support for GPUs, execution speed is fast for large datasets and complex models.

3. Advantages of Using Deep Learning

Deep learning offers several advantages over traditional machine learning algorithms. The main advantages are:

3.1 Non-linear Data Processing

Deep learning is effective in processing non-linear data through multi-layer neural networks. For example, in image recognition problems, even if the background or lighting varies, a deep learning model can identify specific objects.

3.2 Automatic Feature Extraction

In traditional methods, experts had to manually extract features, but deep learning automatically learns features to improve performance. For instance, when using image data, it is possible to generate advanced features with a small number of layers.

3.3 Large-scale Data Processing

Deep learning excels at processing massive amounts of data. As the amount of training data increases, the generalization performance of the system improves. This is particularly important in large-scale applications such as recommendation systems and natural language processing.

3.4 Flexible Architecture Design

PyTorch makes it easy to design custom architectures, allowing for the handling of various problems. For example, users can customize layers, number of neurons, and experiment with different models.

4. PyTorch Example Code

Below is an example of implementing a simple neural network model using PyTorch. This example performs digit classification using the MNIST dataset.

4.1 Installing Required Libraries

!pip install torch torchvision

4.2 Downloading and Preprocessing the MNIST Dataset

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms

# Data preprocessing
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))
])

# Data loading
train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)

train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=64, shuffle=False)

4.3 Defining the Neural Network Model

# Define neural network class
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(28 * 28, 128)  # Input layer
        self.fc2 = nn.Linear(128, 64)        # Hidden layer
        self.fc3 = nn.Linear(64, 10)         # Output layer

    def forward(self, x):
        x = x.view(-1, 28 * 28)  # Flatten image
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x

model = SimpleNN()

4.4 Defining the Loss Function and Optimizer

criterion = nn.CrossEntropyLoss()  # Loss function
optimizer = optim.Adam(model.parameters(), lr=0.001)  # Optimizer

4.5 Training the Model

for epoch in range(5):  # Train for 5 epochs
    for data, target in train_loader:
        optimizer.zero_grad()  # Reset gradients
        output = model(data)    # Prediction
        loss = criterion(output, target)  # Calculate loss
        loss.backward()  # Calculate gradients
        optimizer.step()  # Update weights

    print(f'Epoch {epoch+1} completed.')

4.6 Evaluating the Model

correct = 0
total = 0
with torch.no_grad():
    for data, target in test_loader:
        output = model(data)
        _, predicted = torch.max(output.data, 1)  # Index of maximum value
        total += target.size(0)
        correct += (predicted == target).sum().item()

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

5. Conclusion

Deep learning is a very powerful tool, and PyTorch is an excellent framework for it. Through non-linear data processing, automatic feature extraction, large-scale data processing, and flexible structure design, various phenomena and problems can be addressed. In this course, we explained the basic usage of PyTorch and the advantages of deep learning through a simple example. Advanced courses covering more developed models and technologies will also be prepared in the future. We appreciate your interest!

Deep Learning PyTorch Course, Deep Learning Training Algorithms

Deep learning is a field of machine learning based on artificial neural networks, which is used to learn patterns from data and perform tasks such as prediction or classification. In this course, we will explain the basic concepts of deep learning along with the learning algorithms using a deep learning framework called PyTorch.

Basic Concepts of Deep Learning

The core of deep learning is neural networks. A neural network is a structure composed of units called nodes that are connected in layers, receiving input data and applying weights and biases to generate output data.
Each node performs a nonlinear transformation, which is accomplished through an activation function.

Structure of Neural Networks

Generally, neural networks consist of an input layer, hidden layers, and an output layer.

  • Input Layer: The place where the model receives data
  • Hidden Layers: Internal layers that process the input data, which can have multiple layers
  • Output Layer: The layer that outputs the final prediction value or class

Activation Functions

Activation functions play the role of introducing non-linearity in nodes. Here are the activation functions commonly used.

  • ReLU (Rectified Linear Unit): $f(x) = max(0, x)$
  • Sigmoid: $f(x) = \frac{1}{1 + e^{-x}}$
  • Tanh: $f(x) = \tanh(x) = \frac{e^{x} – e^{-x}}{e^{x} + e^{-x}}$

Deep Learning Learning Algorithms

To train a deep learning model, a dataset is required. The data consists of inputs and targets (outputs).
The learning process of the model proceeds through the following steps.

1. Forward Pass

The input data is passed through the model to compute the predicted values. At this time, the weights and biases of the neural network are used to generate the output.

2. Loss Calculation

The loss is calculated as the difference between the model’s predictions and the actual target values. Common loss functions include Mean Squared Error (MSE) and Cross-Entropy.

3. Backpropagation

This process adjusts weights and biases based on the loss, using Gradient Descent to update the model’s parameters. The backpropagation algorithm calculates the gradient of the loss for each weight using the chain rule.

4. Weight Update

The calculated gradients are used to update the weights and biases. The update formula is as follows.

w = w - learning_rate * gradient
b = b - learning_rate * gradient

Implementation in PyTorch

Now, based on the explanations above, let’s implement a simple deep learning model in PyTorch. This example uses the MNIST handwritten digit recognition dataset to classify handwritten digits.

Install and Import Required Libraries

pip install torch torchvision
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

Load and Preprocess Dataset

Load the MNIST dataset and perform normalization on the image data.

# Data Preprocessing
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))
])

# Load Dataset
train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)

# Create Data Loaders
train_loader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=64, shuffle=False)

Define the Model

Define a simple neural network model. The input size is 28×28 (MNIST image size), and it has two hidden layers. The output layer is set to 10 (digits 0 to 9).

class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(28 * 28, 128)  # Input layer -> Hidden layer
        self.fc2 = nn.Linear(128, 64)        # Hidden layer -> Hidden layer
        self.fc3 = nn.Linear(64, 10)         # Hidden layer -> Output layer
        self.activation = nn.ReLU()          # Activation function

    def forward(self, x):
        x = x.view(-1, 28 * 28)              # Reshape image to 1D tensor
        x = self.activation(self.fc1(x))     # Forward pass
        x = self.activation(self.fc2(x))
        x = self.fc3(x)
        return x

Initialize the Model and Set Loss Function and Optimizer

# Initialize Model
model = SimpleNN()

# Set Loss Function and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

Train the Model

Train the model iteratively while recording the loss and periodically evaluate the model’s performance.

# Train the Model
num_epochs = 5

for epoch in range(num_epochs):
    for images, labels in train_loader:
        optimizer.zero_grad()                 # Initialize gradients
        outputs = model(images)               # Forward pass
        loss = criterion(outputs, labels)     # Calculate loss
        loss.backward()                       # Backpropagation
        optimizer.step()                      # Update weights

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

Evaluate the Model

Evaluate the accuracy of the model using the test dataset.

# Evaluate the Model
model.eval()  # Set to evaluation mode
with torch.no_grad():  # Disable gradient calculation
    correct = 0
    total = 0
    for images, labels in test_loader:
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)  # Predicted classes
        total += labels.size(0)                     # Total sample count
        correct += (predicted == labels).sum().item()  # Count correct predictions

print(f'Accuracy of the model on the test images: {100 * correct / total:.2f}%')

Conclusion

In this course, we covered the basic concepts of deep learning and implemented a simple neural network model using PyTorch. Through hands-on practice, we learned about data preprocessing, model definition, training, and evaluation processes.
This provided an opportunity to gain a deep understanding of how deep learning works.
Furthermore, we can explore the world of deep learning by dealing with complex architectures, advanced optimization techniques, and various datasets.

Thank you!

Deep Learning PyTorch Course, Deep Learning Learning Process

Deep learning is a branch of artificial intelligence and a collection of machine learning methods based on artificial neural networks. One of the core technologies of deep learning widely used in various fields today is PyTorch. PyTorch is popular among many researchers and developers for its easy-to-use dynamic computation graph and powerful tensor operation capabilities. In this article, we will take a detailed look at the learning process of deep learning using PyTorch.

1. Basics of Deep Learning

Deep learning is a method of analyzing and predicting data through artificial neural networks. An artificial neural network is a model that mimics the structure and function of biological neural networks, where each node represents a nerve cell and is connected to transmit information.

1.1 Structure of Artificial Neural Networks

Artificial neural networks mainly consist of an input layer, hidden layers, and an output layer:

  • Input Layer: The layer where data enters the neural network.
  • Hidden Layer: A layer that performs intermediate calculations, which can have one or more instances.
  • Output Layer: The layer that generates the final result of the neural network.

1.2 Activation Function

The activation function determines whether each neuron in the neural network will be activated. Commonly used activation functions include:

  • Sigmoid: $f(x) = \frac{1}{1 + e^{-x}}$
  • ReLU: $f(x) = max(0, x)$
  • Tanh: $f(x) = \tanh(x)$

2. Introduction to PyTorch

PyTorch is an open-source deep learning library developed by Facebook that works with Python and supports tensor operations, automatic differentiation, and GPU acceleration. The advantages of PyTorch include:

  • Support for dynamic computation graphs
  • Intuitive API and thorough documentation
  • Active community and various available examples

3. Deep Learning Learning Process

The deep learning learning process can be broadly divided into four stages: data preparation, model construction, training, and evaluation.

3.1 Data Preparation

To train a deep learning model, data must be prepared. This typically includes the following steps:

  • Data collection
  • Data preprocessing (normalization, sampling, etc.)
  • Separating the training set and testing set

3.2 Preparing Data in PyTorch

In PyTorch, packages like torchvision can be used to handle data. For example, the code to load the CIFAR-10 dataset is as follows:

import torch
import torchvision
import torchvision.transforms as transforms

transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)

3.3 Model Construction

When constructing a model, the structure of the neural network must be defined. In PyTorch, user-defined models can be created by inheriting the torch.nn.Module class. Below is an example of a simple CNN model:

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

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

3.4 Model Training

When training a model, a loss function and an optimization algorithm must be defined. Generally, the cross-entropy loss function is used for classification problems, and optimization algorithms such as SGD or Adam can be applied.

import torch.optim as optim

net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

for epoch in range(2):  # Repeating the dataset multiple times.
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data
        optimizer.zero_grad()  # Initialize gradients
        outputs = net(inputs)  # Forward pass
        loss = criterion(outputs, labels)  # Calculate loss
        loss.backward()  # Backward pass
        optimizer.step()  # Update weights

print('Finished Training')

3.5 Model Evaluation

After training the model, it needs to be evaluated. Typically, the testing dataset is used to calculate accuracy.

correct = 0
total = 0
with torch.no_grad():  # Disable gradient calculation
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 10000 test images: %d %%' % (
    100 * correct / total))

4. Directions for the Advancement of Deep Learning

Deep learning is being utilized in various fields and will continue to evolve. Especially, it is expected to bring innovations in many areas, including autonomous vehicles, medical diagnosis, natural language processing, and image generation. PyTorch will also continue to evolve in line with these trends.

Conclusion

In this article, we started with the basics of deep learning and took a detailed look at the learning process of deep learning using PyTorch. Through the stages of data preparation, model construction, training, and evaluation, we confirmed the various functions and conveniences provided by PyTorch. I hope this guide helps broaden your understanding of deep learning and aids in applying it to real projects.