|
| 1 | +Prepare Data |
| 2 | +============ |
| 3 | + |
| 4 | +In this section, we will prepare the data for the Graphormer model introduced before. We can use any dataset containing :class:`~dgl.DGLGraph` objects and standard PyTorch dataloader to feed the data to the model. The key is to define a collate function to group features of multiple graphs into batches. We show an example of the collate function as follows: |
| 5 | + |
| 6 | + |
| 7 | +.. code:: python |
| 8 | + def collate(graphs): |
| 9 | + # compute shortest path features, can be done in advance |
| 10 | + for g in graphs: |
| 11 | + spd, path = dgl.shortest_dist(g, root=None, return_paths=True) |
| 12 | + g.ndata["spd"] = spd |
| 13 | + g.ndata["path"] = path |
| 14 | +
|
| 15 | + num_graphs = len(graphs) |
| 16 | + num_nodes = [g.num_nodes() for g in graphs] |
| 17 | + max_num_nodes = max(num_nodes) |
| 18 | +
|
| 19 | + attn_mask = th.zeros(num_graphs, max_num_nodes, max_num_nodes) |
| 20 | + node_feat = [] |
| 21 | + in_degree, out_degree = [], [] |
| 22 | + path_data = [] |
| 23 | + # Since shortest_dist returns -1 for unreachable node pairs and padded |
| 24 | + # nodes are unreachable to others, distance relevant to padded nodes |
| 25 | + # use -1 padding as well. |
| 26 | + dist = -th.ones( |
| 27 | + (num_graphs, max_num_nodes, max_num_nodes), dtype=th.long |
| 28 | + ) |
| 29 | +
|
| 30 | + for i in range(num_graphs): |
| 31 | + # A binary mask where invalid positions are indicated by True. |
| 32 | + # Avoid the case where all positions are invalid. |
| 33 | + attn_mask[i, :, num_nodes[i] + 1 :] = 1 |
| 34 | +
|
| 35 | + # +1 to distinguish padded non-existing nodes from real nodes |
| 36 | + node_feat.append(graphs[i].ndata["feat"] + 1) |
| 37 | +
|
| 38 | + # 0 for padding |
| 39 | + in_degree.append( |
| 40 | + th.clamp(graphs[i].in_degrees() + 1, min=0, max=512) |
| 41 | + ) |
| 42 | + out_degree.append( |
| 43 | + th.clamp(graphs[i].out_degrees() + 1, min=0, max=512) |
| 44 | + ) |
| 45 | +
|
| 46 | + # Path padding to make all paths to the same length "max_len". |
| 47 | + path = graphs[i].ndata["path"] |
| 48 | + path_len = path.size(dim=2) |
| 49 | + # shape of shortest_path: [n, n, max_len] |
| 50 | + max_len = 5 |
| 51 | + if path_len >= max_len: |
| 52 | + shortest_path = path[:, :, :max_len] |
| 53 | + else: |
| 54 | + p1d = (0, max_len - path_len) |
| 55 | + # Use the same -1 padding as shortest_dist for |
| 56 | + # invalid edge IDs. |
| 57 | + shortest_path = th.nn.functional.pad(path, p1d, "constant", -1) |
| 58 | + pad_num_nodes = max_num_nodes - num_nodes[i] |
| 59 | + p3d = (0, 0, 0, pad_num_nodes, 0, pad_num_nodes) |
| 60 | + shortest_path = th.nn.functional.pad(shortest_path, p3d, "constant", -1) |
| 61 | + # +1 to distinguish padded non-existing edges from real edges |
| 62 | + edata = graphs[i].edata["feat"] + 1 |
| 63 | +
|
| 64 | + # shortest_dist pads non-existing edges (at the end of shortest |
| 65 | + # paths) with edge IDs -1, and th.zeros(1, edata.shape[1]) stands |
| 66 | + # for all padded edge features. |
| 67 | + edata = th.cat( |
| 68 | + (edata, th.zeros(1, edata.shape[1]).to(edata.device)), dim=0 |
| 69 | + ) |
| 70 | + path_data.append(edata[shortest_path]) |
| 71 | +
|
| 72 | + dist[i, : num_nodes[i], : num_nodes[i]] = graphs[i].ndata["spd"] |
| 73 | +
|
| 74 | + # node feat padding |
| 75 | + node_feat = th.nn.utils.rnn.pad_sequence(node_feat, batch_first=True) |
| 76 | +
|
| 77 | + # degree padding |
| 78 | + in_degree = th.nn.utils.rnn.pad_sequence(in_degree, batch_first=True) |
| 79 | + out_degree = th.nn.utils.rnn.pad_sequence(out_degree, batch_first=True) |
| 80 | +
|
| 81 | + return ( |
| 82 | + node_feat, |
| 83 | + in_degree, |
| 84 | + out_degree, |
| 85 | + attn_mask, |
| 86 | + th.stack(path_data), |
| 87 | + dist, |
| 88 | + ) |
| 89 | +
|
| 90 | +In this example, we also omit details like the addition of a virtual node. For more details, please refer to the `Graphormer example <https://github.com/dmlc/dgl/tree/master/examples/core/Graphormer>`_. |
0 commit comments