Return a node view for source nodes If the graph is a uni-bipartite graph (see :func:`is_unibipartite` for reference), this is :func:`nodes` restricted to source node types. Otherwise, it is an alias for :func:`nodes`. One can use it for: 1. Getting the nod
(self)
| 1826 | |
| 1827 | @property |
| 1828 | def srcnodes(self): |
| 1829 | """Return a node view for source nodes |
| 1830 | |
| 1831 | If the graph is a uni-bipartite graph (see :func:`is_unibipartite` for reference), |
| 1832 | this is :func:`nodes` restricted to source node types. Otherwise, it is an alias |
| 1833 | for :func:`nodes`. |
| 1834 | |
| 1835 | One can use it for: |
| 1836 | |
| 1837 | 1. Getting the node IDs for a single node type. |
| 1838 | 2. Setting/getting features for all nodes of a single node type. |
| 1839 | |
| 1840 | Examples |
| 1841 | -------- |
| 1842 | The following example uses PyTorch backend. |
| 1843 | |
| 1844 | >>> import dgl |
| 1845 | >>> import torch |
| 1846 | |
| 1847 | Create a uni-bipartite graph. |
| 1848 | |
| 1849 | >>> g = dgl.heterograph({ |
| 1850 | ... ('user', 'plays', 'game'): (torch.tensor([0]), torch.tensor([1])), |
| 1851 | ... ('developer', 'develops', 'game'): (torch.tensor([1]), torch.tensor([2])) |
| 1852 | ... }) |
| 1853 | |
| 1854 | Get the node IDs for source node types. |
| 1855 | |
| 1856 | >>> g.srcnodes('user') |
| 1857 | tensor([0]) |
| 1858 | >>> g.srcnodes('developer') |
| 1859 | tensor([0, 1]) |
| 1860 | |
| 1861 | Set/get features for source node types. |
| 1862 | |
| 1863 | >>> g.srcnodes['user'].data['h'] = torch.ones(1, 1) |
| 1864 | >>> g.srcnodes['user'].data['h'] |
| 1865 | tensor([[1.]]) |
| 1866 | |
| 1867 | Create a graph that is not uni-bipartite. |
| 1868 | |
| 1869 | >>> g = dgl.heterograph({ |
| 1870 | ... ('user', 'follows', 'user'): (torch.tensor([0]), torch.tensor([1])), |
| 1871 | ... ('developer', 'develops', 'game'): (torch.tensor([1]), torch.tensor([2])) |
| 1872 | ... }) |
| 1873 | |
| 1874 | :func:`dgl.DGLGraph.srcnodes` falls back to :func:`dgl.DGLGraph.nodes` and one can |
| 1875 | get the node IDs for both source and destination node types. |
| 1876 | |
| 1877 | >>> g.srcnodes('game') |
| 1878 | tensor([0, 1, 2]) |
| 1879 | |
| 1880 | One can also set/get features for destination node types in this case. |
| 1881 | |
| 1882 | >>> g.srcnodes['game'].data['h'] = torch.ones(3, 1) |
| 1883 | >>> g.srcnodes['game'].data['h'] |
| 1884 | tensor([[1.], |
| 1885 | [1.], |