gcn.ipynb 11 KB
Newer Older
1
2
3
4
5
6
7
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
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Building a Graph Convolutional Network Using Sparse Matrices\n",
        "\n",
        "This tutorial illustrates step-by-step how to write and train a Graph Convolutional Network ([Kipf et al. (2017)](https://arxiv.org/abs/1609.02907)) using DGL's sparse matrix APIs.\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/sparse/gcn.ipynb) [![GitHub](https://img.shields.io/badge/-View%20on%20GitHub-181717?logo=github&logoColor=ffffff)](https://github.com/dmlc/dgl/blob/master/notebooks/sparse/gcn.ipynb)"
      ],
      "metadata": {
        "id": "_iqWrPwxtZr6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Install required packages.\n",
        "import os\n",
        "import torch\n",
        "os.environ['TORCH'] = torch.__version__\n",
        "os.environ['DGLBACKEND'] = \"pytorch\"\n",
        "\n",
        "# TODO(Steve): change to stable version.\n",
        "# Uncomment below to install required packages.\n",
        "#!pip install --pre dgl -f https://data.dgl.ai/wheels-test/repo.html > /dev/null\n",
        "\n",
        "try:\n",
        "    import dgl\n",
        "    installed = True\n",
        "except ImportError:\n",
        "    installed = False\n",
48
        "print(\"DGL installed!\" if installed else \"DGL not found!\")"
49
50
51
52
53
54
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FTqB360eRvya",
55
        "outputId": "df54b94e-fd1b-4b96-fca1-21948284254c"
56
      },
57
      "execution_count": 2,
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "DGL installed!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Graph Convolutional Layer\n",
        "\n",
        "Mathematically, the graph convolutional layer is defined as:\n",
74
        "$$f(X^{(l)}, A) = \\sigma(\\hat{D}^{-\\frac{1}{2}}\\hat{A}\\hat{D}^{-\\frac{1}{2}}X^{(l)}W^{(l)})$$",
75
76
77
        "with $\\hat{A} = A + I$, where $A$ denotes the adjacency matrix and $I$ denotes the identity matrix, $\\hat{D}$ refers to the diagonal node degree matrix of $\\hat{A}$ and $W^{(l)}$ denotes a trainable weight matrix. $\\sigma$ refers to a non-linear activation (e.g. relu).\n",
        "\n",
        "The code below shows how to implement it using the `dgl.sparse` package. The core operations are:\n",
78
        "\n",
79
80
        "* `dgl.sparse.identity` creates the identity matrix $I$.\n",
        "* The augmented adjacency matrix $\\hat{A}$ is then computed by adding the identity matrix to the adjacency matrix $A$.\n",
81
82
83
        "* `A_hat.sum(0)` aggregates the augmented adjacency matrix $\\hat{A}$ along the first dimension which gives the degree vector of the augmented graph. The diagonal degree matrix $\\hat{D}$ is then created by `dgl.sparse.diag`.\n",
        "* Compute $\\hat{D}^{-\\frac{1}{2}}$.\n",
        "* `D_hat_invsqrt @ A_hat @ D_hat_invsqrt` computes the convolution matrix which is then multiplied by the linearly transformed node features."
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
      ],
      "metadata": {
        "id": "r3qB1atg_ld0"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import torch\n",
        "import torch.nn as nn\n",
        "import torch.nn.functional as F\n",
        "\n",
        "import dgl.sparse as dglsp\n",
        "\n",
        "class GCNLayer(nn.Module):\n",
        "    def __init__(self, in_size, out_size):\n",
        "        super(GCNLayer, self).__init__()\n",
        "        self.W = nn.Linear(in_size, out_size)\n",
        "\n",
        "    def forward(self, A, X):\n",
        "        ########################################################################\n",
        "        # (HIGHLIGHT) Compute the symmetrically normalized adjacency matrix with\n",
        "        # Sparse Matrix API\n",
        "        ########################################################################\n",
108
109
110
111
112
        "        I = dglsp.identity(A.shape)\n",
        "        A_hat = A + I\n",
        "        D_hat = dglsp.diag(A_hat.sum(0))\n",
        "        D_hat_invsqrt = D_hat ** -0.5\n",
        "        return D_hat_invsqrt @ A_hat @ D_hat_invsqrt @ self.W(X)"
113
114
115
116
      ],
      "metadata": {
        "id": "Y4I4EhHQ_kKb"
      },
117
      "execution_count": 3,
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
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "A Graph Convolutional Network is then defined by stacking this layer."
      ],
      "metadata": {
        "id": "bvP7O2IwV_c7"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a GCN with the GCN layer.\n",
        "class GCN(nn.Module):\n",
        "    def __init__(self, in_size, out_size, hidden_size):\n",
        "        super(GCN, self).__init__()\n",
        "        self.conv1 = GCNLayer(in_size, hidden_size)\n",
        "        self.conv2 = GCNLayer(hidden_size, out_size)\n",
        "\n",
        "    def forward(self, A, X):\n",
        "        X = self.conv1(A, X)\n",
        "        X = F.relu(X)\n",
        "        return self.conv2(A, X)"
      ],
      "metadata": {
        "id": "BHX3vRjDWJTO"
      },
