[파이썬] PyTorch 기하학적 딥러닝

Deep learning has revolutionized the field of machine learning, enabling us to tackle complex tasks such as image classification, natural language processing, and object detection. PyTorch, a popular deep learning framework, provides a powerful toolset for building and training deep neural networks. In this blog post, we will explore the use of PyTorch for geometric deep learning, which focuses on applying neural networks to graph-structured data.

What is Geometric Deep Learning?

Geometric deep learning is a subfield of deep learning that deals with data represented in non-Euclidean domains, such as graphs and manifolds. Traditional deep learning frameworks like PyTorch are designed for processing grid-like data, such as images or sequences. Geometric deep learning, on the other hand, allows us to learn from and make predictions on graph-structured data like social networks, molecular graphs, and 3D meshes.

PyTorch Geometric Library

To facilitate geometric deep learning tasks, PyTorch provides the “PyTorch Geometric” library. This library extends PyTorch’s capabilities by introducing new data structures and operations specific to graph-structured data. It also includes a collection of benchmark datasets and pre-trained models for common geometric deep learning tasks.

To get started with PyTorch Geometric, you can install it using pip:

pip install torch-geometric

Once installed, you can import the library in your Python script or Jupyter Notebook:

import torch
from torch_geometric.datasets import TUDataset

Example: Graph Classification with PyTorch Geometric

Let’s consider a simple example of graph classification using PyTorch Geometric. Here, our task is to classify molecules as either active or inactive based on their graph representations.

First, we need a dataset of molecule graphs. PyTorch Geometric provides several benchmark datasets, including the “Tox21” dataset, which contains molecular graphs with associated labels. We can load the dataset as follows:

dataset = TUDataset(root='data/Tox21', name='Tox21')

Next, we can split the dataset into training and testing sets:

train_dataset = dataset[:9000]
test_dataset = dataset[9000:]

Now, let’s define and train a graph convolutional neural network (GCN) using PyTorch Geometric. The GCN is a popular architecture for graph-based tasks. Here’s a simple implementation:

import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv

class GCN(torch.nn.Module):
    def __init__(self, num_features, num_classes):
        super(GCN, self).__init__()
        self.conv1 = GCNConv(num_features, 16)
        self.conv2 = GCNConv(16, num_classes)

    def forward(self, x, edge_index):
        x = self.conv1(x, edge_index)
        x = torch.relu(x)
        x = F.dropout(x, training=self.training)
        x = self.conv2(x, edge_index)
        return F.softmax(x, dim=1)

model = GCN(num_features=dataset.num_features, num_classes=dataset.num_classes)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

def train():
    model.train()
    optimizer.zero_grad()
    output = model(train_dataset.x, train_dataset.edge_index)
    loss = F.nll_loss(output[train_dataset.y_mask], train_dataset.y[train_dataset.y_mask])
    loss.backward()
    optimizer.step()

for epoch in range(200):
    train()

Finally, we can evaluate the trained model on the test dataset:

model.eval()
output = model(test_dataset.x, test_dataset.edge_index)
pred = output.argmax(dim=1)
accuracy = (pred == test_dataset.y[test_dataset.y_mask]).sum().item() / test_dataset.y_mask.sum().item()
print(f"Test Accuracy: {accuracy}")

With PyTorch Geometric, performing graph-based deep learning tasks like graph classification becomes much more straightforward. The library provides a wide range of graph convolutional layers, pooling techniques, and utilities for dealing with graph data, making it a valuable tool for researchers and practitioners in the field of geometric deep learning.

Conclusion

PyTorch Geometric extends the capabilities of PyTorch for geometric deep learning by providing efficient data structures and operations for graph-structured data. In this blog post, we explored a simple example of graph classification using PyTorch Geometric and demonstrated how to load a dataset, define a graph convolutional network, and train/test the model. With its ease of use and powerful tools, PyTorch Geometric is a valuable resource for researchers and practitioners looking to solve problems in geometric deep learning.