Return the number of edges for each graph in the batch with the specified edge type. Parameters ---------- etype : str or tuple of str, optional The edge type for query, which can be an edge type (str) or a canonical edge type (3-tuple of str). When a
(self, etype=None)
| 1611 | self._batch_num_nodes = val |
| 1612 | |
| 1613 | def batch_num_edges(self, etype=None): |
| 1614 | """Return the number of edges for each graph in the batch with the specified edge type. |
| 1615 | |
| 1616 | Parameters |
| 1617 | ---------- |
| 1618 | etype : str or tuple of str, optional |
| 1619 | The edge type for query, which can be an edge type (str) or a canonical edge type |
| 1620 | (3-tuple of str). When an edge type appears in multiple canonical edge types, one |
| 1621 | must use a canonical edge type. If the graph has multiple edge types, one must |
| 1622 | specify the argument. Otherwise, it can be omitted. |
| 1623 | |
| 1624 | Returns |
| 1625 | ------- |
| 1626 | Tensor |
| 1627 | The number of edges with the specified type for each graph in the batch. The i-th |
| 1628 | element of it is the number of edges with the specified type for the i-th graph. |
| 1629 | If the graph is not a batched one, it will return a list of length 1 that holds |
| 1630 | the number of edges in the graph. |
| 1631 | |
| 1632 | Examples |
| 1633 | -------- |
| 1634 | |
| 1635 | The following example uses PyTorch backend. |
| 1636 | |
| 1637 | >>> import dgl |
| 1638 | >>> import torch |
| 1639 | |
| 1640 | Query for homogeneous graphs. |
| 1641 | |
| 1642 | >>> g1 = dgl.graph((torch.tensor([0, 1, 2]), torch.tensor([1, 2, 3]))) |
| 1643 | >>> g1.batch_num_edges() |
| 1644 | tensor([3]) |
| 1645 | >>> g2 = dgl.graph((torch.tensor([0, 0, 0, 1]), torch.tensor([0, 1, 2, 0]))) |
| 1646 | >>> bg = dgl.batch([g1, g2]) |
| 1647 | >>> bg.batch_num_edges() |
| 1648 | tensor([3, 4]) |
| 1649 | |
| 1650 | Query for heterogeneous graphs. |
| 1651 | |
| 1652 | >>> hg1 = dgl.heterograph({ |
| 1653 | ... ('user', 'plays', 'game') : (torch.tensor([0, 1]), torch.tensor([0, 0]))}) |
| 1654 | >>> hg2 = dgl.heterograph({ |
| 1655 | ... ('user', 'plays', 'game') : (torch.tensor([0, 0]), torch.tensor([1, 0]))}) |
| 1656 | >>> bg = dgl.batch([hg1, hg2]) |
| 1657 | >>> bg.batch_num_edges('plays') |
| 1658 | tensor([2, 2]) |
| 1659 | """ |
| 1660 | if self._batch_num_edges is None: |
| 1661 | self._batch_num_edges = {} |
| 1662 | for ty in self.canonical_etypes: |
| 1663 | bne = F.copy_to( |
| 1664 | F.tensor([self.num_edges(ty)], self.idtype), self.device |
| 1665 | ) |
| 1666 | self._batch_num_edges[ty] = bne |
| 1667 | if etype is None: |
| 1668 | if len(self.etypes) != 1: |
| 1669 | raise DGLError( |
| 1670 | "Edge type name must be specified if there are more than one " |