sum(), prod() and cartesian_prod() in PyTorch
Tags: pytorch, sum, prod, cartesianprod
Published: 2024-04-23 17:39:24

[sum()](https://pytorch.org/docs/stable/generated/torch.sum.html) can get the 0D or more D tensor of one or more sum's elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `sum()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- The 2nd argument with `torch` or the 1st argument with a tensor is `dim`(Optional-Type:`int`, `tuple` of `int` or `list` of `int`).
- The 3rd argument with `torch` or the 2nd argument with a tensor is `keepdim`(Optional-Default:`False`-Type:`bool`):
  *Memos:
  - `keepdim=` must be used with `dim=`.
  - [My post](https://dev.to/hyperkai/set-keepdim-with-keepdim-argument-functions-pytorch-2fdj) explains `keepdim` argument.
- There is `dtype` argument with `torch`(Optional-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)):
  *Memos:
  - If `dtype` is not given, `dtype` is inferred from `input` or `dtype` of [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html) is used for floating-point numbers.
  - `dtype=` must be used.
  - [My post](https://dev.to/hyperkai/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch-13h2) explains `dtype` argument.
- An empty tensor gets `0.`.

```python
import torch

my_tensor = torch.tensor([1, 2, 3, 4])

torch.sum(input=my_tensor)
my_tensor.sum()
torch.sum(input=my_tensor, dim=0)
torch.sum(input=my_tensor, dim=-1)
torch.sum(input=my_tensor, dim=(0,))
torch.sum(input=my_tensor, dim=(-1,))
# tensor(10)

my_tensor = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]])

torch.sum(input=my_tensor)
torch.sum(input=my_tensor, dim=(0, 1))
torch.sum(input=my_tensor, dim=(0, -1))
torch.sum(input=my_tensor, dim=(1, 0))
torch.sum(input=my_tensor, dim=(1, -2))
torch.sum(input=my_tensor, dim=(-1, 0))
torch.sum(input=my_tensor, dim=(-1, -2))
torch.sum(input=my_tensor, dim=(-2, 1))
torch.sum(input=my_tensor, dim=(-2, -1))
# tensor(36)

torch.sum(input=my_tensor, dim=0)
torch.sum(input=my_tensor, dim=-2)
torch.sum(input=my_tensor, dim=(0,))
torch.sum(input=my_tensor, dim=(-2,))
# tensor([6, 8, 10, 12])

torch.sum(input=my_tensor, dim=1)
torch.sum(input=my_tensor, dim=-1)
torch.sum(input=my_tensor, dim=(1,))
torch.sum(input=my_tensor, dim=(-1,))
# tensor([10, 26])

my_tensor = torch.tensor([[1., 2., 3., 4.], [5., 6., 7., 8.]])

torch.sum(input=my_tensor)
# tensor(36.)

my_tensor = torch.tensor([[1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j],
                          [5.+0.j, 6.+0.j, 7.+0.j, 8.+0.j]])
torch.sum(input=my_tensor)
# tensor(36.+0.j)

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True]])
torch.sum(input=my_tensor)
# tensor(4)

my_tensor = torch.tensor([])

torch.sum(input=my_tensor)
# tensor(0.)
```

[prod()](https://pytorch.org/docs/stable/generated/torch.prod.html) can get the 0D or more D tensor of one or more product's elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `prod()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- The 2nd argument with `torch` or the 1st argument with a tensor is `dim`(Optional-Type:`int`).
- The 3rd argument with `torch` or the 2nd argument with a tensor is `keepdim`(Optional-Default:`False`-Type:`bool`):
  *Memos:
  - `keepdim=` must be used with `dim=`.
  - [My post](https://dev.to/hyperkai/set-keepdim-with-keepdim-argument-functions-pytorch-2fdj) explains `keepdim` argument.
- There is `dtype` argument with `torch`(Optional-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)):
  *Memos:
  - If `dtype` is not given, `dtype` is inferred from `input` or `dtype` of [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html) is used for floating-point numbers.
  - `dtype=` must be used.
  - [My post](https://dev.to/hyperkai/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch-13h2) explains `dtype` argument.
- An empty tensor gets `1.`.

```python
import torch

my_tensor = torch.tensor([1, 2, 3, 4])

torch.prod(input=my_tensor)
my_tensor.prod()
torch.prod(input=my_tensor, dim=0)
torch.prod(input=my_tensor, dim=-1)
# tensor(24)

my_tensor = torch.tensor([[1, 2, 3, 4],
                          [5, 6, 7, 8]])
torch.prod(input=my_tensor)
# tensor(40320)

torch.prod(input=my_tensor, dim=0)
torch.prod(input=my_tensor, dim=-2)
# tensor([5, 12, 21, 32])

torch.prod(input=my_tensor, dim=1)
torch.prod(input=my_tensor, dim=-1)
# tensor([24, 1680])

my_tensor = torch.tensor([[1., 2., 3., 4.],
                          [5., 6., 7., 8.]])
torch.prod(input=my_tensor)
# tensor(40320.)

my_tensor = torch.tensor([[1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j],
                          [5.+0.j, 6.+0.j, 7.+0.j, 8.+0.j]])
torch.prod(input=my_tensor)
# tensor(40320.+0.j)

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True]])
torch.prod(input=my_tensor)
# tensor(0)

my_tensor = torch.tensor([])

torch.prod(input=my_tensor)
# tensor(1.)
```

[cartesian_prod()](https://pytorch.org/docs/stable/generated/torch.cartesian_prod.html) can get the 1D or 2D tensor of zero or more cartesian product's elements from the one or more 1D tensors as shown below:

*Memos:
- `cartesian_prod()` can be used with `torch` but not with a tensor.
- The 1st or more arguments with `torch` are `*tensors`(Required at least one tensor-Type:`tensor` of `int`, `float`, `complex` or `bool`).
  *Memos:
  - Don't use any keyword like `*tensors=` or `tensors=`.
  - Tensors must be the same type.

```python
import torch

my_tensor = torch.tensor([1, 2, 3, 4])

torch.cartesian_prod(my_tensor)
# tensor([1, 2, 3, 4])

my_tensor = torch.tensor([1., 2., 3., 4.])

torch.cartesian_prod(my_tensor)
# tensor([1., 2., 3., 4.])

my_tensor = torch.tensor([1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j])

torch.cartesian_prod(my_tensor)
# tensor([1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j])

my_tensor = torch.tensor([True, False, True, False])

torch.cartesian_prod(my_tensor)
# tensor([True, False, True, False])

tensor1 = torch.tensor([1, 2, 3, 4])
tensor2 = torch.tensor([5, 6])

torch.cartesian_prod(tensor1, tensor2)
# tensor([[1, 5],
#         [1, 6],
#         [2, 5],
#         [2, 6],
#         [3, 5],
#         [3, 6],
#         [4, 5],
#         [4, 6]])

tensor1 = torch.tensor([1, 2, 3, 4])
tensor2 = torch.tensor([5, 6])
tensor3 = torch.tensor([7, 8, 9])

torch.cartesian_prod(tensor1, tensor2, tensor3)
# tensor([[1, 5, 7],
#         [1, 5, 8],
#         [1, 5, 9],
#         [1, 6, 7],
#         [1, 6, 8],
#         [1, 6, 9],
#         [2, 5, 7],
#         [2, 5, 8],
#         [2, 5, 9],
#         [2, 6, 7],
#         [2, 6, 8],
#         [2, 6, 9],
#         [3, 5, 7],
#         [3, 5, 8],
#         [3, 5, 9],
#         [3, 6, 7],
#         [3, 6, 8],
#         [3, 6, 9],
#         [4, 5, 7],
#         [4, 5, 8],
#         [4, 5, 9],
#         [4, 6, 7],
#         [4, 6, 8],
#         [4, 6, 9]])
```