Deep Learning PyTorch Course, The Necessity of Convolutional Layers

Deep learning is a field of machine learning that learns patterns from data through multiple layers of neurons. Various deep learning models exist, among which Convolutional Neural Networks (CNN) are architectures particularly suitable for image processing. In this course, we will understand the necessity and working principles of convolutional layers and implement them using PyTorch.

1. Concept of Convolutional Layer

A convolutional layer is designed to extract features from images, operating differently from a typical fully connected layer. The convolutional layer uses parameters called kernels or filters on the input image to learn local features of the image. In this process, it analyzes local regions and performs convolution with the filter.

1.1. Convolution Operation

The convolution operation is the process of sliding the kernel over the input image to extract local features. Specifically, when the kernel is positioned at a particular area of the image, it multiplies the pixel values of that area by the values of the kernel and sums the results to create a new pixel value.

1.2. Pooling Layer

After the convolution operation, the pooling layer is used to reduce dimensions and computational complexity while maintaining robust features against noise. Generally, maximum pooling or average pooling is used. Pooling emphasizes specific features of the image and further strengthens position invariance.

2. Necessity of Convolutional Layers

2.1. Reduction in Number of Parameters

In fully connected layers, every input node is connected to every output node, resulting in a rapid increase in the number of parameters. In contrast, convolutional layers only require parameters equal to the size of the kernel (e.g., 3×3), allowing for effective feature extraction with significantly fewer parameters compared to fully connected layers.

2.2. Extraction of Local Features

Images primarily possess local information. For example, if a particular local area of the image contains a characteristic object, it is crucial to extract the features of that area accurately. Convolutional layers learn such local patterns well, enabling precise predictions.

2.3. Position Invariance

The features learned through convolutional and pooling layers are independent of their location within the image. In other words, regardless of where an object is located in the image, the features can be recognized effectively. This becomes a significant advantage in tasks such as image classification.

2.4. Diverse Application Fields

Convolutional layers can be applied across various fields such as image classification, object detection, image generation, and even natural language processing. Despite the rapid advancement of artificial intelligence, the fundamental structure of CNNs remains a core component in many modern models.

3. Implementing Convolutional Layers in PyTorch

Now, let’s implement a simple CNN using PyTorch. Below is an example of a CNN model that includes basic convolutional layers, pooling layers, and fully connected layers.

import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torch.optim as optim

# Define CNN model
class SimpleCNN(nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1)  # Convolutional Layer
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)  # Pooling Layer
        self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
        self.fc1 = nn.Linear(64 * 7 * 7, 128)  # 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 and preprocess dataset
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))  # Normalization
])
trainset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

# Define model, loss function, and optimization algorithm
model = SimpleCNN()
criterion = nn.CrossEntropyLoss()  # Loss function
optimizer = optim.Adam(model.parameters(), lr=0.001)  # Optimization Algorithm

# Training loop
for epoch in range(10):  # 10 epochs
    for inputs, labels in trainloader:
        optimizer.zero_grad()  # Initialize gradient
        outputs = model(inputs)  # Forward pass
        loss = criterion(outputs, labels)  # Calculate loss
        loss.backward()  # Backward pass
        optimizer.step()  # Update weights

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

print('Training completed!')

3.1. Code Explanation

In the above code, we defined the SimpleCNN class and designed a CNN model composed of two convolutional layers and two fully connected layers. The convolutional layer is defined using torch.nn.Conv2d and the pooling layer is set up through torch.nn.MaxPool2d. The training process used the MNIST dataset and trained the model over 10 epochs.

4. Conclusion

Convolutional layers play a crucial role in effectively extracting significant features from image data. Understanding the structure and operating principles of basic convolutional neural networks is important in the field of deep learning. In this article, we explored the necessity of convolutional layers, their functions, and a simple implementation example using PyTorch. We hope to explore more complex CNN architectures and various application fields in the future.

5. References

The materials referenced in this course are as follows:

Deep Learning PyTorch Course, Introduction to Convolutional Neural Networks

Deep learning has established itself as a dominant methodology in the fields of artificial intelligence and machine learning in recent years. Today, we will take a look at Convolutional Neural Networks (CNNs). CNNs are particularly effective for image recognition and processing, and they are widely used across various industries.

What is a Convolutional Neural Network?

A Convolutional Neural Network is a type of neural network specialized in recognizing visual patterns in given data, such as photos or videos. CNNs are fundamentally composed of convolutional layers, pooling layers, and fully connected layers.

