Get the adjacency matrix of the graph. Parameters ---------- etype : str or (str, str, str), optional The type names of the edges. The allowed type name formats are: * ``(str, str, str)`` for source node type, edge type and destination no
(self, etype=None, eweight_name=None)
| 3761 | return self.adj(etype) |
| 3762 | |
| 3763 | def adj(self, etype=None, eweight_name=None): |
| 3764 | """Get the adjacency matrix of the graph. |
| 3765 | |
| 3766 | Parameters |
| 3767 | ---------- |
| 3768 | etype : str or (str, str, str), optional |
| 3769 | The type names of the edges. The allowed type name formats are: |
| 3770 | |
| 3771 | * ``(str, str, str)`` for source node type, edge type and |
| 3772 | destination node type. |
| 3773 | * or one ``str`` edge type name if the name can uniquely identify a |
| 3774 | triplet format in the graph. |
| 3775 | |
| 3776 | Can be omitted if the graph has only one type of edges. |
| 3777 | |
| 3778 | eweight_name : str, optional |
| 3779 | The name of edge feature used as the non-zero values. If not given, |
| 3780 | the non-zero values are all 1. |
| 3781 | |
| 3782 | Returns |
| 3783 | ------- |
| 3784 | SparseMatrix |
| 3785 | The adjacency matrix. |
| 3786 | |
| 3787 | Examples |
| 3788 | -------- |
| 3789 | |
| 3790 | The following example uses PyTorch backend. |
| 3791 | |
| 3792 | >>> import dgl |
| 3793 | >>> import torch |
| 3794 | |
| 3795 | >>> g = dgl.graph(([0, 1, 2], [1, 2, 3])) |
| 3796 | >>> g.adj() |
| 3797 | SparseMatrix(indices=tensor([[0, 1, 2], |
| 3798 | [1, 2, 3]]), |
| 3799 | values=tensor([1., 1., 1.]), |
| 3800 | shape=(4, 4), nnz=3) |
| 3801 | |
| 3802 | >>> g = dgl.heterograph({ |
| 3803 | ... ('user', 'follows', 'user'): ([0, 1], [0, 1]), |
| 3804 | ... ('developer', 'develops', 'game'): ([0, 1], [0, 2]) |
| 3805 | ... }) |
| 3806 | |
| 3807 | >>> g.adj(etype='develops') |
| 3808 | SparseMatrix(indices=tensor([[0, 1], |
| 3809 | [0, 2]]), |
| 3810 | values=tensor([1., 1.]), |
| 3811 | shape=(2, 3), nnz=2) |
| 3812 | >>> g.edata['h'] = {('user', 'follows', 'user'): torch.tensor([3, 2])} |
| 3813 | >>> g.adj(etype='follows', eweight_name='h') |
| 3814 | SparseMatrix(indices=tensor([[0, 1], |
| 3815 | [0, 1]]), |
| 3816 | values=tensor([3, 2]), |
| 3817 | shape=(2, 2), nnz=2) |
| 3818 | """ |
| 3819 | assert F.backend_name == "pytorch", "Only PyTorch backend supports adj." |
| 3820 | # Temporal fix to introduce a dependency on torch |