Return a node data view for setting/getting source node features. Let ``g`` be a DGLGraph. If ``g`` is a graph of a single source node type, ``g.srcdata[feat]`` returns the source node feature associated with the name ``feat``. One can also set a source node feature associat
(self)
| 2022 | |
| 2023 | @property |
| 2024 | def srcdata(self): |
| 2025 | """Return a node data view for setting/getting source node features. |
| 2026 | |
| 2027 | Let ``g`` be a DGLGraph. If ``g`` is a graph of a single source node type, |
| 2028 | ``g.srcdata[feat]`` returns the source node feature associated with the name ``feat``. |
| 2029 | One can also set a source node feature associated with the name ``feat`` by |
| 2030 | setting ``g.srcdata[feat]`` to a tensor. |
| 2031 | |
| 2032 | If ``g`` is a graph of multiple source node types, ``g.srcdata[feat]`` returns a |
| 2033 | dict[str, Tensor] mapping source node types to the node features associated with |
| 2034 | the name ``feat`` for the corresponding type. One can also set a node feature |
| 2035 | associated with the name ``feat`` for some source node type(s) by setting |
| 2036 | ``g.srcdata[feat]`` to a dictionary as described. |
| 2037 | |
| 2038 | Notes |
| 2039 | ----- |
| 2040 | For setting features, the device of the features must be the same as the device |
| 2041 | of the graph. |
| 2042 | |
| 2043 | Examples |
| 2044 | -------- |
| 2045 | The following example uses PyTorch backend. |
| 2046 | |
| 2047 | >>> import dgl |
| 2048 | >>> import torch |
| 2049 | |
| 2050 | Set and get feature 'h' for a graph of a single source node type. |
| 2051 | |
| 2052 | >>> g = dgl.heterograph({ |
| 2053 | ... ('user', 'plays', 'game'): (torch.tensor([0, 1]), torch.tensor([1, 2]))}) |
| 2054 | >>> g.srcdata['h'] = torch.ones(2, 1) |
| 2055 | >>> g.srcdata['h'] |
| 2056 | tensor([[1.], |
| 2057 | [1.]]) |
| 2058 | |
| 2059 | Set and get feature 'h' for a graph of multiple source node types. |
| 2060 | |
| 2061 | >>> g = dgl.heterograph({ |
| 2062 | ... ('user', 'plays', 'game'): (torch.tensor([1, 2]), torch.tensor([3, 4])), |
| 2063 | ... ('player', 'plays', 'game'): (torch.tensor([2, 2]), torch.tensor([1, 1])) |
| 2064 | ... }) |
| 2065 | >>> g.srcdata['h'] = {'user': torch.zeros(3, 1), 'player': torch.ones(3, 1)} |
| 2066 | >>> g.srcdata['h'] |
| 2067 | {'player': tensor([[1.], [1.], [1.]]), |
| 2068 | 'user': tensor([[0.], [0.], [0.]])} |
| 2069 | >>> g.srcdata['h'] = {'user': torch.ones(3, 1)} |
| 2070 | >>> g.srcdata['h'] |
| 2071 | {'player': tensor([[1.], [1.], [1.]]), |
| 2072 | 'user': tensor([[1.], [1.], [1.]])} |
| 2073 | |
| 2074 | See Also |
| 2075 | -------- |
| 2076 | nodes |
| 2077 | ndata |
| 2078 | srcnodes |
| 2079 | """ |
| 2080 | if len(self.srctypes) == 1: |
| 2081 | ntype = self.srctypes[0] |
nothing calls this directly
no test coverage detected