Convolutional Layer

The convolutional layer is responsible for extracting features from the input data. This layer uses small filters (kernels) to perform operations on specific parts of the input image to generate output. The resulting feature map contains only the useful information from the input data.

Pooling Layer

The pooling layer is used to reduce the size of the feature map. This helps to reduce model complexity and computational load, preventing overfitting. The most common method is max pooling, which reduces the size of the feature map by selecting the largest value from a given area.

Fully Connected Layer

At the end of the neural network, there is a fully connected layer. This layer makes the final predictions based on the information obtained from the previous layers. Since all neurons are connected to the previous layer, it can make complex decisions regarding the input data.

Implementing CNN with PyTorch

Now, let’s implement a simple CNN model using PyTorch. We will create a model to classify handwritten digits using the MNIST dataset.

Preparation

First, we will install the necessary libraries and download the dataset. The following libraries are required:

pip install torch torchvision

Preparing the Dataset

We will download and load the MNIST dataset. You can use the code below to prepare the training and testing datasets.


import torch
import torchvision
import torchvision.transforms as transforms

# Define data transformations
transform = transforms.Compose(
    [transforms.ToTensor(), 
     transforms.Normalize((0.5,), (0.5,))])  # Normalization using mean and standard deviation

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

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

Defining the Model

Now, let’s define the convolutional neural network model. CNN models are typically designed with a structure that combines convolutional layers and pooling layers.


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

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3)  # Input channels 1, output channels 32
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)  # Max pooling
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3)  # Input channels 32, output channels 64
        self.fc1 = nn.Linear(64 * 6 * 6, 128)  # Fully connected layer, 64x6x6 is the output size
        self.fc2 = nn.Linear(128, 10)  # Final output 10 classes (0-9)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))  # First convolution and pooling
        x = self.pool(F.relu(self.conv2(x)))  # Second convolution and pooling
        x = x.view(-1, 64 * 6 * 6)  # Change tensor shape
        x = F.relu(self.fc1(x))  # First fully connected layer
        x = self.fc2(x)  # Second fully connected layer
        return x

Training the Model

To train the model, we need to define a loss function and an optimizer, and iteratively train on the data.


# Initialize the model
cnn = CNN()
criterion = nn.CrossEntropyLoss()  # Loss function
optimizer = torch.optim.SGD(cnn.parameters(), lr=0.01)  # Stochastic Gradient Descent

# Model training
for epoch in range(5):  # Number of epochs
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data
        optimizer.zero_grad()  # Zero the gradients
        outputs = cnn(inputs)  # Prediction
        loss = criterion(outputs, labels)  # Loss calculation
        loss.backward()  # Gradient calculation
        optimizer.step()  # Parameter update
        running_loss += loss.item()
    print(f'Epoch {epoch + 1}, Loss: {running_loss / len(trainloader)}')

Evaluating the Model

After training is complete, we evaluate the model’s performance using the test dataset.


correct = 0
total = 0

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

print(f'Accuracy on the test set: {100 * correct / total} %')

Conclusion

We have now experienced the process of constructing a simple convolutional neural network using PyTorch and training and evaluating it on a real dataset. We hope this tutorial has helped you understand the basic structure of deep learning and practical implementation using Python. Challenge yourself to tackle more complex models and diverse datasets in the future!

References

Deep Learning PyTorch Course, Convolutional & Deconvolutional Networks

Deep learning technology has achieved innovative results in computer vision, natural language processing, and various fields. In this course, we will take an in-depth look at Convolutional Neural Networks (CNN) and Deconvolutional Neural Networks (or Transpose Convolutional Networks) using PyTorch.

1. Introduction to Convolutional Neural Networks (CNN)

Convolutional Neural Networks (CNN) are a deep learning technology that demonstrates superior performance primarily in image recognition and processing. CNNs use specialized layers known as convolutional layers to process input images. These layers extract features by leveraging the spatial structure of the images.

1.1 How Convolutional Layers Work

Convolutional layers perform convolution operations with filters (or kernels) over the input image. Filters are small matrices that detect specific features in images, and multiple filters are used to extract various features. Typically, filters are updated during the learning process.

1.2 Convolution Operations

The convolution operation is performed by sliding the filter over the input image. It can be expressed by the following formula:

Convolution Operation

Here, \(Y\) is the output, \(X\) is the input image, \(K\) is the filter, and \(M\) and \(N\) are the dimensions of the filter.

1.3 Activation Functions

