"[](https://colab.research.google.com/github/dmlc/dgl/blob/master/notebooks/stochastic_training/link_prediction.ipynb) [](https://github.com/dmlc/dgl/blob/master/notebooks/stochastic_training/link_prediction.ipynb)\n",
"\n",
"This tutorial will show how to train a multi-layer GraphSAGE for link\n",
"prediction on [CoraGraphDataset](https://data.dgl.ai/dataset/cora_v2.zip).\n",
"The dataset contains 2708 nodes and 10556 edges.\n",
"\n",
"By the end of this tutorial, you will be able to\n",
"\n",
"- Train a GNN model for link prediction on target device with DGL's\n",
"print(\"DGL installed!\" if installed else \"DGL not found!\")"
],
"metadata": {
"id": "QcpjTazg6hEo"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"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()"
],
"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. 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",
"\n",
"To perform link prediction, you need to specify a negative sampler. DGL provides builtin negative samplers such as `dgl.graphbolt.UniformNegativeSampler`. Here this tutorial uniformly draws 5 negative examples per positive example.\n",
"\n",
"Except for the negative sampler, the rest of the code is identical to the node classification tutorial.\n",