Training GNN with Neighbor Sampling for Node Classification

This tutorial shows how to train a multi-layer GraphSAGE for node classification on ogbn-arxiv provided by Open Graph Benchmark (OGB). The dataset contains around 170 thousand nodes and 1 million edges.

By the end of this tutorial, you will be able to

  • Train a GNN model for node classification on a single GPU with DGL’s neighbor sampling components.

This tutorial assumes that you have read the Introduction of Neighbor Sampling for GNN Training.

Loading Dataset

OGB already prepared the data as DGL graph.

import dgl
import torch
import numpy as np
from ogb.nodeproppred import DglNodePropPredDataset

dataset = DglNodePropPredDataset('ogbn-arxiv')
device = 'cpu'      # change to 'cuda' for GPU

Out:

WARNING:root:The OGB package is out of date. Your version is 1.2.4, while the latest version is 1.2.5.
Downloading https://snap.stanford.edu/ogb/data/nodeproppred/arxiv.zip

  0%|          | 0/81 [00:00<?, ?it/s]
Downloaded 0.00 GB:   0%|          | 0/81 [00:00<?, ?it/s]
Downloaded 0.00 GB:   1%|1         | 1/81 [00:00<00:16,  4.88it/s]
Downloaded 0.00 GB:   1%|1         | 1/81 [00:00<00:16,  4.88it/s]
Downloaded 0.00 GB:   1%|1         | 1/81 [00:00<00:16,  4.88it/s]
Downloaded 0.00 GB:   1%|1         | 1/81 [00:00<00:16,  4.88it/s]
Downloaded 0.00 GB:   1%|1         | 1/81 [00:00<00:16,  4.88it/s]
Downloaded 0.00 GB:   6%|6         | 5/81 [00:00<00:11,  6.58it/s]
Downloaded 0.01 GB:   6%|6         | 5/81 [00:00<00:11,  6.58it/s]
Downloaded 0.01 GB:   6%|6         | 5/81 [00:00<00:11,  6.58it/s]
Downloaded 0.01 GB:   6%|6         | 5/81 [00:00<00:11,  6.58it/s]
Downloaded 0.01 GB:   6%|6         | 5/81 [00:00<00:11,  6.58it/s]
Downloaded 0.01 GB:  11%|#1        | 9/81 [00:00<00:08,  8.78it/s]
Downloaded 0.01 GB:  11%|#1        | 9/81 [00:00<00:08,  8.78it/s]
Downloaded 0.01 GB:  11%|#1        | 9/81 [00:00<00:08,  8.78it/s]
Downloaded 0.01 GB:  11%|#1        | 9/81 [00:00<00:08,  8.78it/s]
Downloaded 0.01 GB:  11%|#1        | 9/81 [00:00<00:08,  8.78it/s]
Downloaded 0.01 GB:  11%|#1        | 9/81 [00:00<00:08,  8.78it/s]
Downloaded 0.01 GB:  17%|#7        | 14/81 [00:00<00:05, 11.50it/s]
Downloaded 0.01 GB:  17%|#7        | 14/81 [00:00<00:05, 11.50it/s]
Downloaded 0.02 GB:  17%|#7        | 14/81 [00:00<00:05, 11.50it/s]
Downloaded 0.02 GB:  17%|#7        | 14/81 [00:00<00:05, 11.50it/s]
Downloaded 0.02 GB:  17%|#7        | 14/81 [00:00<00:05, 11.50it/s]
Downloaded 0.02 GB:  17%|#7        | 14/81 [00:00<00:05, 11.50it/s]
Downloaded 0.02 GB:  23%|##3       | 19/81 [00:00<00:04, 14.69it/s]
Downloaded 0.02 GB:  23%|##3       | 19/81 [00:00<00:04, 14.69it/s]
Downloaded 0.02 GB:  23%|##3       | 19/81 [00:00<00:04, 14.69it/s]
Downloaded 0.02 GB:  23%|##3       | 19/81 [00:00<00:04, 14.69it/s]
Downloaded 0.02 GB:  23%|##3       | 19/81 [00:00<00:04, 14.69it/s]
Downloaded 0.02 GB:  23%|##3       | 19/81 [00:00<00:04, 14.69it/s]
Downloaded 0.02 GB:  30%|##9       | 24/81 [00:00<00:03, 18.21it/s]
Downloaded 0.02 GB:  30%|##9       | 24/81 [00:00<00:03, 18.21it/s]
Downloaded 0.03 GB:  30%|##9       | 24/81 [00:00<00:03, 18.21it/s]
Downloaded 0.03 GB:  30%|##9       | 24/81 [00:00<00:03, 18.21it/s]
Downloaded 0.03 GB:  30%|##9       | 24/81 [00:00<00:03, 18.21it/s]
Downloaded 0.03 GB:  35%|###4      | 28/81 [00:00<00:02, 20.49it/s]
Downloaded 0.03 GB:  35%|###4      | 28/81 [00:00<00:02, 20.49it/s]
Downloaded 0.03 GB:  35%|###4      | 28/81 [00:01<00:02, 20.49it/s]
Downloaded 0.03 GB:  35%|###4      | 28/81 [00:01<00:02, 20.49it/s]
Downloaded 0.03 GB:  35%|###4      | 28/81 [00:01<00:02, 20.49it/s]
Downloaded 0.03 GB:  40%|###9      | 32/81 [00:01<00:02, 20.31it/s]
Downloaded 0.03 GB:  40%|###9      | 32/81 [00:01<00:02, 20.31it/s]
Downloaded 0.03 GB:  40%|###9      | 32/81 [00:01<00:02, 20.31it/s]
Downloaded 0.03 GB:  40%|###9      | 32/81 [00:01<00:02, 20.31it/s]
Downloaded 0.03 GB:  43%|####3     | 35/81 [00:01<00:02, 20.37it/s]
Downloaded 0.04 GB:  43%|####3     | 35/81 [00:01<00:02, 20.37it/s]
Downloaded 0.04 GB:  43%|####3     | 35/81 [00:01<00:02, 20.37it/s]
Downloaded 0.04 GB:  43%|####3     | 35/81 [00:01<00:02, 20.37it/s]
Downloaded 0.04 GB:  47%|####6     | 38/81 [00:01<00:02, 20.55it/s]
Downloaded 0.04 GB:  47%|####6     | 38/81 [00:01<00:02, 20.55it/s]
Downloaded 0.04 GB:  47%|####6     | 38/81 [00:01<00:02, 20.55it/s]
Downloaded 0.04 GB:  47%|####6     | 38/81 [00:01<00:02, 20.55it/s]
Downloaded 0.04 GB:  51%|#####     | 41/81 [00:01<00:01, 20.70it/s]
Downloaded 0.04 GB:  51%|#####     | 41/81 [00:01<00:01, 20.70it/s]
Downloaded 0.04 GB:  51%|#####     | 41/81 [00:01<00:01, 20.70it/s]
Downloaded 0.04 GB:  51%|#####     | 41/81 [00:01<00:01, 20.70it/s]
Downloaded 0.04 GB:  54%|#####4    | 44/81 [00:01<00:01, 20.53it/s]
Downloaded 0.04 GB:  54%|#####4    | 44/81 [00:01<00:01, 20.53it/s]
Downloaded 0.04 GB:  54%|#####4    | 44/81 [00:01<00:01, 20.53it/s]
Downloaded 0.05 GB:  54%|#####4    | 44/81 [00:01<00:01, 20.53it/s]
Downloaded 0.05 GB:  58%|#####8    | 47/81 [00:01<00:02, 16.63it/s]
Downloaded 0.05 GB:  58%|#####8    | 47/81 [00:02<00:02, 16.63it/s]
Downloaded 0.05 GB:  58%|#####8    | 47/81 [00:02<00:02, 16.63it/s]
Downloaded 0.05 GB:  60%|######    | 49/81 [00:02<00:02, 14.44it/s]
Downloaded 0.05 GB:  60%|######    | 49/81 [00:02<00:02, 14.44it/s]
Downloaded 0.05 GB:  60%|######    | 49/81 [00:02<00:02, 14.44it/s]
Downloaded 0.05 GB:  63%|######2   | 51/81 [00:02<00:02, 12.16it/s]
Downloaded 0.05 GB:  63%|######2   | 51/81 [00:02<00:02, 12.16it/s]
Downloaded 0.05 GB:  63%|######2   | 51/81 [00:02<00:02, 12.16it/s]
Downloaded 0.05 GB:  65%|######5   | 53/81 [00:02<00:02, 10.65it/s]
Downloaded 0.05 GB:  65%|######5   | 53/81 [00:02<00:02, 10.65it/s]
Downloaded 0.05 GB:  65%|######5   | 53/81 [00:02<00:02, 10.65it/s]
Downloaded 0.05 GB:  68%|######7   | 55/81 [00:02<00:02,  9.88it/s]
Downloaded 0.05 GB:  68%|######7   | 55/81 [00:02<00:02,  9.88it/s]
Downloaded 0.06 GB:  68%|######7   | 55/81 [00:03<00:02,  9.88it/s]
Downloaded 0.06 GB:  70%|#######   | 57/81 [00:03<00:02,  9.47it/s]
Downloaded 0.06 GB:  70%|#######   | 57/81 [00:03<00:02,  9.47it/s]
Downloaded 0.06 GB:  70%|#######   | 57/81 [00:03<00:02,  9.47it/s]
Downloaded 0.06 GB:  73%|#######2  | 59/81 [00:03<00:02,  9.23it/s]
Downloaded 0.06 GB:  73%|#######2  | 59/81 [00:03<00:02,  9.23it/s]
Downloaded 0.06 GB:  74%|#######4  | 60/81 [00:03<00:02,  9.17it/s]
Downloaded 0.06 GB:  74%|#######4  | 60/81 [00:03<00:02,  9.17it/s]
Downloaded 0.06 GB:  75%|#######5  | 61/81 [00:03<00:02,  8.57it/s]
Downloaded 0.06 GB:  75%|#######5  | 61/81 [00:03<00:02,  8.57it/s]
Downloaded 0.06 GB:  77%|#######6  | 62/81 [00:03<00:02,  7.90it/s]
Downloaded 0.06 GB:  77%|#######6  | 62/81 [00:03<00:02,  7.90it/s]
Downloaded 0.06 GB:  78%|#######7  | 63/81 [00:03<00:02,  7.35it/s]
Downloaded 0.06 GB:  78%|#######7  | 63/81 [00:04<00:02,  7.35it/s]
Downloaded 0.06 GB:  79%|#######9  | 64/81 [00:04<00:02,  6.28it/s]
Downloaded 0.06 GB:  79%|#######9  | 64/81 [00:04<00:02,  6.28it/s]
Downloaded 0.06 GB:  80%|########  | 65/81 [00:04<00:02,  5.75it/s]
Downloaded 0.06 GB:  80%|########  | 65/81 [00:04<00:02,  5.75it/s]
Downloaded 0.06 GB:  81%|########1 | 66/81 [00:04<00:02,  5.49it/s]
Downloaded 0.07 GB:  81%|########1 | 66/81 [00:04<00:02,  5.49it/s]
Downloaded 0.07 GB:  83%|########2 | 67/81 [00:04<00:02,  5.44it/s]
Downloaded 0.07 GB:  83%|########2 | 67/81 [00:04<00:02,  5.44it/s]
Downloaded 0.07 GB:  84%|########3 | 68/81 [00:04<00:02,  5.29it/s]
Downloaded 0.07 GB:  84%|########3 | 68/81 [00:05<00:02,  5.29it/s]
Downloaded 0.07 GB:  85%|########5 | 69/81 [00:05<00:02,  5.35it/s]
Downloaded 0.07 GB:  85%|########5 | 69/81 [00:05<00:02,  5.35it/s]
Downloaded 0.07 GB:  86%|########6 | 70/81 [00:05<00:02,  5.40it/s]
Downloaded 0.07 GB:  86%|########6 | 70/81 [00:05<00:02,  5.40it/s]
Downloaded 0.07 GB:  88%|########7 | 71/81 [00:05<00:01,  5.43it/s]
Downloaded 0.07 GB:  88%|########7 | 71/81 [00:05<00:01,  5.43it/s]
Downloaded 0.07 GB:  89%|########8 | 72/81 [00:05<00:01,  5.47it/s]
Downloaded 0.07 GB:  89%|########8 | 72/81 [00:05<00:01,  5.47it/s]
Downloaded 0.07 GB:  90%|######### | 73/81 [00:05<00:01,  5.59it/s]
Downloaded 0.07 GB:  90%|######### | 73/81 [00:05<00:01,  5.59it/s]
Downloaded 0.07 GB:  91%|#########1| 74/81 [00:05<00:01,  5.71it/s]
Downloaded 0.07 GB:  91%|#########1| 74/81 [00:06<00:01,  5.71it/s]
Downloaded 0.07 GB:  93%|#########2| 75/81 [00:06<00:01,  5.78it/s]
Downloaded 0.07 GB:  93%|#########2| 75/81 [00:06<00:01,  5.78it/s]
Downloaded 0.07 GB:  94%|#########3| 76/81 [00:06<00:00,  5.43it/s]
Downloaded 0.08 GB:  94%|#########3| 76/81 [00:06<00:00,  5.43it/s]
Downloaded 0.08 GB:  95%|#########5| 77/81 [00:06<00:00,  5.19it/s]
Downloaded 0.08 GB:  95%|#########5| 77/81 [00:06<00:00,  5.19it/s]
Downloaded 0.08 GB:  96%|#########6| 78/81 [00:06<00:00,  5.14it/s]
Downloaded 0.08 GB:  96%|#########6| 78/81 [00:06<00:00,  5.14it/s]
Downloaded 0.08 GB:  98%|#########7| 79/81 [00:06<00:00,  5.15it/s]
Downloaded 0.08 GB:  98%|#########7| 79/81 [00:06<00:00,  5.15it/s]
Downloaded 0.08 GB:  98%|#########7| 79/81 [00:06<00:00,  5.15it/s]
Downloaded 0.08 GB: 100%|##########| 81/81 [00:06<00:00, 11.64it/s]
Extracting dataset/arxiv.zip
Loading necessary files...
This might take a while.
Processing graphs...

  0%|          | 0/1 [00:00<?, ?it/s]
