ondisk_dataset_heterograph.ipynb 32.7 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
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# OnDiskDataset for Heterogeneous Graph\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/ondisk_dataset_heterograph.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/ondisk_dataset_heterograph.ipynb)\n",
        "\n",
25
        "This tutorial shows how to create `OnDiskDataset` for heterogeneous graph that could be used in **GraphBolt** framework. The major difference from creating dataset for homogeneous graph is that we need to specify node/edge types for edges, feature data, training/validation/test sets.\n",
26
27
        "\n",
        "By the end of this tutorial, you will be able to\n",
28
        "\n",
29
30
        "- organize graph structure data.\n",
        "- organize feature data.\n",
31
32
33
34
35
        "- organize training/validation/test set for specific tasks.\n",
        "\n",
        "To create an ``OnDiskDataset`` object, you need to organize all the data including graph structure, feature data and tasks into a directory. The directory should contain a ``metadata.yaml`` file that describes the metadata of the dataset.\n",
        "\n",
        "Now let's generate various data step by step and organize them together to instantiate `OnDiskDataset` finally."
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
      ],
      "metadata": {
        "id": "FnFhPMaAfLtJ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Install DGL package"
      ],
      "metadata": {
        "id": "Wlb19DtWgtzq"
      }
    },
    {
      "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": "UojlT9ZGgyr9"
      },
      "execution_count": null,
      "outputs": []
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
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Data preparation\n",
        "In order to demonstrate how to organize various data, let's create a base directory first."
      ],
      "metadata": {
        "id": "2R7WnSbjsfbr"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "base_dir = './ondisk_dataset_heterograph'\n",
        "os.makedirs(base_dir, exist_ok=True)\n",
        "print(f\"Created base directory: {base_dir}\")"
      ],
      "metadata": {
        "id": "SZipbzyltLfO"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Generate graph structure data\n",
106
        "For heterogeneous graph, we need to save different edge edges(namely node pairs) into separate **Numpy** or **CSV** files.\n",
107
        "\n",
108
109
110
        "Note:\n",
        "- when saving to **Numpy**, the array requires to be in shape of `(2, N)`. This format is recommended as constructing graph from it is much faster than **CSV** file.\n",
        "- when saving to **CSV** file, do not save index and header.\n"
111
112
113
114
115
116
117
118
119
120
      ],
      "metadata": {
        "id": "qhNtIn_xhlnl"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
121
122
123
124
125
        "\n",
        "# For simplicity, we create a heterogeneous graph with\n",
        "# 2 node types: `user`, `item`\n",
        "# 2 edge types: `user:like:item`, `user:follow:user`\n",
        "# And each node/edge type has the same number of nodes/edges.\n",
126
127
128
        "num_nodes = 1000\n",
        "num_edges = 10 * num_nodes\n",
        "\n",
129
130
131
        "# Edge type: \"user:like:item\"\n",
        "like_edges_path = os.path.join(base_dir, \"like-edges.csv\")\n",
        "like_edges = np.random.randint(0, num_nodes, size=(num_edges, 2))\n",
132
        "print(f\"Part of [user:like:item] edges: {like_edges[:5, :]}\\n\")\n",
133
134
135
        "\n",
        "df = pd.DataFrame(like_edges)\n",
        "df.to_csv(like_edges_path, index=False, header=False)\n",
136
        "print(f\"[user:like:item] edges are saved into {like_edges_path}\\n\")\n",
137
        "\n",
138
139
140
        "# Edge type: \"user:follow:user\"\n",
        "follow_edges_path = os.path.join(base_dir, \"follow-edges.csv\")\n",
        "follow_edges = np.random.randint(0, num_nodes, size=(num_edges, 2))\n",
141
        "print(f\"Part of [user:follow:user] edges: {follow_edges[:5, :]}\\n\")\n",
142
        "\n",
143
144
        "df = pd.DataFrame(follow_edges)\n",
        "df.to_csv(follow_edges_path, index=False, header=False)\n",
145
        "print(f\"[user:follow:user] edges are saved into {follow_edges_path}\\n\")"
146
147
148
149
150
151
152
153
154
155
156
      ],
      "metadata": {
        "id": "HcBt4G5BmSjr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Generate feature data for graph\n",
157
        "For feature data, numpy arrays and torch tensors are supported for now. Let's generate feature data for each node/edge type."
158
159
160
161
162
163
164
165
      ],
      "metadata": {
        "id": "kh-4cPtzpcaH"
      }
    },
    {
      "cell_type": "code",
      "source": [
166
167
168
        "# Generate node[user] feature in numpy array.\n",
        "node_user_feat_0_path = os.path.join(base_dir, \"node-user-feat-0.npy\")\n",
        "node_user_feat_0 = np.random.rand(num_nodes, 5)\n",
169
        "print(f\"Part of node[user] feature [feat_0]: {node_user_feat_0[:3, :]}\")\n",
170
        "np.save(node_user_feat_0_path, node_user_feat_0)\n",
171
        "print(f\"Node[user] feature [feat_0] is saved to {node_user_feat_0_path}\\n\")\n",
172
173
174
175
        "\n",
        "# Generate another node[user] feature in torch tensor\n",
        "node_user_feat_1_path = os.path.join(base_dir, \"node-user-feat-1.pt\")\n",
        "node_user_feat_1 = torch.rand(num_nodes, 5)\n",
176
        "print(f\"Part of node[user] feature [feat_1]: {node_user_feat_1[:3, :]}\")\n",
177
        "torch.save(node_user_feat_1, node_user_feat_1_path)\n",
178
        "print(f\"Node[user] feature [feat_1] is saved to {node_user_feat_1_path}\\n\")\n",
179
180
181
182
        "\n",
        "# Generate node[item] feature in numpy array.\n",
        "node_item_feat_0_path = os.path.join(base_dir, \"node-item-feat-0.npy\")\n",
        "node_item_feat_0 = np.random.rand(num_nodes, 5)\n",
183
        "print(f\"Part of node[item] feature [feat_0]: {node_item_feat_0[:3, :]}\")\n",
184
        "np.save(node_item_feat_0_path, node_item_feat_0)\n",
185
        "print(f\"Node[item] feature [feat_0] is saved to {node_item_feat_0_path}\\n\")\n",
186
187
188
189
        "\n",
        "# Generate another node[item] feature in torch tensor\n",
        "node_item_feat_1_path = os.path.join(base_dir, \"node-item-feat-1.pt\")\n",
        "node_item_feat_1 = torch.rand(num_nodes, 5)\n",
190
        "print(f\"Part of node[item] feature [feat_1]: {node_item_feat_1[:3, :]}\")\n",
191
        "torch.save(node_item_feat_1, node_item_feat_1_path)\n",
192
        "print(f\"Node[item] feature [feat_1] is saved to {node_item_feat_1_path}\\n\")\n",
193
194
195
196
        "\n",
        "# Generate edge[user:like:item] feature in numpy array.\n",
        "edge_like_feat_0_path = os.path.join(base_dir, \"edge-like-feat-0.npy\")\n",
        "edge_like_feat_0 = np.random.rand(num_edges, 5)\n",
197
        "print(f\"Part of edge[user:like:item] feature [feat_0]: {edge_like_feat_0[:3, :]}\")\n",
198
        "np.save(edge_like_feat_0_path, edge_like_feat_0)\n",
199
        "print(f\"Edge[user:like:item] feature [feat_0] is saved to {edge_like_feat_0_path}\\n\")\n",
200
201
202
203
        "\n",
        "# Generate another edge[user:like:item] feature in torch tensor\n",
        "edge_like_feat_1_path = os.path.join(base_dir, \"edge-like-feat-1.pt\")\n",
        "edge_like_feat_1 = torch.rand(num_edges, 5)\n",
204
        "print(f\"Part of edge[user:like:item] feature [feat_1]: {edge_like_feat_1[:3, :]}\")\n",
205
        "torch.save(edge_like_feat_1, edge_like_feat_1_path)\n",
206
        "print(f\"Edge[user:like:item] feature [feat_1] is saved to {edge_like_feat_1_path}\\n\")\n",
207
208
209
210
        "\n",
        "# Generate edge[user:follow:user] feature in numpy array.\n",
        "edge_follow_feat_0_path = os.path.join(base_dir, \"edge-follow-feat-0.npy\")\n",
        "edge_follow_feat_0 = np.random.rand(num_edges, 5)\n",
211
        "print(f\"Part of edge[user:follow:user] feature [feat_0]: {edge_follow_feat_0[:3, :]}\")\n",
212
        "np.save(edge_follow_feat_0_path, edge_follow_feat_0)\n",
213
        "print(f\"Edge[user:follow:user] feature [feat_0] is saved to {edge_follow_feat_0_path}\\n\")\n",
214
215
216
217
        "\n",
        "# Generate another edge[user:follow:user] feature in torch tensor\n",
        "edge_follow_feat_1_path = os.path.join(base_dir, \"edge-follow-feat-1.pt\")\n",
        "edge_follow_feat_1 = torch.rand(num_edges, 5)\n",
218
        "print(f\"Part of edge[user:follow:user] feature [feat_1]: {edge_follow_feat_1[:3, :]}\")\n",
219
        "torch.save(edge_follow_feat_1, edge_follow_feat_1_path)\n",
220
        "print(f\"Edge[user:follow:user] feature [feat_1] is saved to {edge_follow_feat_1_path}\\n\")"
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
      ],
      "metadata": {
        "id": "_PVu1u5brBhF"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Generate tasks\n",
        "`OnDiskDataset` supports multiple tasks. For each task, we need to prepare training/validation/test sets respectively. Such sets usually vary among different tasks. In this tutorial, let's create a **Node Classification** task and **Link Prediction** task."
      ],
      "metadata": {
        "id": "ZyqgOtsIwzh_"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Node Classification Task\n",
        "For node classification task, we need **node IDs** and corresponding **labels** for each training/validation/test set. Like feature data, numpy arrays and torch tensors are supported for these sets."
      ],
      "metadata": {
        "id": "hVxHaDIfzCkr"
      }
    },
    {
      "cell_type": "code",
      "source": [
251
        "# For illustration, let's generate item sets for each node type.\n",
252
253
254
255
        "num_trains = int(num_nodes * 0.6)\n",
        "num_vals = int(num_nodes * 0.2)\n",
        "num_tests = num_nodes - num_trains - num_vals\n",
        "\n",
256
257
258
259
260
261
262
263
264
        "user_ids = np.arange(num_nodes)\n",
        "np.random.shuffle(user_ids)\n",
        "\n",
        "item_ids = np.arange(num_nodes)\n",
        "np.random.shuffle(item_ids)\n",
        "\n",
        "# Train IDs for user.\n",
        "nc_train_user_ids_path = os.path.join(base_dir, \"nc-train-user-ids.npy\")\n",
        "nc_train_user_ids = user_ids[:num_trains]\n",
265
        "print(f\"Part of train ids[user] for node classification: {nc_train_user_ids[:3]}\")\n",
266
        "np.save(nc_train_user_ids_path, nc_train_user_ids)\n",
267
        "print(f\"NC train ids[user] are saved to {nc_train_user_ids_path}\\n\")\n",
268
269
270
271
        "\n",
        "# Train labels for user.\n",
        "nc_train_user_labels_path = os.path.join(base_dir, \"nc-train-user-labels.pt\")\n",
        "nc_train_user_labels = torch.randint(0, 10, (num_trains,))\n",
272
        "print(f\"Part of train labels[user] for node classification: {nc_train_user_labels[:3]}\")\n",
273
        "torch.save(nc_train_user_labels, nc_train_user_labels_path)\n",
274
        "print(f\"NC train labels[user] are saved to {nc_train_user_labels_path}\\n\")\n",
275
276
277
278
        "\n",
        "# Train IDs for item.\n",
        "nc_train_item_ids_path = os.path.join(base_dir, \"nc-train-item-ids.npy\")\n",
        "nc_train_item_ids = item_ids[:num_trains]\n",
279
        "print(f\"Part of train ids[item] for node classification: {nc_train_item_ids[:3]}\")\n",
280
        "np.save(nc_train_item_ids_path, nc_train_item_ids)\n",
281
        "print(f\"NC train ids[item] are saved to {nc_train_item_ids_path}\\n\")\n",
282
283
284
285
        "\n",
        "# Train labels for item.\n",
        "nc_train_item_labels_path = os.path.join(base_dir, \"nc-train-item-labels.pt\")\n",
        "nc_train_item_labels = torch.randint(0, 10, (num_trains,))\n",
286
        "print(f\"Part of train labels[item] for node classification: {nc_train_item_labels[:3]}\")\n",
287
        "torch.save(nc_train_item_labels, nc_train_item_labels_path)\n",
288
        "print(f\"NC train labels[item] are saved to {nc_train_item_labels_path}\\n\")\n",
289
290
291
292
        "\n",
        "# Val IDs for user.\n",
        "nc_val_user_ids_path = os.path.join(base_dir, \"nc-val-user-ids.npy\")\n",
        "nc_val_user_ids = user_ids[num_trains:num_trains+num_vals]\n",
293
        "print(f\"Part of val ids[user] for node classification: {nc_val_user_ids[:3]}\")\n",
294
        "np.save(nc_val_user_ids_path, nc_val_user_ids)\n",
295
        "print(f\"NC val ids[user] are saved to {nc_val_user_ids_path}\\n\")\n",
296
297
298
299
        "\n",
        "# Val labels for user.\n",
        "nc_val_user_labels_path = os.path.join(base_dir, \"nc-val-user-labels.pt\")\n",
        "nc_val_user_labels = torch.randint(0, 10, (num_vals,))\n",
300
        "print(f\"Part of val labels[user] for node classification: {nc_val_user_labels[:3]}\")\n",
301
        "torch.save(nc_val_user_labels, nc_val_user_labels_path)\n",
302
        "print(f\"NC val labels[user] are saved to {nc_val_user_labels_path}\\n\")\n",
303
304
305
306
        "\n",
        "# Val IDs for item.\n",
        "nc_val_item_ids_path = os.path.join(base_dir, \"nc-val-item-ids.npy\")\n",
        "nc_val_item_ids = item_ids[num_trains:num_trains+num_vals]\n",
307
        "print(f\"Part of val ids[item] for node classification: {nc_val_item_ids[:3]}\")\n",
308
        "np.save(nc_val_item_ids_path, nc_val_item_ids)\n",
309
        "print(f\"NC val ids[item] are saved to {nc_val_item_ids_path}\\n\")\n",
310
311
312
313
        "\n",
        "# Val labels for item.\n",
        "nc_val_item_labels_path = os.path.join(base_dir, \"nc-val-item-labels.pt\")\n",
        "nc_val_item_labels = torch.randint(0, 10, (num_vals,))\n",
314
        "print(f\"Part of val labels[item] for node classification: {nc_val_item_labels[:3]}\")\n",
315
        "torch.save(nc_val_item_labels, nc_val_item_labels_path)\n",
316
        "print(f\"NC val labels[item] are saved to {nc_val_item_labels_path}\\n\")\n",
317
318
319
320
        "\n",
        "# Test IDs for user.\n",
        "nc_test_user_ids_path = os.path.join(base_dir, \"nc-test-user-ids.npy\")\n",
        "nc_test_user_ids = user_ids[-num_tests:]\n",
321
        "print(f\"Part of test ids[user] for node classification: {nc_test_user_ids[:3]}\")\n",
322
        "np.save(nc_test_user_ids_path, nc_test_user_ids)\n",
323
        "print(f\"NC test ids[user] are saved to {nc_test_user_ids_path}\\n\")\n",
324
325
326
327
        "\n",
        "# Test labels for user.\n",
        "nc_test_user_labels_path = os.path.join(base_dir, \"nc-test-user-labels.pt\")\n",
        "nc_test_user_labels = torch.randint(0, 10, (num_tests,))\n",
328
        "print(f\"Part of test labels[user] for node classification: {nc_test_user_labels[:3]}\")\n",
329
        "torch.save(nc_test_user_labels, nc_test_user_labels_path)\n",
330
        "print(f\"NC test labels[user] are saved to {nc_test_user_labels_path}\\n\")\n",
331
332
333
334
        "\n",
        "# Test IDs for item.\n",
        "nc_test_item_ids_path = os.path.join(base_dir, \"nc-test-item-ids.npy\")\n",
        "nc_test_item_ids = item_ids[-num_tests:]\n",
335
        "print(f\"Part of test ids[item] for node classification: {nc_test_item_ids[:3]}\")\n",
336
        "np.save(nc_test_item_ids_path, nc_test_item_ids)\n",
337
        "print(f\"NC test ids[item] are saved to {nc_test_item_ids_path}\\n\")\n",
338
339
340
341
        "\n",
        "# Test labels for item.\n",
        "nc_test_item_labels_path = os.path.join(base_dir, \"nc-test-item-labels.pt\")\n",
        "nc_test_item_labels = torch.randint(0, 10, (num_tests,))\n",
342
        "print(f\"Part of test labels[item] for node classification: {nc_test_item_labels[:3]}\")\n",
343
        "torch.save(nc_test_item_labels, nc_test_item_labels_path)\n",
344
        "print(f\"NC test labels[item] are saved to {nc_test_item_labels_path}\\n\")"
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
      ],
      "metadata": {
        "id": "S5-fyBbHzTCO"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Link Prediction Task\n",
        "For link prediction task, we need **node pairs** or **negative src/dsts** for each training/validation/test set. Like feature data, numpy arrays and torch tensors are supported for these sets."
      ],
      "metadata": {
        "id": "LhAcDCHQ_KJ0"
      }
    },
    {
      "cell_type": "code",
      "source": [
365
        "# For illustration, let's generate item sets for each edge type.\n",
366
367
368
369
        "num_trains = int(num_edges * 0.6)\n",
        "num_vals = int(num_edges * 0.2)\n",
        "num_tests = num_edges - num_trains - num_vals\n",
        "\n",
370
371
372
        "# Train node pairs for user:like:item.\n",
        "lp_train_like_node_pairs_path = os.path.join(base_dir, \"lp-train-like-node-pairs.npy\")\n",
        "lp_train_like_node_pairs = like_edges[:num_trains, :]\n",
373
        "print(f\"Part of train node pairs[user:like:item] for link prediction: {lp_train_like_node_pairs[:3]}\")\n",
374
        "np.save(lp_train_like_node_pairs_path, lp_train_like_node_pairs)\n",
375
        "print(f\"LP train node pairs[user:like:item] are saved to {lp_train_like_node_pairs_path}\\n\")\n",
376
377
378
379
        "\n",
        "# Train node pairs for user:follow:user.\n",
        "lp_train_follow_node_pairs_path = os.path.join(base_dir, \"lp-train-follow-node-pairs.npy\")\n",
        "lp_train_follow_node_pairs = follow_edges[:num_trains, :]\n",
380
        "print(f\"Part of train node pairs[user:follow:user] for link prediction: {lp_train_follow_node_pairs[:3]}\")\n",
381
        "np.save(lp_train_follow_node_pairs_path, lp_train_follow_node_pairs)\n",
382
        "print(f\"LP train node pairs[user:follow:user] are saved to {lp_train_follow_node_pairs_path}\\n\")\n",
383
384
385
386
        "\n",
        "# Val node pairs for user:like:item.\n",
        "lp_val_like_node_pairs_path = os.path.join(base_dir, \"lp-val-like-node-pairs.npy\")\n",
        "lp_val_like_node_pairs = like_edges[num_trains:num_trains+num_vals, :]\n",
387
        "print(f\"Part of val node pairs[user:like:item] for link prediction: {lp_val_like_node_pairs[:3]}\")\n",
388
        "np.save(lp_val_like_node_pairs_path, lp_val_like_node_pairs)\n",
389
        "print(f\"LP val node pairs[user:like:item] are saved to {lp_val_like_node_pairs_path}\\n\")\n",
390
391
392
393
        "\n",
        "# Val negative dsts for user:like:item.\n",
        "lp_val_like_neg_dsts_path = os.path.join(base_dir, \"lp-val-like-neg-dsts.pt\")\n",
        "lp_val_like_neg_dsts = torch.randint(0, num_nodes, (num_vals, 10))\n",
394
        "print(f\"Part of val negative dsts[user:like:item] for link prediction: {lp_val_like_neg_dsts[:3]}\")\n",
395
        "torch.save(lp_val_like_neg_dsts, lp_val_like_neg_dsts_path)\n",
396
        "print(f\"LP val negative dsts[user:like:item] are saved to {lp_val_like_neg_dsts_path}\\n\")\n",
397
398
399
400
        "\n",
        "# Val node pairs for user:follow:user.\n",
        "lp_val_follow_node_pairs_path = os.path.join(base_dir, \"lp-val-follow-node-pairs.npy\")\n",
        "lp_val_follow_node_pairs = follow_edges[num_trains:num_trains+num_vals, :]\n",
401
        "print(f\"Part of val node pairs[user:follow:user] for link prediction: {lp_val_follow_node_pairs[:3]}\")\n",
402
        "np.save(lp_val_follow_node_pairs_path, lp_val_follow_node_pairs)\n",
403
        "print(f\"LP val node pairs[user:follow:user] are saved to {lp_val_follow_node_pairs_path}\\n\")\n",
404
405
406
407
        "\n",
        "# Val negative dsts for user:follow:user.\n",
        "lp_val_follow_neg_dsts_path = os.path.join(base_dir, \"lp-val-follow-neg-dsts.pt\")\n",
        "lp_val_follow_neg_dsts = torch.randint(0, num_nodes, (num_vals, 10))\n",
408
        "print(f\"Part of val negative dsts[user:follow:user] for link prediction: {lp_val_follow_neg_dsts[:3]}\")\n",
409
        "torch.save(lp_val_follow_neg_dsts, lp_val_follow_neg_dsts_path)\n",
410
        "print(f\"LP val negative dsts[user:follow:user] are saved to {lp_val_follow_neg_dsts_path}\\n\")\n",
411
412
413
        "\n",
        "# Test node paris for user:like:item.\n",
        "lp_test_like_node_pairs_path = os.path.join(base_dir, \"lp-test-like-node-pairs.npy\")\n",
414
        "lp_test_like_node_pairs = like_edges[-num_tests:, :]\n",
415
        "print(f\"Part of test node pairs[user:like:item] for link prediction: {lp_test_like_node_pairs[:3]}\")\n",
416
        "np.save(lp_test_like_node_pairs_path, lp_test_like_node_pairs)\n",
417
        "print(f\"LP test node pairs[user:like:item] are saved to {lp_test_like_node_pairs_path}\\n\")\n",
418
419
420
421
        "\n",
        "# Test negative dsts for user:like:item.\n",
        "lp_test_like_neg_dsts_path = os.path.join(base_dir, \"lp-test-like-neg-dsts.pt\")\n",
        "lp_test_like_neg_dsts = torch.randint(0, num_nodes, (num_tests, 10))\n",
422
        "print(f\"Part of test negative dsts[user:like:item] for link prediction: {lp_test_like_neg_dsts[:3]}\")\n",
423
        "torch.save(lp_test_like_neg_dsts, lp_test_like_neg_dsts_path)\n",
424
        "print(f\"LP test negative dsts[user:like:item] are saved to {lp_test_like_neg_dsts_path}\\n\")\n",
425
426
427
        "\n",
        "# Test node paris for user:follow:user.\n",
        "lp_test_follow_node_pairs_path = os.path.join(base_dir, \"lp-test-follow-node-pairs.npy\")\n",
428
        "lp_test_follow_node_pairs = follow_edges[-num_tests:, :]\n",
429
        "print(f\"Part of test node pairs[user:follow:user] for link prediction: {lp_test_follow_node_pairs[:3]}\")\n",
430
        "np.save(lp_test_follow_node_pairs_path, lp_test_follow_node_pairs)\n",
431
        "print(f\"LP test node pairs[user:follow:user] are saved to {lp_test_follow_node_pairs_path}\\n\")\n",
432
433
434
435
        "\n",
        "# Test negative dsts for user:follow:user.\n",
        "lp_test_follow_neg_dsts_path = os.path.join(base_dir, \"lp-test-follow-neg-dsts.pt\")\n",
        "lp_test_follow_neg_dsts = torch.randint(0, num_nodes, (num_tests, 10))\n",
436
        "print(f\"Part of test negative dsts[user:follow:user] for link prediction: {lp_test_follow_neg_dsts[:3]}\")\n",
437
        "torch.save(lp_test_follow_neg_dsts, lp_test_follow_neg_dsts_path)\n",
438
        "print(f\"LP test negative dsts[user:follow:user] are saved to {lp_test_follow_neg_dsts_path}\\n\")"
439
440
441
442
443
444
445
446
447
448
449
      ],
      "metadata": {
        "id": "u0jCnXIcAQy4"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Organize Data into YAML File\n",
450
451
        "Now we need to create a `metadata.yaml` file which contains the paths, dadta types of graph structure, feature data, training/validation/test sets. Please note that all path should be relative to `metadata.yaml`.\n",
        "\n",
452
453
454
455
456
457
458
459
        "For heterogeneous graph, we need to specify the node/edge type in **type** fields. For edge type, canonical etype is required which is a string that's concatenated by source node type, etype, and destination node type together with `:`.\n",
        "\n",
        "Notes:\n",
        "- all path should be relative to `metadata.yaml`.\n",
        "- Below fields are optional and not specified in below example.\n",
        "  - `in_memory`: indicates whether to load dada into memory or `mmap`. Default is `True`.\n",
        "\n",
        "Please refer to [YAML specification](https://github.com/dmlc/dgl/blob/master/docs/source/stochastic_training/ondisk-dataset-specification.rst) for more details."
460
461
462
463
464
465
466
467
468
469
470
471
      ],
      "metadata": {
        "id": "wbk6-wxRK-6S"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "yaml_content = f\"\"\"\n",
        "    dataset_name: heterogeneous_graph_nc_lp\n",
        "    graph:\n",
        "      nodes:\n",
472
473
474
475
        "        - type: user\n",
        "          num: {num_nodes}\n",
        "        - type: item\n",
        "          num: {num_nodes}\n",
476
        "      edges:\n",
477
478
479
480
481
482
        "        - type: \"user:like:item\"\n",
        "          format: csv\n",
        "          path: {os.path.basename(like_edges_path)}\n",
        "        - type: \"user:follow:user\"\n",
        "          format: csv\n",
        "          path: {os.path.basename(follow_edges_path)}\n",
483
484
        "    feature_data:\n",
        "      - domain: node\n",
485
486
487
488
489
490
491
492
493
494
495
        "        type: user\n",
        "        name: feat_0\n",
        "        format: numpy\n",
        "        path: {os.path.basename(node_user_feat_0_path)}\n",
        "      - domain: node\n",
        "        type: user\n",
        "        name: feat_1\n",
        "        format: torch\n",
        "        path: {os.path.basename(node_user_feat_1_path)}\n",
        "      - domain: node\n",
        "        type: item\n",
496
497
        "        name: feat_0\n",
        "        format: numpy\n",
498
        "        path: {os.path.basename(node_item_feat_0_path)}\n",
499
        "      - domain: node\n",
500
501
502
503
504
505
506
507
508
509
510
        "        type: item\n",
        "        name: feat_1\n",
        "        format: torch\n",
        "        path: {os.path.basename(node_item_feat_1_path)}\n",
        "      - domain: edge\n",
        "        type: \"user:like:item\"\n",
        "        name: feat_0\n",
        "        format: numpy\n",
        "        path: {os.path.basename(edge_like_feat_0_path)}\n",
        "      - domain: edge\n",
        "        type: \"user:like:item\"\n",
511
512
        "        name: feat_1\n",
        "        format: torch\n",
513
        "        path: {os.path.basename(edge_like_feat_1_path)}\n",
514
        "      - domain: edge\n",
515
        "        type: \"user:follow:user\"\n",
516
517
        "        name: feat_0\n",
        "        format: numpy\n",
518
        "        path: {os.path.basename(edge_follow_feat_0_path)}\n",
519
        "      - domain: edge\n",
520
        "        type: \"user:follow:user\"\n",
521
522
        "        name: feat_1\n",
        "        format: torch\n",
523
        "        path: {os.path.basename(edge_follow_feat_1_path)}\n",
524
525
526
527
        "    tasks:\n",
        "      - name: node_classification\n",
        "        num_classes: 10\n",
        "        train_set:\n",
Rhett Ying's avatar
Rhett Ying committed
528
529
530
        "          - type: user\n",
        "            data:\n",
        "              - name: seed_nodes\n",
531
        "                format: numpy\n",
532
        "                path: {os.path.basename(nc_train_user_ids_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
533
        "              - name: labels\n",
534
        "                format: torch\n",
535
        "                path: {os.path.basename(nc_train_user_labels_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
536
537
538
        "          - type: item\n",
        "            data:\n",
        "              - name: seed_nodes\n",
539
540
        "                format: numpy\n",
        "                path: {os.path.basename(nc_train_item_ids_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
541
        "              - name: labels\n",
542
543
        "                format: torch\n",
        "                path: {os.path.basename(nc_train_item_labels_path)}\n",
544
        "        validation_set:\n",
Rhett Ying's avatar
Rhett Ying committed
545
546
547
        "          - type: user\n",
        "            data:\n",
        "              - name: seed_nodes\n",
548
        "                format: numpy\n",
549
        "                path: {os.path.basename(nc_val_user_ids_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
550
        "              - name: labels\n",
551
        "                format: torch\n",
552
        "                path: {os.path.basename(nc_val_user_labels_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
553
554
555
        "          - type: item\n",
        "            data:\n",
        "              - name: seed_nodes\n",
556
557
        "                format: numpy\n",
        "                path: {os.path.basename(nc_val_item_ids_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
558
        "              - name: labels\n",
559
560
        "                format: torch\n",
        "                path: {os.path.basename(nc_val_item_labels_path)}\n",
561
        "        test_set:\n",
Rhett Ying's avatar
Rhett Ying committed
562
563
564
        "          - type: user\n",
        "            data:\n",
        "              - name: seed_nodes\n",
565
        "                format: numpy\n",
566
        "                path: {os.path.basename(nc_test_user_ids_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
567
        "              - name: labels\n",
568
        "                format: torch\n",
569
        "                path: {os.path.basename(nc_test_user_labels_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
570
571
572
        "          - type: item\n",
        "            data:\n",
        "              - name: seed_nodes\n",
573
574
        "                format: numpy\n",
        "                path: {os.path.basename(nc_test_item_ids_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
575
        "              - name: labels\n",
576
577
        "                format: torch\n",
        "                path: {os.path.basename(nc_test_item_labels_path)}\n",
578
579
580
        "      - name: link_prediction\n",
        "        num_classes: 10\n",
        "        train_set:\n",
Rhett Ying's avatar
Rhett Ying committed
581
582
583
        "          - type: \"user:like:item\"\n",
        "            data:\n",
        "              - name: node_pairs\n",
584
585
        "                format: numpy\n",
        "                path: {os.path.basename(lp_train_like_node_pairs_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
586
587
588
        "          - type: \"user:follow:user\"\n",
        "            data:\n",
        "              - name: node_pairs\n",
589
        "                format: numpy\n",
590
        "                path: {os.path.basename(lp_train_follow_node_pairs_path)}\n",
591
        "        validation_set:\n",
Rhett Ying's avatar
Rhett Ying committed
592
593
594
        "          - type: \"user:like:item\"\n",
        "            data:\n",
        "              - name: node_pairs\n",
595
        "                format: numpy\n",
596
        "                path: {os.path.basename(lp_val_like_node_pairs_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
597
        "              - name: negative_dsts\n",
598
        "                format: torch\n",
599
        "                path: {os.path.basename(lp_val_like_neg_dsts_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
600
601
602
        "          - type: \"user:follow:user\"\n",
        "            data:\n",
        "              - name: node_pairs\n",
603
604
        "                format: numpy\n",
        "                path: {os.path.basename(lp_val_follow_node_pairs_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
605
        "              - name: negative_dsts\n",
606
607
        "                format: torch\n",
        "                path: {os.path.basename(lp_val_follow_neg_dsts_path)}\n",
608
        "        test_set:\n",
Rhett Ying's avatar
Rhett Ying committed
609
610
611
        "          - type: \"user:like:item\"\n",
        "            data:\n",
        "              - name: node_pairs\n",
612
613
        "                format: numpy\n",
        "                path: {os.path.basename(lp_test_like_node_pairs_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
614
        "              - name: negative_dsts\n",
615
616
        "                format: torch\n",
        "                path: {os.path.basename(lp_test_like_neg_dsts_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
617
618
619
        "          - type: \"user:follow:user\"\n",
        "            data:\n",
        "              - name: node_pairs\n",
620
        "                format: numpy\n",
621
        "                path: {os.path.basename(lp_test_follow_node_pairs_path)}\n",
Rhett Ying's avatar
Rhett Ying committed
622
        "              - name: negative_dsts\n",
623
        "                format: torch\n",
624
        "                path: {os.path.basename(lp_test_follow_neg_dsts_path)}\n",
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
        "\"\"\"\n",
        "metadata_path = os.path.join(base_dir, \"metadata.yaml\")\n",
        "with open(metadata_path, \"w\") as f:\n",
        "  f.write(yaml_content)"
      ],
      "metadata": {
        "id": "ddGTWW61Lpwp"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Instantiate `OnDiskDataset`\n",
        "Now we're ready to load dataset via `dgl.graphbolt.OnDiskDataset`. When instantiating, we just pass in the base directory where `metadata.yaml` file lies.\n",
        "\n",
        "During first instantiation, GraphBolt preprocesses the raw data such as constructing `FusedCSCSamplingGraph` from edges. All data including graph, feature data, training/validation/test sets are put into `preprocessed` directory after preprocessing. Any following dataset loading will skip the preprocess stage.\n",
        "\n",
        "After preprocessing, `load()` is required to be called explicitly in order to load graph, feature data and tasks."
      ],
      "metadata": {
        "id": "kEfybHGhOW7O"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset = gb.OnDiskDataset(base_dir).load()\n",
        "graph = dataset.graph\n",
655
        "print(f\"Loaded graph: {graph}\\n\")\n",
656
657
        "\n",
        "feature = dataset.feature\n",
658
        "print(f\"Loaded feature store: {feature}\\n\")\n",
659
660
661
        "\n",
        "tasks = dataset.tasks\n",
        "nc_task = tasks[0]\n",
662
        "print(f\"Loaded node classification task: {nc_task}\\n\")\n",
663
        "lp_task = tasks[1]\n",
664
        "print(f\"Loaded link prediction task: {lp_task}\\n\")"
665
666
667
668
669
670
      ],
      "metadata": {
        "id": "W58CZoSzOiyo"
      },
      "execution_count": null,
      "outputs": []
671
672
    }
  ]
673
}