Return the in-degree(s) of the given nodes. It computes the in-degree(s) w.r.t. to the edges of the given edge type. Parameters ---------- v : node IDs The node IDs. The allowed formats are: * ``int``: A single node. * Int Tensor
(self, v=ALL, etype=None)
| 3601 | ) |
| 3602 | |
| 3603 | def in_degrees(self, v=ALL, etype=None): |
| 3604 | """Return the in-degree(s) of the given nodes. |
| 3605 | |
| 3606 | It computes the in-degree(s) w.r.t. to the edges of the given edge type. |
| 3607 | |
| 3608 | Parameters |
| 3609 | ---------- |
| 3610 | v : node IDs |
| 3611 | The node IDs. The allowed formats are: |
| 3612 | |
| 3613 | * ``int``: A single node. |
| 3614 | * Int Tensor: Each element is a node ID. The tensor must have the same device type |
| 3615 | and ID data type as the graph's. |
| 3616 | * iterable[int]: Each element is a node ID. |
| 3617 | |
| 3618 | If not given, return the in-degrees of all the nodes. |
| 3619 | etype : str or (str, str, str), optional |
| 3620 | The type name of the edges. The allowed type name formats are: |
| 3621 | |
| 3622 | * ``(str, str, str)`` for source node type, edge type and destination node type. |
| 3623 | * or one ``str`` edge type name if the name can uniquely identify a |
| 3624 | triplet format in the graph. |
| 3625 | |
| 3626 | Can be omitted if the graph has only one type of edges. |
| 3627 | |
| 3628 | Returns |
| 3629 | ------- |
| 3630 | int or Tensor |
| 3631 | The in-degree(s) of the node(s) in a Tensor. The i-th element is the in-degree |
| 3632 | of the i-th input node. If :attr:`v` is an ``int``, return an ``int`` too. |
| 3633 | |
| 3634 | Examples |
| 3635 | -------- |
| 3636 | The following example uses PyTorch backend. |
| 3637 | |
| 3638 | >>> import dgl |
| 3639 | >>> import torch |
| 3640 | |
| 3641 | Create a homogeneous graph. |
| 3642 | |
| 3643 | >>> g = dgl.graph((torch.tensor([0, 0, 1, 1]), torch.tensor([1, 1, 2, 3]))) |
| 3644 | |
| 3645 | Query for all nodes. |
| 3646 | |
| 3647 | >>> g.in_degrees() |
| 3648 | tensor([0, 2, 1, 1]) |
| 3649 | |
| 3650 | Query for nodes 1 and 2. |
| 3651 | |
| 3652 | >>> g.in_degrees(torch.tensor([1, 2])) |
| 3653 | tensor([2, 1]) |
| 3654 | |
| 3655 | For a graph of multiple edge types, it is required to specify the edge type in query. |
| 3656 | |
| 3657 | >>> hg = dgl.heterograph({ |
| 3658 | ... ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])), |
| 3659 | ... ('user', 'plays', 'game'): (torch.tensor([3, 4]), torch.tensor([5, 6])) |
| 3660 | ... }) |