Return a node view for destination nodes If the graph is a uni-bipartite graph (see :func:`is_unibipartite` for reference), this is :func:`nodes` restricted to destination node types. Otherwise, it is an alias for :func:`nodes`. One can use it for: 1. Getti
(self)
| 1893 | |
| 1894 | @property |
| 1895 | def dstnodes(self): |
| 1896 | """Return a node view for destination nodes |
| 1897 | |
| 1898 | If the graph is a uni-bipartite graph (see :func:`is_unibipartite` for reference), |
| 1899 | this is :func:`nodes` restricted to destination node types. Otherwise, it is an alias |
| 1900 | for :func:`nodes`. |
| 1901 | |
| 1902 | One can use it for: |
| 1903 | |
| 1904 | 1. Getting the node IDs for a single node type. |
| 1905 | 2. Setting/getting features for all nodes of a single node type. |
| 1906 | |
| 1907 | Examples |
| 1908 | -------- |
| 1909 | The following example uses PyTorch backend. |
| 1910 | |
| 1911 | >>> import dgl |
| 1912 | >>> import torch |
| 1913 | |
| 1914 | Create a uni-bipartite graph. |
| 1915 | |
| 1916 | >>> g = dgl.heterograph({ |
| 1917 | ... ('user', 'plays', 'game'): (torch.tensor([0]), torch.tensor([1])), |
| 1918 | ... ('developer', 'develops', 'game'): (torch.tensor([1]), torch.tensor([2])) |
| 1919 | ... }) |
| 1920 | |
| 1921 | Get the node IDs for destination node types. |
| 1922 | |
| 1923 | >>> g.dstnodes('game') |
| 1924 | tensor([0, 1, 2]) |
| 1925 | |
| 1926 | Set/get features for destination node types. |
| 1927 | |
| 1928 | >>> g.dstnodes['game'].data['h'] = torch.ones(3, 1) |
| 1929 | >>> g.dstnodes['game'].data['h'] |
| 1930 | tensor([[1.], |
| 1931 | [1.], |
| 1932 | [1.]]) |
| 1933 | |
| 1934 | Create a graph that is not uni-bipartite. |
| 1935 | |
| 1936 | >>> g = dgl.heterograph({ |
| 1937 | ... ('user', 'follows', 'user'): (torch.tensor([0]), torch.tensor([1])), |
| 1938 | ... ('developer', 'develops', 'game'): (torch.tensor([1]), torch.tensor([2])) |
| 1939 | ... }) |
| 1940 | |
| 1941 | :func:`dgl.DGLGraph.dstnodes` falls back to :func:`dgl.DGLGraph.nodes` and one can |
| 1942 | get the node IDs for both source and destination node types. |
| 1943 | |
| 1944 | >>> g.dstnodes('developer') |
| 1945 | tensor([0, 1]) |
| 1946 | |
| 1947 | One can also set/get features for source node types in this case. |
| 1948 | |
| 1949 | >>> g.dstnodes['developer'].data['h'] = torch.ones(2, 1) |
| 1950 | >>> g.dstnodes['developer'].data['h'] |
| 1951 | tensor([[1.], |
| 1952 | [1.]]) |