After the convolution operation, an activation function is applied to introduce non-linearity. The ReLU (Rectified Linear Unit) function is primarily used:

ReLU Function

2. Implementing CNN in PyTorch

Now, let’s explore how to implement a CNN using PyTorch. Below is an example of a basic CNN structure.

2.1 Preparing the Dataset

We will use the MNIST dataset. MNIST is a dataset consisting of handwritten digit images, which is suitable for testing basic image processing models.


import torch
import torchvision
import torchvision.transforms as transforms

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

# Download MNIST dataset
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)
    

2.2 Defining the CNN Model

The code for defining the CNN structure is as follows. It includes convolutional layers, fully connected layers, and activation functions.


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

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 convolution layer
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)  # Max pooling layer
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)  # Second convolution layer
        self.fc1 = nn.Linear(64 * 7 * 7, 128)  # First fully connected layer
        self.fc2 = nn.Linear(128, 10)  # Second fully connected layer

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))  # Convolution -> Activation -> Pooling
        x = self.pool(F.relu(self.conv2(x)))  # Convolution -> Activation -> Pooling
        x = x.view(-1, 64 * 7 * 7)  # Reshape tensor
        x = F.relu(self.fc1(x))  # Fully connected -> Activation
        x = self.fc2(x)  # Output layer
        return x
    

2.3 Training the Model

To train the model, we will define the loss function and optimizer.


import torch.optim as optim

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = CNN().to(device)
criterion = nn.CrossEntropyLoss()  # Loss function
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)  # SGD optimizer

# Training the model
for epoch in range(10):  # 10 epochs
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data[0].to(device), data[1].to(device)
        
        # Zero the gradients
        optimizer.zero_grad()
        
        # Forward pass + backward pass + optimization
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        
        running_loss += loss.item()
        if i % 100 == 99:    # Print every 100 batches
            print(f'[Epoch {epoch + 1}, Batch {i + 1}] loss: {running_loss / 100:.3f}')
            running_loss = 0.0
    print("Epoch finished")
    

2.4 Evaluating the Model

We will evaluate the trained model and measure its accuracy.


correct = 0
total = 0

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

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

3. Introduction to Deconvolutional Neural Networks

Deconvolutional Neural Networks, or Transpose Convolutional Networks, are structures that reconstruct images after feature extraction from Convolutional Neural Networks (CNN). They are mainly used in image generation tasks, especially in fields like Generative Adversarial Networks (GANs).

3.1 How Deconvolutional Layers Work

Deconvolutional layers perform the inverse of the standard convolution functions in CNNs. They are used to convert low-resolution images into higher resolution images. Such layers are also known as “Transpose Convolution” or “Deconvolution”. This involves applying spatial linear transformations of the filters.

3.2 Example of Deconvolution

Let’s look at an example of implementing a Deconvolutional Neural Network in PyTorch.


class DeconvNetwork(nn.Module):
    def __init__(self):
        super(DeconvNetwork, self).__init__()
        self.deconv1 = nn.ConvTranspose2d(64, 32, kernel_size=3, stride=2, padding=1)  # First deconvolution layer
        self.deconv2 = nn.ConvTranspose2d(32, 1, kernel_size=3, stride=2, padding=1)  # Second deconvolution layer

    def forward(self, x):
        x = F.relu(self.deconv1(x))  # Activation
        x = torch.sigmoid(self.deconv2(x))  # Output layer
        return x
    

3.3 Image Reconstruction via Deconvolution Networks

We can check the basic structure of image reconstruction using the model we have defined. This can be applied to solutions like GANs or Autoencoders.


deconv_model = DeconvNetwork().to(device)

# Adding an image to the array
image = torch.randn(1, 64, 7, 7).to(device)  # Random tensor
reconstructed_image = deconv_model(image)
print(reconstructed_image.shape)  # It can reconstruct to (1, 1, 28, 28)
    

4. Conclusion

In this course, we learned about two core technologies of deep learning: Convolutional Neural Networks (CNN) and Deconvolutional Neural Networks. We explained how to build and train a CNN structure using the PyTorch framework, alongside the basic operation principles of Deconvolutional Networks. These technologies are foundational to many state-of-the-art deep learning models and continue to evolve.

We hope this aids your deep learning journey, and may you continue to develop your models through deeper research and exploration!

Deep Learning PyTorch Course, Convolutional Neural Network Structure

1. Introduction