147
      "execution_count": 4,
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
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Training the GCN\n",
        "\n",
        "We then train the GCN model on the Cora dataset for node classification. Note that since the model expects an adjacency matrix as the first argument, we first construct the adjacency matrix from the graph using the `dgl.sparse.from_coo` API which returns a DGL `SparseMatrix` object."
      ],
      "metadata": {
        "id": "2Qw7fTdGNnEp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def evaluate(g, pred):\n",
        "    label = g.ndata[\"label\"]\n",
        "    val_mask = g.ndata[\"val_mask\"]\n",
        "    test_mask = g.ndata[\"test_mask\"]\n",
        "\n",
        "    # Compute accuracy on validation/test set.\n",
        "    val_acc = (pred[val_mask] == label[val_mask]).float().mean()\n",
        "    test_acc = (pred[test_mask] == label[test_mask]).float().mean()\n",
        "    return val_acc, test_acc\n",
        "\n",
        "def train(model, g):\n",
        "    features = g.ndata[\"feat\"]\n",
        "    label = g.ndata[\"label\"]\n",
        "    train_mask = g.ndata[\"train_mask\"]\n",
        "    optimizer = torch.optim.Adam(model.parameters(), lr=1e-2, weight_decay=5e-4)\n",
        "    loss_fcn = nn.CrossEntropyLoss()\n",
        "\n",
        "    # Preprocess to get the adjacency matrix of the graph.\n",
        "    src, dst = g.edges()\n",
        "    N = g.num_nodes()\n",
        "    A = dglsp.from_coo(dst, src, shape=(N, N))\n",
        "\n",
        "    for epoch in range(100):\n",
        "        model.train()\n",
        "\n",
        "        # Forward.\n",
        "        logits = model(A, features)\n",
        "\n",
        "        # Compute loss with nodes in the training set.\n",
        "        loss = loss_fcn(logits[train_mask], label[train_mask])\n",
        "\n",
        "        # Backward.\n",
        "        optimizer.zero_grad()\n",
        "        loss.backward()\n",
        "        optimizer.step()\n",
        "\n",
        "        # Compute prediction.\n",
        "        pred = logits.argmax(dim=1)\n",
        "\n",
        "        # Evaluate the prediction.\n",
        "        val_acc, test_acc = evaluate(g, pred)\n",
        "        if epoch % 5 == 0:\n",
        "            print(\n",
        "                f\"In epoch {epoch}, loss: {loss:.3f}, val acc: {val_acc:.3f}\"\n",
        "                f\", test acc: {test_acc:.3f}\"\n",
        "            )\n",
        "\n",
        "\n",
        "# Load graph from the existing dataset.\n",
        "dataset = dgl.data.CoraGraphDataset()\n",
        "g = dataset[0]\n",
        "\n",
        "# Create model.\n",
        "feature = g.ndata['feat']\n",
        "in_size = feature.shape[1]\n",
        "out_size = dataset.num_classes\n",
        "gcn_model = GCN(in_size, out_size, 16)\n",
        "\n",
        "# Kick off training.\n",
        "train(gcn_model, g)"
      ],
      "metadata": {
        "id": "5Sp1B1_QHgC2",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
230
        "outputId": "552e2c22-44f4-4495-c7f9-a57f13484270"
231
      },
232
      "execution_count": 5,
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Downloading /root/.dgl/cora_v2.zip from https://data.dgl.ai/dataset/cora_v2.zip...\n",
            "Extracting file to /root/.dgl/cora_v2\n",
            "Finished data loading and preprocessing.\n",
            "  NumNodes: 2708\n",
            "  NumEdges: 10556\n",
            "  NumFeats: 1433\n",
            "  NumClasses: 7\n",
            "  NumTrainingSamples: 140\n",
            "  NumValidationSamples: 500\n",
            "  NumTestSamples: 1000\n",
            "Done saving data into cached files.\n",
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
            "In epoch 0, loss: 1.954, val acc: 0.114, test acc: 0.103\n",
            "In epoch 5, loss: 1.921, val acc: 0.158, test acc: 0.147\n",
            "In epoch 10, loss: 1.878, val acc: 0.288, test acc: 0.283\n",
            "In epoch 15, loss: 1.822, val acc: 0.344, test acc: 0.353\n",
            "In epoch 20, loss: 1.751, val acc: 0.388, test acc: 0.389\n",
            "In epoch 25, loss: 1.663, val acc: 0.406, test acc: 0.410\n",
            "In epoch 30, loss: 1.562, val acc: 0.472, test acc: 0.481\n",
            "In epoch 35, loss: 1.450, val acc: 0.558, test acc: 0.573\n",
            "In epoch 40, loss: 1.333, val acc: 0.636, test acc: 0.641\n",
            "In epoch 45, loss: 1.216, val acc: 0.684, test acc: 0.683\n",
            "In epoch 50, loss: 1.102, val acc: 0.726, test acc: 0.713\n",
            "In epoch 55, loss: 0.996, val acc: 0.740, test acc: 0.740\n",
            "In epoch 60, loss: 0.899, val acc: 0.754, test acc: 0.760\n",
            "In epoch 65, loss: 0.813, val acc: 0.762, test acc: 0.771\n",
            "In epoch 70, loss: 0.737, val acc: 0.768, test acc: 0.781\n",
            "In epoch 75, loss: 0.671, val acc: 0.776, test acc: 0.786\n",
            "In epoch 80, loss: 0.614, val acc: 0.784, test acc: 0.790\n",
            "In epoch 85, loss: 0.566, val acc: 0.780, test acc: 0.788\n",
            "In epoch 90, loss: 0.524, val acc: 0.780, test acc: 0.791\n",
            "In epoch 95, loss: 0.489, val acc: 0.772, test acc: 0.795\n"
269
270
271
272
273
274
275
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
276
        "*Check out the full example script* [here](https://github.com/dmlc/dgl/blob/master/examples/sparse/gcn.py)."
277
278
279
280
281
282
      ],
      "metadata": {
        "id": "yQnJZvE9ZduM"
      }
    }
  ]
283
}