100%|##########| 1/1 [00:00<00:00, 9664.29it/s]
Converting graphs into DGL objects...

  0%|          | 0/1 [00:00<?, ?it/s]
100%|##########| 1/1 [00:00<00:00, 30.62it/s]
Saving...

OGB dataset is a collection of graphs and their labels. ogbn-arxiv dataset only contains a single graph. So you can simply get the graph and its node labels like this:

graph, node_labels = dataset[0]
# Add reverse edges since ogbn-arxiv is unidirectional.
graph = dgl.add_reverse_edges(graph)
graph.ndata['label'] = node_labels[:, 0]
print(graph)
print(node_labels)

node_features = graph.ndata['feat']
num_features = node_features.shape[1]
num_classes = (node_labels.max() + 1).item()
print('Number of classes:', num_classes)

Out:

Graph(num_nodes=169343, num_edges=2332486,
      ndata_schemes={'year': Scheme(shape=(1,), dtype=torch.int64), 'feat': Scheme(shape=(128,), dtype=torch.float32), 'label': Scheme(shape=(), dtype=torch.int64)}
      edata_schemes={})
tensor([[ 4],
        [ 5],
        [28],
        ...,
        [10],
        [ 4],
        [ 1]])
Number of classes: 40

You can get the training-validation-test split of the nodes with get_split_idx method.

