Return the adjacency matrix in an external format, such as Scipy or backend dependent sparse tensor. By default, a row of returned adjacency matrix represents the source of an edge and the column represents the destination. When transpose is True, a row represents t
(
self, transpose=False, ctx=F.cpu(), scipy_fmt=None, etype=None
)
| 3836 | ) |
| 3837 | |
| 3838 | def adj_external( |
| 3839 | self, transpose=False, ctx=F.cpu(), scipy_fmt=None, etype=None |
| 3840 | ): |
| 3841 | """Return the adjacency matrix in an external format, such as Scipy or |
| 3842 | backend dependent sparse tensor. |
| 3843 | |
| 3844 | By default, a row of returned adjacency matrix represents the |
| 3845 | source of an edge and the column represents the destination. |
| 3846 | |
| 3847 | When transpose is True, a row represents the destination and a column |
| 3848 | represents the source. |
| 3849 | |
| 3850 | Parameters |
| 3851 | ---------- |
| 3852 | transpose : bool, optional |
| 3853 | A flag to transpose the returned adjacency matrix. (Default: False) |
| 3854 | ctx : context, optional |
| 3855 | The context of returned adjacency matrix. (Default: cpu) |
| 3856 | scipy_fmt : str, optional |
| 3857 | If specified, return a scipy sparse matrix in the given format. |
| 3858 | Otherwise, return a backend dependent sparse tensor. (Default: None) |
| 3859 | etype : str or (str, str, str), optional |
| 3860 | The type names of the edges. The allowed type name formats are: |
| 3861 | |
| 3862 | * ``(str, str, str)`` for source node type, edge type and destination node type. |
| 3863 | * or one ``str`` edge type name if the name can uniquely identify a |
| 3864 | triplet format in the graph. |
| 3865 | |
| 3866 | Can be omitted if the graph has only one type of edges. |
| 3867 | |
| 3868 | Returns |
| 3869 | ------- |
| 3870 | SparseTensor or scipy.sparse.spmatrix |
| 3871 | Adjacency matrix. |
| 3872 | |
| 3873 | Examples |
| 3874 | -------- |
| 3875 | |
| 3876 | The following example uses PyTorch backend. |
| 3877 | |
| 3878 | >>> import dgl |
| 3879 | >>> import torch |
| 3880 | |
| 3881 | Instantiate a heterogeneous graph. |
| 3882 | |
| 3883 | >>> g = dgl.heterograph({ |
| 3884 | ... ('user', 'follows', 'user'): ([0, 1], [0, 1]), |
| 3885 | ... ('developer', 'develops', 'game'): ([0, 1], [0, 2]) |
| 3886 | ... }) |
| 3887 | |
| 3888 | Get a backend dependent sparse tensor. Here we use PyTorch for example. |
| 3889 | |
| 3890 | >>> g.adj_external(etype='develops') |
| 3891 | tensor(indices=tensor([[0, 1], |
| 3892 | [0, 2]]), |
| 3893 | values=tensor([1., 1.]), |
| 3894 | size=(2, 3), nnz=2, layout=torch.sparse_coo) |
| 3895 |