<jupyter_start><jupyter_text><jupyter_code>#%%capture
!pip install dgl-cu111 -f https://data.dgl.ai/wheels/repo.html<jupyter_output>Looking in links: https://data.dgl.ai/wheels-test/repo.html
Collecting dgl-cu111
  Downloading https://data.dgl.ai/wheels-test/dgl_cu111-0.8a210920-cp37-cp37m-manylinux1_x86_64.whl (144.8 MB)
[K     |████████████████████████████████| 144.8 MB 39 kB/s 
[?25hRequirement already satisfied: networkx>=2.1 in /usr/local/lib/python3.7/dist-packages (from dgl-cu111) (2.6.3)
Requirement already satisfied: numpy>=1.14.0 in /usr/local/lib/python3.7/dist-packages (from dgl-cu111) (1.19.5)
Requirement already satisfied: requests>=2.19.0 in /usr/local/lib/python3.7/dist-packages (from dgl-cu111) (2.23.0)
Requirement already satisfied: scipy>=1.1.0 in /usr/local/lib/python3.7/dist-packages (from dgl-cu111) (1.4.1)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.19.0->dgl-cu111) (2021.5.30)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.19.0->dgl-cu111) (3.0.4)
Requirement alre[...]<jupyter_text>Node Classification with DGL============================GNNs are powerful tools for many machine learning tasks on graphs. Inthis introductory tutorial, you will learn the basic workflow of usingGNNs for node classification, i.e. predicting the category of a node ina graph.By completing this tutorial, you will be able to-  Load a DGL-provided dataset.-  Build a GNN model with DGL-provided neural network modules.-  Train and evaluate a GNN model for node classification on either CPU   or GPU.This tutorial assumes that you have experience in building neuralnetworks with PyTorch.(Time estimate: 13 minutes)<jupyter_code>import dgl
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
# Contruct a two-layer GNN model
import dgl.nn as dglnn
import torch.nn as nn
import torch.nn.functional as F
class SAGE(nn.Module):
    def __init__(self, in_feats, hid_feats, out_feats):
        super().__init__()
        self.conv1 = dglnn.SAGEConv(
            in_feats=in_feats, out_feats=hid_feats, aggregator_type='mean')
        self.conv2 = dglnn.SAGEConv(
            in_feats=hid_feats, out_feats=out_feats, aggregator_type='mean')

    def forward(self, graph, inputs):
        # inputs are features of nodes
        h = self.conv1(graph, inputs)
        h = F.relu(h)
        h = self.conv2(graph, h)
        return h
import dgl

dataset = dgl.data.CiteseerGraphDataset()
graph = dataset[0]
node_features = graph.ndata['feat']
node_labels = graph.ndata['label']
train_mask = graph.ndata['train_mask']
valid_mask = graph.ndata['val_mask']
test_mask = graph.ndata['test_mask']
n_features = node_features.shape[1]
n_labels = int(node_labels.max().item() + 1)
def evaluate(model, graph, features, labels, mask):
    model.eval()
    with torch.no_grad():
        logits = model(graph, features)
        logits = logits[mask]
        labels = labels[mask]
        _, indices = torch.max(logits, dim=1)
        correct = torch.sum(indices == labels)
        return correct.item() * 1.0 / len(labels)
model = SAGE(in_feats=n_features, hid_feats=100, out_feats=n_labels)
opt = torch.optim.Adam(model.parameters())

for epoch in range(10):
    model.train()
    # forward propagation by using all nodes
    logits = model(graph, node_features)
    # compute loss
    loss = F.cross_entropy(logits[train_mask], node_labels[train_mask])
    # compute validation accuracy
    acc = evaluate(model, graph, node_features, node_labels, valid_mask)
    # backward propagation
    opt.zero_grad()
    loss.backward()
    opt.step()
    print(loss.item())

    # Save model if necessary.  Omitted in this example.
self = model
inputs = node_features

h1 = self.conv1(graph, inputs)
h2 = F.relu(h1)
h3 = self.conv2(graph, h2)
print(h1.shape)
print(h2.shape)
print(h3.shape)

logits = model(graph, features)
node_labels.shape
n_features<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13296948647978168, 'fraction_numerical': 0.03745968742247581, 'mean_word_length': 4.10379746835443, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '410638', 'license': 'MIT', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1400, 'n_words': 345, 'path': 'dgl/1_introduction.ipynb', 'repo_name': 'cschlick/notebooks', 'size': '4031'}