idx_split = dataset.get_idx_split()
train_nids = idx_split['train']
valid_nids = idx_split['valid']
test_nids = idx_split['test']

How DGL Handles Computation Dependency

In the previous tutorial, you have seen that the computation dependency for message passing of a single node can be described as a series of message flow graphs (MFG).

image1

Defining Neighbor Sampler and Data Loader in DGL

DGL provides tools to iterate over the dataset in minibatches while generating the computation dependencies to compute their outputs with the MFGs above. For node classification, you can use dgl.dataloading.NodeDataLoader for iterating over the dataset. It accepts a sampler object to control how to generate the computation dependencies in the form of MFGs. DGL provides implementations of common sampling algorithms such as dgl.dataloading.MultiLayerNeighborSampler which randomly picks a fixed number of neighbors for each node.

Note

To write your own neighbor sampler, please refer to this user guide section.

The syntax of dgl.dataloading.NodeDataLoader is mostly similar to a PyTorch DataLoader, with the addition that it needs a graph to generate computation dependency from, a set of node IDs to iterate on, and the neighbor sampler you defined.

Let’s say that each node will gather messages from 4 neighbors on each layer. The code defining the data loader and neighbor sampler will look like the following.

sampler = dgl.dataloading.MultiLayerNeighborSampler([4, 4])
train_dataloader = dgl.dataloading.NodeDataLoader(
    # The following arguments are specific to NodeDataLoader.
    graph,              # The graph
    train_nids,         # The node IDs to iterate over in minibatches
    sampler,            # The neighbor sampler
    device=device,      # Put the sampled MFGs on CPU or GPU
    # The following arguments are inherited from PyTorch DataLoader.
    batch_size=1024,    # Batch size
    shuffle=True,       # Whether to shuffle the nodes for every epoch
    drop_last=False,    # Whether to drop the last incomplete batch
    num_workers=0       # Number of sampler processes
)

