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 )

        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 =['type'] * self.in_feat + edges.src['id']
                return {'msg': embed.index_select(0, index) *['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


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.


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