`torch.sum` with equivalent implementation for numpy Args: x: array/tensor. Returns: the sum of x.
(x: NdarrayTensor, dim: int | tuple | None = None, **kwargs)
| 570 | |
| 571 | |
| 572 | def sum(x: NdarrayTensor, dim: int | tuple | None = None, **kwargs) -> NdarrayTensor: |
| 573 | """`torch.sum` with equivalent implementation for numpy |
| 574 | |
| 575 | Args: |
| 576 | x: array/tensor. |
| 577 | |
| 578 | Returns: |
| 579 | the sum of x. |
| 580 | """ |
| 581 | |
| 582 | ret: NdarrayTensor |
| 583 | if dim is None: |
| 584 | ret = np.sum(x, **kwargs) if isinstance(x, (np.ndarray, list)) else torch.sum(x, **kwargs) # type: ignore |
| 585 | else: |
| 586 | if isinstance(x, (np.ndarray, list)): |
| 587 | ret = np.sum(x, axis=dim, **kwargs) |
| 588 | else: |
| 589 | ret = torch.sum(x, int(dim), **kwargs) # type: ignore |
| 590 | |
| 591 | return ret |
no outgoing calls