You can iterate over the data loader and see what it yields.

input_nodes, output_nodes, mfgs = example_minibatch = next(iter(train_dataloader))
print(example_minibatch)
print("To compute {} nodes' outputs, we need {} nodes' input features".format(len(output_nodes), len(input_nodes)))

Out:

[tensor([92352, 37492, 18734,  ..., 68384,  8637, 99518]), tensor([92352, 37492, 18734,  ..., 74543, 92867, 16927]), [Block(num_src_nodes=12978, num_dst_nodes=4139, num_edges=15005), Block(num_src_nodes=4139, num_dst_nodes=1024, num_edges=3316)]]
To compute 1024 nodes' outputs, we need 12978 nodes' input features

NodeDataLoader gives us three items per iteration.

  • An ID tensor for the input nodes, i.e., nodes whose input features are needed on the first GNN layer for this minibatch.

  • An ID tensor for the output nodes, i.e. nodes whose representations are to be computed.

  • A list of MFGs storing the computation dependencies for each GNN layer.

You can get the source and destination node IDs of the MFGs and verify that the first few source nodes are always the same as the destination nodes. As we described in the overview, destination nodes’ own features from the previous layer may also be necessary in the computation of the new features.

mfg_0_src = mfgs[0].srcdata[dgl.NID]
mfg_0_dst = mfgs[0].dstdata[dgl.NID]
print(mfg_0_src)
print(mfg_0_dst)
print(torch.equal(mfg_0_src[:mfgs[0].num_dst_nodes()], mfg_0_dst))