Deep learning is a field of machine learning and a major area of artificial intelligence research. Among them, Convolutional Neural Networks (CNN) are a highly effective structure for image recognition and processing. In this course, we will explore the basic structure and operation principles of CNN using PyTorch.

2. Basic Concepts of Convolutional Neural Networks

Convolutional Neural Networks are composed of the following key components:

  • Convolutional Layer: A layer designed to process high-dimensional data such as images.
  • Pooling Layer: Reduces the dimensions of feature maps, decreasing the computational load and providing invariance.
  • Fully Connected Layer: A layer used for classifying classes at the final stage of the network.

3. Structure of Convolutional Neural Networks

The basic structure of Convolutional Neural Networks can be summarized as follows:

  1. Input Layer: The original image is inputted.
  2. Convolutional Layer: Filters are applied to the input image to generate feature maps.
  3. Activation Layer (ReLU): ReLU activation function is used to introduce non-linearity.
  4. Pooling Layer: Reduces the size of the feature map to decrease the computational load.
  5. Fully Connected Layer: Performs predictions for various classes.

4. Implementing CNN with PyTorch

Now, let’s implement a simple CNN using PyTorch. We will use the Fashion MNIST dataset to classify clothing images.

4.1. Setting Up the Environment

Install and import the necessary libraries. Use the command below to install PyTorch:

pip install torch torchvision

4.2. Loading the Dataset

Load and preprocess the Fashion MNIST dataset. The following code allows you to download and load the data.


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

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

# Load training and testing datasets
train_dataset = datasets.FashionMNIST(root='./data', train=True, transform=transform, download=True)
test_dataset = datasets.FashionMNIST(root='./data', train=False, transform=transform)

# Set up data loaders
train_loader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=64, shuffle=False)
    

4.3. Defining the CNN Model

Let’s define a CNN model. The following code implements a simple CNN consisting of convolutional layers, activation layers, pooling layers, and fully connected layers.


import torch.nn as nn

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.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)  # Second convolutional layer
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)  # Max pooling layer
        self.fc1 = nn.Linear(64 * 7 * 7, 128)  # First fully connected layer
        self.fc2 = nn.Linear(128, 10)  # Second fully connected layer

    def forward(self, x):
        x = self.pool(torch.relu(self.conv1(x)))  # First convolution and pooling
        x = self.pool(torch.relu(self.conv2(x)))  # Second convolution and pooling
        x = x.view(-1, 64 * 7 * 7)  # Flatten the tensor
        x = torch.relu(self.fc1(x))  # First fully connected layer
        x = self.fc2(x)  # Second fully connected layer
        return x
    

4.4. Training the Model

To train the model, we need to set up the loss function and optimization algorithm. You can use the code below to set up the training.


import torch.optim as optim

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

# Train the model
num_epochs = 5
for epoch in range(num_epochs):
    model.train()
    running_loss = 0.0
    for images, labels in train_loader:
        optimizer.zero_grad()  # Initialize gradients
        outputs = model(images)  # Predictions from the model
        loss = criterion(outputs, labels)  # Calculate loss
        loss.backward()  # Backpropagation
        optimizer.step()  # Update weights
        running_loss += loss.item()

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

4.5. Evaluating the Model

Evaluate the trained model to check its accuracy on the test dataset. You can use the code below to perform the evaluation.


model.eval()
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}%')
    

5. Conclusion

In this course, we explored the basic structure of Convolutional Neural Networks (CNN), a core component of deep learning, as well as practical implementation methods using PyTorch. I hope you have gained an understanding of how to efficiently distinguish and classify features of image data through CNNs. The world of deep learning is vast, and it is being utilized in many future applications. I encourage you to continue improving your skills through ongoing learning and practice.

6. References

– Ian Goodfellow, Yoshua Bengio, Aaron Courville, Deep Learning, MIT Press, 2016
– PyTorch Documentation: https://pytorch.org/docs/stable/index.html

Deep Learning PyTorch Course, Korean Embedding

With the advancement of deep learning, many innovations have also been made in the field of Natural Language Processing (NLP). In particular, embedding, which is a vector representation of language, plays an important role in deep learning models. In this article, we will explain in detail how to implement Korean embedding using PyTorch.

1. What is Embedding?

Embedding is the process of converting words or sentences into vectors in high-dimensional space, making them understandable for machine learning models. This allows for the reflection of similarities between words. For example, the embedding vectors for ‘king’ and ‘queen’ will be located close to each other.

2. Korean Natural Language Processing

