Create a heterogeneous graph and return. Parameters ---------- data_dict : graph data The dictionary data for constructing a heterogeneous graph. The keys are in the form of string triplets (src_type, edge_type, dst_type), specifying the source node, edge, and de
(data_dict, num_nodes_dict=None, idtype=None, device=None)
| 206 | |
| 207 | |
| 208 | def heterograph(data_dict, num_nodes_dict=None, idtype=None, device=None): |
| 209 | """Create a heterogeneous graph and return. |
| 210 | |
| 211 | Parameters |
| 212 | ---------- |
| 213 | data_dict : graph data |
| 214 | The dictionary data for constructing a heterogeneous graph. The keys are in the form of |
| 215 | string triplets (src_type, edge_type, dst_type), specifying the source node, |
| 216 | edge, and destination node types. The values are graph data in the form of |
| 217 | :math:`(U, V)`, where :math:`(U[i], V[i])` forms the edge with ID :math:`i`. |
| 218 | The allowed graph data formats are: |
| 219 | |
| 220 | - ``(Tensor, Tensor)``: Each tensor must be a 1D tensor containing node IDs. DGL calls |
| 221 | this format "tuple of node-tensors". The tensors should have the same data type, |
| 222 | which must be either int32 or int64. They should also have the same device context |
| 223 | (see below the descriptions of :attr:`idtype` and :attr:`device`). |
| 224 | - ``('coo', (Tensor, Tensor))``: Same as ``(Tensor, Tensor)``. |
| 225 | - ``('csr', (Tensor, Tensor, Tensor))``: The three tensors form the CSR representation |
| 226 | of the graph's adjacency matrix. The first one is the row index pointer. The |
| 227 | second one is the column indices. The third one is the edge IDs, which can be empty |
| 228 | (i.e. with 0 elements) to represent consecutive integer IDs starting from 0. |
| 229 | - ``('csc', (Tensor, Tensor, Tensor))``: The three tensors form the CSC representation |
| 230 | of the graph's adjacency matrix. The first one is the column index pointer. The |
| 231 | second one is the row indices. The third one is the edge IDs, which can be empty |
| 232 | to represent consecutive integer IDs starting from 0. |
| 233 | |
| 234 | The tensors can be replaced with any iterable of integers (e.g. list, tuple, |
| 235 | numpy.ndarray). |
| 236 | num_nodes_dict : dict[str, int], optional |
| 237 | The number of nodes for some node types, which is a dictionary mapping a node type |
| 238 | :math:`T` to the number of :math:`T`-typed nodes. If not given for a node type |
| 239 | :math:`T`, DGL finds the largest ID appearing in *every* graph data whose source |
| 240 | or destination node type is :math:`T`, and sets the number of nodes to be that ID |
| 241 | plus one. If given and the value is no greater than the largest ID for some node type, |
| 242 | DGL will raise an error. By default, DGL infers the number of nodes for all node types. |
| 243 | idtype : int32 or int64, optional |
| 244 | The data type for storing the structure-related graph information such as node and |
| 245 | edge IDs. It should be a framework-specific data type object (e.g., ``torch.int32``). |
| 246 | If ``None`` (default), DGL infers the ID type from the :attr:`data_dict` argument. |
| 247 | device : device context, optional |
| 248 | The device of the returned graph, which should be a framework-specific device object |
| 249 | (e.g., ``torch.device``). If ``None`` (default), DGL uses the device of the tensors of |
| 250 | the :attr:`data` argument. If :attr:`data` is not a tuple of node-tensors, the |
| 251 | returned graph is on CPU. If the specified :attr:`device` differs from that of the |
| 252 | provided tensors, it casts the given tensors to the specified device first. |
| 253 | |
| 254 | Returns |
| 255 | ------- |
| 256 | DGLGraph |
| 257 | The created graph. |
| 258 | |
| 259 | Notes |
| 260 | ----- |
| 261 | 1. If the :attr:`idtype` argument is not given then: |
| 262 | |
| 263 | - in the case of the tuple of node-tensor format, DGL uses |
| 264 | the data type of the given ID tensors. |
| 265 | - in the case of the tuple of sequence format, DGL uses int64. |
no test coverage detected