"print(\"DGL installed!\" if installed else \"DGL not found!\")"
],
"metadata": {
"id": "QcpjTazg6hEo"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OOKZxxT7W1Rz"
},
"source": [
"## Loading Dataset\n",
"`cora` is already prepared as `BuiltinDataset` in **GraphBolt**.\n"
],
"metadata": {
"id": "OOKZxxT7W1Rz"
}
]
},
{
"cell_type": "code",
"source": [
"dataset = gb.BuiltinDataset(\"cora\").load()"
],
"execution_count": null,
"metadata": {
"id": "RnJkkSKhWiUG"
},
"execution_count": null,
"outputs": []
"outputs": [],
"source": [
"dataset = gb.BuiltinDataset(\"cora\").load()"
]
},
{
"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. This dataset contains 2 tasks, one for node classification and the other for link prediction. We will use the link prediction task."
],
"metadata": {
"id": "WxnTMEQXXKsM"
}
},
"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. This dataset contains 2 tasks, one for node classification and the other for link prediction. We will use the link prediction task."
"## Defining Neighbor Sampler and Data Loader in DGL\n",
"Different from the link prediction tutorial for full graph, a common practice to train GNN on large graphs is to iterate over the edges in minibatches, since computing the probability of all edges is usually impossible. For each minibatch of edges, you compute the output representation of their incident nodes using neighbor sampling and GNN, in a similar fashion introduced in the node classification tutorial.\n",
...
...
@@ -130,63 +117,66 @@
"\n",
"Except for the negative sampler, the rest of the code is identical to the node classification tutorial.\n",
"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"
}
},
"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`."
"## 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.DataLoader` 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"
"The following initializes the model and defines the optimizer.\n"
],
"metadata": {
"id": "OGLN3kCcwCA8"
}
]
},
{
"cell_type": "code",
"source": [
"opt = torch.optim.Adam(model.parameters())"
],
"execution_count": null,
"metadata": {
"id": "dET8i_hewLUi"
},
"execution_count": null,
"outputs": []
"outputs": [],
"source": [
"opt = torch.optim.Adam(model.parameters())"
]
},
{
"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"
}
},
"source": [
"When computing the validation score for model selection, usually you can also do neighbor sampling. We can just reuse our create_dataloader function to create two separate dataloaders for training and validation."