Support initial node features in RGCN

Hi all,
The example code for RGCN assumes that there are no initial features for each node, thus they made an “embedding lookup” on weights matrix, see the following code (line 86 of https://github.com/dmlc/dgl/blob/master/examples/pytorch/rgcn/layers.py )

        if self.is_input_layer:
            def msg_func(edges):
                # for input layer, matrix multiply can be converted to be
                # an embedding lookup using source node id
                embed = weight.view(-1, self.out_feat)
                index = edges.data['type'] * self.in_feat + edges.src['id']
                return {'msg': embed.index_select(0, index) * edges.data['norm']}

However, in my case, I have the initial features for each node, how to modify the code to fit that?

You can use g.ndata['feat']=th.tensor(...) to set initial feature for nodes

Hi,

Could you elaborate a little bit more on how to use initial node features with RGCN?

Thanks in advance!

Hi @thibaudm,

To use initial node features, you first need to set features in DGL graph. you can simply do something like g.ndata['feat']=... before calling model.forward() in your main training loop. Then you should change the message function to use your node feature. For example, the code snippet in the question above checks if it’s currently executing the first layer, in which case there is no node features available. So you can just remove the if condition, and always use your node feature in message function.

I see. Thank you for your reply @lingfan!

Here is an implementation of RGCN that takes into account node features. As it is extremely memory demanding I tested it on Zachary’s Karate Club. I hope it may be helpful to some people.

3 Likes

Hey @thibaudm, that’s very interesting! I’d like to put this example in our R-GCN tutorial page. Would you like to contribute?

It seems only have edge features (edge[‘norm’]), but not nodes’ featuers.
All in all, thanks for your help @thibaudm Could you tell me how you change the message passing function? and I wonder how to adapt it for nodes’ features? If my nodes have different level and different shape tensor.