Out:

tensor([92352, 37492, 18734,  ..., 68384,  8637, 99518])
tensor([92352, 37492, 18734,  ..., 22905, 99768,  7283])
True

Defining Model

Let’s consider training a 2-layer GraphSAGE with neighbor sampling. The model can be written as follows:

import torch.nn as nn
import torch.nn.functional as F
from dgl.nn import SAGEConv

class Model(nn.Module):
    def __init__(self, in_feats, h_feats, num_classes):
        super(Model, self).__init__()
        self.conv1 = SAGEConv(in_feats, h_feats, aggregator_type='mean')
        self.conv2 = SAGEConv(h_feats, num_classes, aggregator_type='mean')
        self.h_feats = h_feats

    def forward(self, mfgs, x):
        # Lines that are changed are marked with an arrow: "<---"

        h_dst = x[:mfgs[0].num_dst_nodes()]  # <---
        h = self.conv1(mfgs[0], (x, h_dst))  # <---
        h = F.relu(h)
        h_dst = h[:mfgs[1].num_dst_nodes()]  # <---
        h = self.conv2(mfgs[1], (h, h_dst))  # <---
        return h

model = Model(num_features, 128, num_classes).to(device)

If you compare against the code in the introduction, you will notice several differences:

  • DGL GNN layers on MFGs. Instead of computing on the full graph:

    h = self.conv1(g, x)
    

    you only compute on the sampled MFG:

    h = self.conv1(mfgs[0], (x, h_dst))
    

    All DGL’s GNN modules support message passing on MFGs, where you supply a pair of features, one for source nodes and another for destination nodes.

  • Feature slicing for self-dependency. There are statements that perform slicing to obtain the previous-layer representation of the

    nodes:

    h_dst = x[:mfgs[0].num_dst_nodes()]
    

    num_dst_nodes method works with MFGs, where it will return the number of destination nodes.

    Since the first few source nodes of the yielded MFG are always the same as the destination nodes, these statements obtain the representations of the destination nodes on the previous layer. They are then combined with neighbor aggregation in dgl.nn.SAGEConv layer.

