How to add several nodes of a new type in a heterograph?

Hi, I want to add several nodes of a new type in a constructed heterograph? I tried this with the following code:

import dgl
import torch

g = dgl.heterograph({
    ('user', 'plays', 'game'): (torch.tensor([0, 1, 1, 2]),
                                torch.tensor([0, 0, 1, 1])),
    ('developer', 'develops', 'game'): (torch.tensor([0, 1]),
                                        torch.tensor([0, 1]))
    })

dgl.add_nodes(g, 2, ntype='player')

Then it will return this error:

---------------------------------------------------------------------------
DGLError                                  Traceback (most recent call last)
<ipython-input-41-e2129c6a370f> in <module>
----> 1 dgl.add_nodes(g, 2, ntype='player')

~/anaconda3/lib/python3.7/site-packages/dgl/transform.py in add_nodes(g, num, data, ntype)
   1008     """
   1009     g = g.clone()
-> 1010     g.add_nodes(num, data=data, ntype=ntype)
   1011     return g
   1012 

~/anaconda3/lib/python3.7/site-packages/dgl/heterograph.py in add_nodes(self, num, data, ntype)
    296 
    297         assert num > 0, 'Number of new nodes should be larger than one.'
--> 298         ntid = self.get_ntype_id(ntype)
    299         # update graph idx
    300         metagraph = self._graph.metagraph

~/anaconda3/lib/python3.7/site-packages/dgl/heterograph.py in get_ntype_id(self, ntype)
   1075         ntid = self._srctypes_invmap.get(ntype, self._dsttypes_invmap.get(ntype, None))
   1076         if ntid is None:
-> 1077             raise DGLError('Node type "{}" does not exist.'.format(ntype))
   1078         return ntid
   1079 

DGLError: Node type "player" does not exist.

Is there some built-in function could implement this problem? Or do I need to construct a new graph?

As mentioned in point 3 of FAQ, you need to construct a new graph.

1 Like

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