node_classification.ipynb 12.2 KB
Newer Older
1
2
3
4
5
6
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "private_outputs": true,
7
      "provenance": []
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Node Classification\n",
        "This tutorial shows how to train a multi-layer GraphSAGE for node\n",
        "classification on ``ogbn-arxiv`` provided by [Open Graph\n",
        "Benchmark (OGB)](https://ogb.stanford.edu/). The dataset contains around\n",
        "170 thousand nodes and 1 million edges.\n",
        "\n",
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/dmlc/dgl/blob/master/notebooks/stochastic_training/node_classification.ipynb) [![GitHub](https://img.shields.io/badge/-View%20on%20GitHub-181717?logo=github&logoColor=ffffff)](https://github.com/dmlc/dgl/blob/master/notebooks/stochastic_training/node_classification.ipynb)\n",
        "\n",
        "By the end of this tutorial, you will be able to\n",
        "\n",
        "-  Train a GNN model for node classification on a single GPU with DGL's\n",
        "   neighbor sampling components."
      ],
      "metadata": {
        "id": "OxbY2KlG4ZfJ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Install DGL package"
      ],
      "metadata": {
        "id": "mzZKrVVk6Y_8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Install required packages.\n",
        "import os\n",
        "import torch\n",
        "import numpy as np\n",
        "os.environ['TORCH'] = torch.__version__\n",
        "os.environ['DGLBACKEND'] = \"pytorch\"\n",
        "\n",
        "# Install the CPU version.\n",
        "device = torch.device(\"cpu\")\n",
        "!pip install --pre dgl -f https://data.dgl.ai/wheels-test/repo.html\n",
        "\n",
        "try:\n",
        "    import dgl\n",
        "    import dgl.graphbolt as gb\n",
        "    installed = True\n",
        "except ImportError as error:\n",
        "    installed = False\n",
        "    print(error)\n",
        "print(\"DGL installed!\" if installed else \"DGL not found!\")"
      ],
      "metadata": {
        "id": "QcpjTazg6hEo"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Loading Dataset\n",
        "`ogbn-arxiv` is already prepared as ``BuiltinDataset`` in **GraphBolt**."
      ],
      "metadata": {
        "id": "XWdRZAM-51Cb"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset = gb.BuiltinDataset(\"ogbn-arxiv\").load()"
      ],
      "metadata": {
        "id": "RnJkkSKhWiUG"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Dataset consists of graph, feature and tasks. You can get the training-validation-test set from the tasks. Seed nodes and corresponding labels are already stored in each training-validation-test set. Other metadata such as number of classes are also stored in the tasks. In this dataset, there is only one task: `node classification`."
      ],
      "metadata": {
        "id": "S8avoKBiXA9j"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "graph = dataset.graph\n",
        "feature = dataset.feature\n",
        "train_set = dataset.tasks[0].train_set\n",
        "valid_set = dataset.tasks[0].validation_set\n",
        "test_set = dataset.tasks[0].test_set\n",
        "task_name = dataset.tasks[0].metadata[\"name\"]\n",
        "num_classes = dataset.tasks[0].metadata[\"num_classes\"]\n",
        "print(f\"Task: {task_name}. Number of classes: {num_classes}\")"
      ],
      "metadata": {
        "id": "IXGZmgIaXJWQ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## How DGL Handles Computation Dependency¶\n",
        "The computation dependency for message passing of a single node can be described as a series of message flow graphs (MFG).\n",
        "\n",
        "![DGL Computation](https://data.dgl.ai/tutorial/img/bipartite.gif)"
      ],
      "metadata": {
        "id": "y8yn77Kg6HkW"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Defining Neighbor Sampler and Data Loader in DGL\n",
        "\n",
        "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.graphbolt.MultiProcessDataLoader` for iterating over the dataset. It accepts a data pipe that generates minibatches of nodes and their labels, sample neighbors for each node, and generate the computation dependencies in the form of MFGs. Feature fetching, block creation and copying to target device are also supported. All these operations are split into separate stages in the data pipe, so that you can customize the data pipeline by inserting your own operations.\n",
        "\n",
        "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.\n"
      ],
      "metadata": {
        "id": "q7GrcJTnZQjt"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "datapipe = gb.ItemSampler(train_set, batch_size=1024, shuffle=True)\n",
        "datapipe = datapipe.sample_neighbor(graph, [4, 4])\n",
        "datapipe = datapipe.fetch_feature(feature, node_feature_keys=[\"feat\"])\n",
        "datapipe = datapipe.to_dgl()\n",
        "datapipe = datapipe.copy_to(device)\n",
        "train_dataloader = gb.MultiProcessDataLoader(datapipe, num_workers=0)"
      ],
      "metadata": {
        "id": "yQVYDO0ZbBvi"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "You can iterate over the data loader and a `DGLMiniBatch` object is yielded.\n",
        "\n"
      ],
      "metadata": {
        "id": "7Rp12SUhbEV1"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "data = next(iter(train_dataloader))\n",
        "print(data)"
      ],
      "metadata": {
        "id": "V7vQiKj2bL_o"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "You can get the input node IDs from MFGs."
      ],
      "metadata": {
        "id": "-eBuPnT-bS-o"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "mfgs = data.blocks\n",
        "input_nodes = mfgs[0].srcdata[dgl.NID]\n",
        "print(f\"Input nodes: {input_nodes}.\")"
      ],
      "metadata": {
        "id": "bN4sgZqFbUvd"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Defining Model\n",
        "Let’s consider training a 2-layer GraphSAGE with neighbor sampling. The model can be written as follows:\n",
        "\n"
      ],
      "metadata": {
        "id": "fV6epnRxbZl4"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import torch.nn as nn\n",
        "import torch.nn.functional as F\n",
        "from dgl.nn import SAGEConv\n",
        "\n",
        "\n",
        "class Model(nn.Module):\n",
        "    def __init__(self, in_feats, h_feats, num_classes):\n",
        "        super(Model, self).__init__()\n",
        "        self.conv1 = SAGEConv(in_feats, h_feats, aggregator_type=\"mean\")\n",
        "        self.conv2 = SAGEConv(h_feats, num_classes, aggregator_type=\"mean\")\n",
        "        self.h_feats = h_feats\n",
        "\n",
        "    def forward(self, mfgs, x):\n",
        "        h = self.conv1(mfgs[0], x)\n",
        "        h = F.relu(h)\n",
        "        h = self.conv2(mfgs[1], h)\n",
        "        return h\n",
        "\n",
        "\n",
        "in_size = feature.size(\"node\", None, \"feat\")[0]\n",
        "model = Model(in_size, 64, num_classes).to(device)"
      ],
      "metadata": {
        "id": "iKhEIL0Ccmwx"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Defining Training Loop\n",
        "\n",
        "The following initializes the model and defines the optimizer.\n"
      ],
      "metadata": {
        "id": "OGLN3kCcwCA8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "opt = torch.optim.Adam(model.parameters())"
      ],
      "metadata": {
        "id": "dET8i_hewLUi"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "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."
      ],
      "metadata": {
        "id": "leZvFP4GwMcq"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "datapipe = gb.ItemSampler(valid_set, batch_size=1024, shuffle=False)\n",
        "datapipe = datapipe.sample_neighbor(graph, [4, 4])\n",
        "datapipe = datapipe.fetch_feature(feature, node_feature_keys=[\"feat\"])\n",
        "datapipe = datapipe.to_dgl()\n",
        "datapipe = datapipe.copy_to(device)\n",
        "valid_dataloader = gb.MultiProcessDataLoader(datapipe, num_workers=0)\n",
        "\n",
        "\n",
        "import sklearn.metrics"
      ],
      "metadata": {
        "id": "Gvd7vFWZwQI5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "The following is a training loop that performs validation every epoch. It also saves the model with the best validation accuracy into a file."
      ],
      "metadata": {
        "id": "nTIIfVMDwXqX"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import tqdm\n",
        "\n",
        "for epoch in range(10):\n",
        "    model.train()\n",
        "\n",
        "    with tqdm.tqdm(train_dataloader) as tq:\n",
        "        for step, data in enumerate(tq):\n",
        "            x = data.node_features[\"feat\"]\n",
        "            labels = data.labels\n",
        "\n",
        "            predictions = model(data.blocks, x)\n",
        "\n",
        "            loss = F.cross_entropy(predictions, labels)\n",
        "            opt.zero_grad()\n",
        "            loss.backward()\n",
        "            opt.step()\n",
        "\n",
        "            accuracy = sklearn.metrics.accuracy_score(\n",
        "                labels.cpu().numpy(),\n",
        "                predictions.argmax(1).detach().cpu().numpy(),\n",
        "            )\n",
        "\n",
        "            tq.set_postfix(\n",
        "                {\"loss\": \"%.03f\" % loss.item(), \"acc\": \"%.03f\" % accuracy},\n",
        "                refresh=False,\n",
        "            )\n",
        "\n",
        "    model.eval()\n",
        "\n",
        "    predictions = []\n",
        "    labels = []\n",
        "    with tqdm.tqdm(valid_dataloader) as tq, torch.no_grad():\n",
        "        for data in tq:\n",
        "            x = data.node_features[\"feat\"]\n",
        "            labels.append(data.labels.cpu().numpy())\n",
        "            predictions.append(model(data.blocks, x).argmax(1).cpu().numpy())\n",
        "        predictions = np.concatenate(predictions)\n",
        "        labels = np.concatenate(labels)\n",
        "        accuracy = sklearn.metrics.accuracy_score(labels, predictions)\n",
        "        print(\"Epoch {} Validation Accuracy {}\".format(epoch, accuracy))\n",
        "\n",
        "        # Note that this tutorial do not train the whole model to the end.\n",
        "        break"
      ],
      "metadata": {
        "id": "wsfqhKUvwZEj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Conclusion\n",
        "\n",
        "In this tutorial, you have learned how to train a multi-layer GraphSAGE with neighbor sampling.\n"
      ],
      "metadata": {
        "id": "kmHnUI0QwfJ4"
      }
    }
  ]
375
}