Dgl.heterograph does not preserve node indices across edge types

So according to this: Working with Heterogeneous Graphs — DGL 0.6.1 documentation I define a heterograph by specifying a dictionary with edges. I have done this but I’m not sure the graph returned is correct or what I expected.

My code is this:

g = dgl.heterograph(data_dict)

src_1 = torch.tensor([10,  8,  5,  7,  6,  0,  1,  2,  3], dtype=torch.int64)
dst_1 = torch.tensor([11,  9,  9,  9,  9,  1,  4,  3,  4], dtype=torch.int64)

src_2 = torch.tensor([13, 14, 15], dtype=torch.int64)
dst_2 = torch.tensor([12, 12, 12], dtype=torch.int64)

src_3 = torch.tensor([ 4,  9, 11], dtype=torch.int64)
dst_3 = torch.tensor([13, 14, 15], dtype=torch.int64)


data_dict = {
    ('x', 'connects', 'x'): (src_1, dst_1),
    ('y', 'connects', 'y'): (src_2, dst_2),
    ('x', 'in', 'y'): (src_3, dst_3)
}

g2 = dgl.heterograph(data_dict)
print(g2)
Graph(num_nodes={'x': 12, 'y': 16},
      num_edges={('x', 'connects', 'x'): 9, ('x', 'in', 'y'): 3, ('y', 'connects', 'y'): 3},
      metagraph=[('x', 'x', 'connects'), ('x', 'y', 'in'), ('y', 'y', 'connects')])

This does not make sense to me, how I interpreted it my graph should contain 11 nodes of type ‘x’ and 4 nodes of type ‘y’.

Nodes of type ‘x’ have indices 1 through 11, while nodes of type ‘y’ have indices 12 through 15.

The green nodes are nodes of type ‘x’ and the blue nodes are nodes of type ‘y’. The indices are mapped using a dictionary, so they don’t correspond to my tensors.

How can I construct a heterogeneous graph with shared indices between edge types? Or am I misunderstanding whatever the printing of the graph is trying to convery.

dgl by default considers the number of nodes as largest id+1, so the y’s nodes are added from 0 to 15 as 15 is the max in src_2 for y
you could use num_nodes_dict as in the heterograph documentation to override this behaviour

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.