Note

See the custom message passing tutorial for more details on how to manipulate MFGs produced in this way, such as the usage of num_dst_nodes.

Defining Training Loop

The following initializes the model and defines the optimizer.

opt = torch.optim.Adam(model.parameters())

When computing the validation score for model selection, usually you can also do neighbor sampling. To do that, you need to define another data loader.

valid_dataloader = dgl.dataloading.NodeDataLoader(
    graph, valid_nids, sampler,
    batch_size=1024,
    shuffle=False,
    drop_last=False,
    num_workers=0,
    device=device
)

The following is a training loop that performs validation every epoch. It also saves the model with the best validation accuracy into a file.

import tqdm
import sklearn.metrics

best_accuracy = 0
best_model_path = 'model.pt'
for epoch in range(10):
    model.train()

    with tqdm.tqdm(train_dataloader) as tq:
        for step, (input_nodes, output_nodes, mfgs) in enumerate(tq):
            # feature copy from CPU to GPU takes place here
            inputs = mfgs[0].srcdata['feat']
            labels = mfgs[-1].dstdata['label']

            predictions = model(mfgs, inputs)

            loss = F.cross_entropy(predictions, labels)
            opt.zero_grad()
            loss.backward()
            opt.step()

            accuracy = sklearn.metrics.accuracy_score(labels.cpu().numpy(), predictions.argmax(1).detach().cpu().numpy())

            tq.set_postfix({'loss': '%.03f' % loss.item(), 'acc': '%.03f' % accuracy}, refresh=False)

    model.eval()

    predictions = []
    labels = []
    with tqdm.tqdm(valid_dataloader) as tq, torch.no_grad():
        for input_nodes, output_nodes, mfgs in tq:
            inputs = mfgs[0].srcdata['feat']
            labels.append(mfgs[-1].dstdata['label'].cpu().numpy())
            predictions.append(model(mfgs, inputs).argmax(1).cpu().numpy())
        predictions = np.concatenate(predictions)
        labels = np.concatenate(labels)
        accuracy = sklearn.metrics.accuracy_score(labels, predictions)
        print('Epoch {} Validation Accuracy {}'.format(epoch, accuracy))
        if best_accuracy < accuracy:
            best_accuracy = accuracy
            torch.save(model.state_dict(), best_model_path)

        # Note that this tutorial do not train the whole model to the end.
        break