Korean is composed of various morphemes, making natural language processing more complex compared to languages like English. To address this, a Korean morphological analyzer can be used. Representative morphological analyzers include KoNLPy, mecab, and khaiii.

2.1 Installing and Using KoNLPy

KoNLPy is a library that helps you easily perform Korean natural language processing. Below are the installation method and basic usage of KoNLPy.

!pip install konlpy

2.2 Basic Usage Example

from konlpy.tag import Okt

okt = Okt()
text = "Deep learning is a field of artificial intelligence."
print(okt.morphs(text))  # Morphological analysis
print(okt.nouns(text))   # Noun extraction
print(okt.phrases(text))  # Phrase extraction
    

3. Implementing Embedding with PyTorch

Now we are ready to build a model, process Korean data, and execute the embedding.

3.1 Preparing the Dataset

We will prepare the text data. Here, we will use a simple list of Korean sentences.

sentences = [
    "Hello",
    "Deep learning is fun.",
    "You can learn machine learning using Python.",
    "Artificial intelligence is our future."
]
    

3.2 Text Preprocessing

We will use a morphological analyzer to extract words and prepare to create embeddings from them.

from collections import Counter
import numpy as np

# Morphological analysis
def preprocess(sentences):
    okt = Okt()
    tokens = [okt.morphs(sentence) for sentence in sentences]
    return tokens

tokens = preprocess(sentences)

# Create word set
flat_list = [item for sublist in tokens for item in sublist]
word_counter = Counter(flat_list)
word_vocab = {word: i + 1 for i, (word, _) in enumerate(word_counter.most_common())}  # 0 is reserved for padding
    

3.3 Configuring the PyTorch DataLoader

We will utilize PyTorch’s DataLoader to generate word vectors.

import torch
from torch.utils.data import Dataset, DataLoader

class CustomDataset(Dataset):
    def __init__(self, tokens, word_vocab):
        self.tokens = tokens
        self.word_vocab = word_vocab

    def __len__(self):
        return len(self.tokens)

    def __getitem__(self, idx):
        sentence = self.tokens[idx]
        return torch.tensor([self.word_vocab[word] for word in sentence], dtype=torch.long)

dataset = CustomDataset(tokens, word_vocab)
dataloader = DataLoader(dataset, batch_size=2, shuffle=True)
    

3.4 Building the Embedding Model

Now we will build a model that includes an embedding layer.

import torch.nn as nn

class WordEmbeddingModel(nn.Module):
    def __init__(self, vocab_size, embedding_dim):
        super(WordEmbeddingModel, self).__init__()
        self.embeddings = nn.Embedding(vocab_size, embedding_dim)

    def forward(self, input):
        return self.embeddings(input)

embedding_dim = 5
model = WordEmbeddingModel(vocab_size=len(word_vocab) + 1, embedding_dim=embedding_dim)
    

3.5 Training the Embedding

To train the model, we will set up a loss function and optimizer.

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

# Training for just 5 epochs as a simple example
for epoch in range(5):
    for i, data in enumerate(dataloader):
        model.zero_grad()
        output = model(data)
        label = data.view(-1)  # Setting the label (using the same word as an example)
        loss = loss_function(output.view(-1, len(word_vocab) + 1), label)
        loss.backward()
        optimizer.step()
    print(f"Epoch {epoch + 1}, Loss: {loss.item()}")
    

3.6 Visualizing the Embedding Results

We can visualize the embedding results to intuitively understand the relationships between words. Here, we will use t-SNE to visualize in 2D.

from sklearn.manifold import TSNE
import matplotlib.pyplot as plt

def visualize_embeddings(model, word_vocab):
    embeddings = model.embeddings.weight.data.numpy()
    words = list(word_vocab.keys())

    tsne = TSNE(n_components=2)
    embeddings_2d = tsne.fit_transform(embeddings)

    plt.figure(figsize=(10, 10))
    for i, word in enumerate(words):
        plt.scatter(embeddings_2d[i, 0], embeddings_2d[i, 1])
        plt.annotate(word, (embeddings_2d[i, 0], embeddings_2d[i, 1]), fontsize=9)
    plt.show()

visualize_embeddings(model, word_vocab)
    

4. Conclusion

This article covered the process of implementing Korean embedding using PyTorch. Embedding plays an important role in natural language processing and requires preprocessing tailored to the characteristics of various languages. In the future, it is recommended to conduct in-depth research on more complex models and datasets.

I hope this lecture helps improve your understanding of deep learning and natural language processing. If you have any questions, please leave a comment!