Combine all node types of DGL Heterogeneous graph

Hi,
I have a Heterogeneous graph of two node types. Ex: {’_ID’: tensor([0, 1, 0, 1, 2, 3]), ‘_TYPE’: tensor([0, 0, 1, 1, 1, 1])}. Can I use DGL library and get the below ones -

  1. I would like to combine the above two node types and want to see node numbers like - [0, 1, 2, 3, 4, 5].

  2. Whenever I want, I should get the node type of a given node number, i.e., XX.node[5] = 1. In other words, mapping between these continuous nodes and Heterogeneous graph node types.

The requirement is:
I will use the DGL library to store the heterogeneous graph data along with attributes.
Convert the nodes with different types to continuous nodes and use these nodes for index table construction.

Can you try dgl.to_homogeneous — DGL 1.2 documentation? it should do what you want. Let us know if you encounter any trouble. Thanks.

import dgl
import torch as th

u_p_p_list =[0,1,2,0]
v_p_p_list =[1,0,0,2]

u_p_c_list =[2,3]
v_p_c_list =[1,0]

graph_data = {
(‘person’, ‘IS_FRIENDS_WITH’, ‘person’): (th.tensor(u_p_p_list), th.tensor(v_p_p_list)),
(‘person’, ‘WORKS_FOR’, ‘company’): (th.tensor(u_p_c_list), th.tensor(v_p_c_list))

}
g_hetero= dgl.heterograph(graph_data)

g_homo = dgl.to_homogeneous(g_hetero)

print(g_homo.ndata)

Output:
{’_ID’: tensor([0, 1, 0, 1, 2, 3]), ‘_TYPE’: tensor([0, 0, 1, 1, 1, 1])}

Expected:
[0, 1, 2, 3, 4, 5].

#To get the above-expected output
print(g_homo.nodes())

#To get the node tensor and node type of a given node
print(g_homo.nodes[4])
#Output:
#NodeSpace(data={’_ID’: tensor([2]), ‘_TYPE’: tensor([1])})

Question: How to get the reverse way - How to get the homogeneous graph node number for a given node type and node tensor value?

If you want to get continuous ids [0, 1, 2, 3, 4, 5], you can easily do:

torch.arange(0,n)

make n the number of the total node ids.

I am not sure whether this is what you want, can you clarify a little bit?

g_homo = dgl.to_homogeneous(g_hetero, store_type=True)

homo_to_hetero_id = g_homo.ndata[’_ID’]
homo_to_hetero_type = g_homo.ndata[’_TYPE’]

def get_homogeneous_node_number(graph, node_type, tensor_value):
matched_nodes = (homo_to_hetero_type == node_type) & (graph.ndata[’_ID’] == tensor_value)
homogeneous_node_numbers = (th.nonzero(matched_nodes)).item()
return homogeneous_node_numbers

node_type = 1
tensor_value = 2

homogeneous_node_number = get_homogeneous_node_number(g_homo, node_type, tensor_value)

print(homogeneous_node_number)

I have written a piece of code (see above) to get the homogeneous node number for a given:
1)homogeneous graph
2)nod type
3) tensor value

Question: Any better way to get the homogeneous node number? OR the above piece of code is the only way?

I don’t see any problem of the code, if it gets what you want, I don’t see a better way to re-write it.
However, I am not able to understand your goal with your description. If you can give a clear example input and output, I might be able to double check.

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