Project: GraphScope (App Definition and Development / Database)
File: tutorial_node_classification_pyg_local.md

This tutorial presents an end-to-end example that illustrates how GraphScope trains the GraphSAGE model (implemented in PyG) for a node classification task. ```python import time import torch import torch.nn.functional as F from ogb.nodeproppred import Evaluator from torch_geometric.nn import GraphSAGE import graphscope as gs import graphscope.learning.graphlearn_torch as glt from graphscope.dataset import loadogbnarxiv from graphscope.learning.graphlearn_torch.typing import Split gs.setoption(showlog=True) g = loadogbnarxiv() ``` ```python @torch.no_grad() def test(model, testloader, datasetname): evaluator = Evaluator(name=dataset_name) model.eval() xs = [] y_true = [] for i, batch in enumerate(test_loader): if i == 0: device = batch.x.device batch.x = batch.x.to(torch.float32) # TODO x = model(batch.x, batch.edgeindex)[: batch.batchsize] xs.append(x.cpu()) ytrue.append(batch.y[: batch.batchsize].cpu()) del batch xs = [t.to(device) for t in xs] ytrue = [t.to(device) for t in ytrue] y_pred = torch.cat(xs, dim=0).argmax(dim=-1, keepdim=True) ytrue = torch.cat(ytrue, dim=0).unsqueeze(-1) test_acc = evaluator.eval( { "ytrue": ytrue, "ypred": ypred, } )["acc"] return test_acc ``` ```python gltgraph = gs.graphlearntorch( g, edges=[ ("paper", "citation", "paper"), ], node_features={ "paper": [f"feat_{i}" for i in range(128)], }, node_labels={ "paper": "label", }, edge_dir="out", randomnodesplit={ "num_val": 0.1, "num_test": 0.1, }, ) print("-- Initializing client ...") glt.distributed.init_client( num_servers=1, num_clients=1, client_rank=0, masteraddr=gltgraph.master_addr, masterport=gltgraph.serverclientmaster_port, numrpcthreads=4, is_dynamic=True, ) ``` ```python device = torch.device("cpu") print("-- Creating training dataloader ...") train_loader = glt.distributed.DistNeighborLoader( data=None, num_neighbors=[15, 10, 5], input_nodes=Split.train, batch_size=512, shuffle=True, collect_features=True, to_device=device, worker_options=glt.distributed.RemoteDistSamplingWorkerOptions( num_workers=1, worker_devices=[torch.device("cpu")], worker_concurrency=1, buffer_size="1GB", prefetch_size=1, gltgraph=gltgraph, workload_type="train", ), ) print("-- Creating testing dataloader ...") test_loader = glt.distributed.DistNeighborLoader( data=None, num_neighbors=[15, 10, 5], input_nodes=Split.test, batch_size=512, shuffle=False, collect_features=True, to_device=device, worker_options=glt.distributed.RemoteDistSamplingWorkerOptions( num_workers=1, worker_devices=[torch.device("cpu")], worker_concurrency=1, buffer_size="1GB", prefetch_size=1, gltgraph=gltgraph, workload_type="test", ), ) ``` ```python print("-- Initializing model and optimizer ...") model = GraphSAGE( in_channels=128, hidden_channels=256, num_layers=3, out_channels=47, ).to(device) optimizer = torch.optim.Adam(model.parameters(), lr=0.01) ``` ```python print("-- Start training and testing ...") epochs = 10 dataset_name = "ogbn-arxiv" for epoch in range(0, epochs): model.train() start = time.time() for batch in train_loader: optimizer.zero_grad() batch.x = batch.x.to(torch.float32) # TODO out = model(batch.x, batch.edgeindex)[: batch.batchsize].log_softmax(dim=-1) loss = F.nllloss(out, batch.y[: batch.batchsize]) loss.backward() optimizer.step() end = time.time() print(f"-- Epoch: {epoch:03d}, Loss: {loss:.4f}, Epoch Time: {end - start}") if epoch == 0 or epoch > (epochs // 2): testacc = test(model, testloader, dataset_name) print(f"-- Test Accuracy: {test_acc:.4f}") print("-- Shutting down ...") glt.distributed.shutdown_client() ```