What is the 'h' key?

g.srcdata['h'] and edges.src['h'] are used all over the documentation and source code. however, unlike ‘u’, ‘v’, ‘e’, ‘m’ - I haven’t seen it defined https://docs.dgl.ai/api/python/function.html?highlight=src

GitHub won’t let me search the source code for ['h'] to see where it is set.

If I had to guess, ‘h’ is some hidden tensor about how the nodes are connected like the adjacency or laplacian matrix? Perhaps something to do with in_degrees() which is also obscure for me.

When I do g.ndata I don’t see h listed as a key. Is it calculated on the fly if I happen to pass a parameter to the feat part of the forward in GraphConv? https://docs.dgl.ai/_modules/dgl/nn/pytorch/conv/graphconv.html#GraphConv

h is not a keyword in dgl, we just use it to denote features (as you suggested, h is short for hidden).

You may have noticed, in every function we use something like g.srcdata['h']/g.ndata['h'], the field must have been assigned in the same function before its usage. For example, in the GraphConv module you mentioned, we assign the h field in:

        if self._in_feats > self._out_feats:
            # mult W first to reduce the feature size for aggregation.
            if weight is not None:
                feat = th.matmul(feat, weight)
            graph.srcdata['h'] = feat
            graph.update_all(fn.copy_src(src='h', out='m'),
                             fn.sum(msg='m', out='h'))
            rst = graph.dstdata['h']
        else:
            # aggregate first then mult W
            graph.srcdata['h'] = feat
            graph.update_all(fn.copy_src(src='h', out='m'),
                             fn.sum(msg='m', out='h'))
            rst = graph.dstdata['h']
            if weight is not None:
                rst = th.matmul(rst, weight)

Of course you can use other names such as x, y, z, the result would not be influenced.

@zihao should these letters ‘u’, ‘v’, ‘e’, ‘m’, 'h' be treated as reserved words in the node and edge dictionary?

If a user was working with a feature like ‘height’ should they avoid using ‘h’ as a key for ndata?

No these are absolutely not reserved words, feel free to use other keys you like, you can regard node/edge frames as ordinary dictionaries in Python.

If you concern about “pollution” problem (for example, in dgl’s GraphConv we use h field, what if you use the same key outside), note that we provide local_scope method: https://docs.dgl.ai/generated/dgl.DGLGraph.local_scope.html?highlight=local_scope#dgl.DGLGraph.local_scope, which means node/edge frame assignment under the scope would not influence the data frame outside.

1 Like

Thank you. I just didn’t know if local_scope was being used in all situations.