Out:

  0%|          | 0/89 [00:00<?, ?it/s]
  3%|3         | 3/89 [00:00<00:03, 23.26it/s, loss=3.374, acc=0.164]
  7%|6         | 6/89 [00:00<00:03, 23.77it/s, loss=3.148, acc=0.186]
 11%|#1        | 10/89 [00:00<00:03, 25.55it/s, loss=2.939, acc=0.205]
 15%|#4        | 13/89 [00:00<00:02, 25.70it/s, loss=2.884, acc=0.240]
 18%|#7        | 16/89 [00:00<00:02, 25.72it/s, loss=2.820, acc=0.268]
 21%|##1       | 19/89 [00:00<00:02, 25.89it/s, loss=2.691, acc=0.302]
 25%|##4       | 22/89 [00:00<00:02, 26.39it/s, loss=2.689, acc=0.295]
 28%|##8       | 25/89 [00:00<00:02, 25.89it/s, loss=2.559, acc=0.316]
 31%|###1      | 28/89 [00:01<00:02, 25.73it/s, loss=2.575, acc=0.342]
 35%|###4      | 31/89 [00:01<00:02, 26.34it/s, loss=2.464, acc=0.358]
 38%|###8      | 34/89 [00:01<00:02, 27.19it/s, loss=2.379, acc=0.377]
 42%|####1     | 37/89 [00:01<00:01, 26.47it/s, loss=2.371, acc=0.396]
 45%|####4     | 40/89 [00:01<00:01, 26.59it/s, loss=2.214, acc=0.428]
 48%|####8     | 43/89 [00:01<00:01, 26.71it/s, loss=2.241, acc=0.416]
 52%|#####1    | 46/89 [00:01<00:01, 26.93it/s, loss=2.210, acc=0.429]
 55%|#####5    | 49/89 [00:01<00:01, 26.91it/s, loss=2.223, acc=0.453]
 58%|#####8    | 52/89 [00:01<00:01, 26.31it/s, loss=2.118, acc=0.429]
 62%|######1   | 55/89 [00:02<00:01, 26.02it/s, loss=2.027, acc=0.474]
 65%|######5   | 58/89 [00:02<00:01, 26.19it/s, loss=1.995, acc=0.477]
 69%|######8   | 61/89 [00:02<00:01, 25.94it/s, loss=2.016, acc=0.472]
 72%|#######1  | 64/89 [00:02<00:00, 26.42it/s, loss=2.039, acc=0.454]
 75%|#######5  | 67/89 [00:02<00:00, 27.24it/s, loss=1.926, acc=0.505]
 79%|#######8  | 70/89 [00:02<00:00, 27.02it/s, loss=1.970, acc=0.491]
 82%|########2 | 73/89 [00:02<00:00, 26.64it/s, loss=1.943, acc=0.480]
 85%|########5 | 76/89 [00:02<00:00, 27.37it/s, loss=1.809, acc=0.533]
 89%|########8 | 79/89 [00:02<00:00, 27.39it/s, loss=1.891, acc=0.511]
 92%|#########2| 82/89 [00:03<00:00, 26.98it/s, loss=1.768, acc=0.544]
 96%|#########5| 85/89 [00:03<00:00, 27.70it/s, loss=1.834, acc=0.521]
 99%|#########8| 88/89 [00:03<00:00, 26.94it/s, loss=1.661, acc=0.562]
100%|##########| 89/89 [00:03<00:00, 26.70it/s, loss=1.706, acc=0.565]

  0%|          | 0/30 [00:00<?, ?it/s]
 13%|#3        | 4/30 [00:00<00:00, 38.30it/s]
 30%|###       | 9/30 [00:00<00:00, 40.66it/s]
 47%|####6     | 14/30 [00:00<00:00, 42.14it/s]
 63%|######3   | 19/30 [00:00<00:00, 43.59it/s]
 80%|########  | 24/30 [00:00<00:00, 44.69it/s]
 97%|#########6| 29/30 [00:00<00:00, 45.52it/s]
100%|##########| 30/30 [00:00<00:00, 46.65it/s]
Epoch 0 Validation Accuracy 0.5712272223900131

Conclusion

In this tutorial, you have learned how to train a multi-layer GraphSAGE with neighbor sampling.

What’s next?

# Thumbnail Courtesy: Stanford CS224W Notes
# sphinx_gallery_thumbnail_path = '_static/blitz_1_introduction.png'

Total running time of the script: ( 0 minutes 15.864 seconds)

Gallery generated by Sphinx-Gallery