Book Image

Hands-On Graph Neural Networks Using Python

By : Maxime Labonne
Book Image

Hands-On Graph Neural Networks Using Python

By: Maxime Labonne

Overview of this book

Graph neural networks are a highly effective tool for analyzing data that can be represented as a graph, such as networks, chemical compounds, or transportation networks. The past few years have seen an explosion in the use of graph neural networks, with their application ranging from natural language processing and computer vision to recommendation systems and drug discovery. Hands-On Graph Neural Networks Using Python begins with the fundamentals of graph theory and shows you how to create graph datasets from tabular data. As you advance, you’ll explore major graph neural network architectures and learn essential concepts such as graph convolution, self-attention, link prediction, and heterogeneous graphs. Finally, the book proposes applications to solve real-life problems, enabling you to build a professional portfolio. The code is readily available online and can be easily adapted to other datasets and apps. By the end of this book, you’ll have learned to create graph datasets, implement graph neural networks using Python and PyTorch Geometric, and apply them to solve real-world problems, along with building and training graph neural network models for node and graph classification, link prediction, and much more.
Table of Contents (25 chapters)
1
Part 1: Introduction to Graph Learning
5
Part 2: Fundamentals
10
Part 3: Advanced Techniques
18
Part 4: Applications
22
Chapter 18: Unlocking the Potential of Graph Neural Networks for Real-World Applications

Implementing DeepWalk

Now that we have a good understanding of every component in this architecture, let’s use it to solve an ML problem.

The dataset we will use is Zachary’s Karate Club. It simply represents the relationships within a karate club studied by Wayne W. Zachary in the 1970s. It is a kind of social network where every node is a member, and members who interact outside the club are connected.

In this example, the club is divided into two groups: we would like to assign the right group to every member (node classification) just by looking at their connections:

  1. Let’s import the dataset using nx.karate_club_graph():
    G = nx.karate_club_graph()
  2. Next, we need to convert string class labels into numerical values (Mr. Hi = 0, Officer = 1):
    labels = []
    for node in G.nodes:
        label = G.nodes[node]['club']
        labels.append(1 if label == 'Officer' else 0)
  3. Let’s plot this graph...