Return a node view One can use it for: 1. Getting the node IDs for a single node type. 2. Setting/getting features for all nodes of a single node type. Examples -------- The following example uses PyTorch backend. >>> import dgl >>>
(self)
| 1776 | |
| 1777 | @property |
| 1778 | def nodes(self): |
| 1779 | """Return a node view |
| 1780 | |
| 1781 | One can use it for: |
| 1782 | |
| 1783 | 1. Getting the node IDs for a single node type. |
| 1784 | 2. Setting/getting features for all nodes of a single node type. |
| 1785 | |
| 1786 | Examples |
| 1787 | -------- |
| 1788 | The following example uses PyTorch backend. |
| 1789 | |
| 1790 | >>> import dgl |
| 1791 | >>> import torch |
| 1792 | |
| 1793 | Create a homogeneous graph and a heterogeneous graph of two node types. |
| 1794 | |
| 1795 | >>> g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2]))) |
| 1796 | >>> hg = dgl.heterograph({ |
| 1797 | ... ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])), |
| 1798 | ... ('user', 'plays', 'game'): (torch.tensor([3, 4]), torch.tensor([5, 6])) |
| 1799 | ... }) |
| 1800 | |
| 1801 | Get the node IDs of the homogeneous graph. |
| 1802 | |
| 1803 | >>> g.nodes() |
| 1804 | tensor([0, 1, 2]) |
| 1805 | |
| 1806 | Get the node IDs of the heterogeneous graph. With multiple node types introduced, |
| 1807 | one needs to specify the node type for query. |
| 1808 | |
| 1809 | >>> hg.nodes('user') |
| 1810 | tensor([0, 1, 2, 3, 4]) |
| 1811 | |
| 1812 | Set and get a feature 'h' for all nodes of a single type in the heterogeneous graph. |
| 1813 | |
| 1814 | >>> hg.nodes['user'].data['h'] = torch.ones(5, 1) |
| 1815 | >>> hg.nodes['user'].data['h'] |
| 1816 | tensor([[1.], [1.], [1.], [1.], [1.]]) |
| 1817 | |
| 1818 | To set node features for a graph with a single node type, use :func:`DGLGraph.ndata`. |
| 1819 | |
| 1820 | See Also |
| 1821 | -------- |
| 1822 | ndata |
| 1823 | """ |
| 1824 | # Todo (Mufei) Replace the syntax g.nodes[...].ndata[...] with g.nodes[...][...] |
| 1825 | return HeteroNodeView(self, self.get_ntype_id) |
| 1826 | |
| 1827 | @property |
| 1828 | def srcnodes(self): |
no test coverage detected