How to obtain edge_norm from a customized dataset when using rgcn

There is a ‘edge_norm’ in the rgcn graph, and in the tutorial it directly obtain from in-built dataset. I am wondering if I have a customized dataset, how should I obtain the ‘edge_norm’? How should I interpret ‘edge_norm’? Thanks

As described above, \frac{1}{c_{i,r}}=\frac{1}{|N_{i}^r|}, where N_i^r is the number of predecessors of i with edge type r.

To compute it, you can do something as follows:

import dgl
import dgl.function as fn
import torch

g = dgl.heterograph({('user', 'plays', 'game'): (torch.tensor([1, 2]), torch.tensor([1, 2])), 
                     ('developer', 'develops', 'game'): (torch.tensor([2, 3, 4]), torch.tensor([1, 1, 2]))})
# Iterate over all canonical edge types
for cano_etype in g.canonical_etypes:
    _, _, dsttype = cano_etype
    # Get the number of predecessors for each node of dsttype with edge type cano_etype
    # Clamp the degrees of isolated nodes to 1 to avoid overflow in division
    dst_degs = g.in_degrees(g.nodes(dsttype), cano_etype).clamp(min=1).float()
    g.nodes[dsttype].data['norm'] = 1. / dst_degs
    g.apply_edges(lambda edges: {'norm': edges.dst['norm']}, etype=cano_etype)
# Remove the intermediate node features
g.ndata.pop('norm')
# The edge_norm is now stored in g.edata.

If you want to work with homogeneous graphs, you can then do

g = dgl.to_homogeneous(g, edata=['norm'])
1 Like

Thanks! It really helps!