sum() can get one or more sum values from a 0D or more D tensor as shown below:
*Memos:
-
sum()
can be used with torch or a tensor. - The tensor of zero or more integers, floating-point numbers, complex numbers or boolean values can be used.
- The 2nd argument(
int
,tuple
ofint
orlist
ofint
) withtorch
or the 1st argument(int
,tuple
ofint
orlist
ofint
) with a tensor isdim
(Optional). - The 3rd argument(
bool
) withtorch
or the 2nd argument(bool
) with a tensor iskeepdim
(Optional-Default:False
) which keeps the dimension of the input tensor. *keepdim=
must be used withdim=
.
import torch
my_tensor = torch.tensor([0, 1, 2, 3])
torch.sum(my_tensor)
my_tensor.sum()
torch.sum(my_tensor, dim=0)
my_tensor.sum(dim=0)
torch.sum(my_tensor, dim=-1)
my_tensor.sum(dim=-1)
torch.sum(my_tensor, dim=(0,))
my_tensor.sum(dim=(0,))
torch.sum(my_tensor, dim=(-1,))
my_tensor.sum(dim=(-1,))
# tensor(6)
torch.sum(my_tensor, dim=0, keepdim=True)
# tensor([6])
my_tensor = torch.tensor([False, True, 2., 3+0j])
torch.sum(my_tensor)
# tensor(6.+0.j)
torch.sum(my_tensor, dim=0, keepdim=True)
# tensor([6.+0.j])
my_tensor = torch.tensor([[0, 1, 2, 3], [4, 5, 6, 7]])
torch.sum(my_tensor)
torch.sum(my_tensor, dim=(0, 1))
torch.sum(my_tensor, dim=(0, -1))
torch.sum(my_tensor, dim=(1, 0))
torch.sum(my_tensor, dim=(1, -2))
torch.sum(my_tensor, dim=(-1, 0))
torch.sum(my_tensor, dim=(-1, -2))
torch.sum(my_tensor, dim=(-2, 1))
torch.sum(my_tensor, dim=(-2, -1))
# tensor(28)
torch.sum(my_tensor, dim=(0, 1), keepdim=True)
# tensor([[28]])
torch.sum(my_tensor, dim=0)
torch.sum(my_tensor, dim=-2)
torch.sum(my_tensor, dim=(0,))
torch.sum(my_tensor, dim=(-2,))
# tensor([4, 6, 8, 10])
torch.sum(my_tensor, dim=0, keepdim=True)
# tensor([[4, 6, 8, 10]])
torch.sum(my_tensor, dim=1)
torch.sum(my_tensor, dim=-1)
torch.sum(my_tensor, dim=(1,))
torch.sum(my_tensor, dim=(-1,))
# tensor([6, 22])
torch.sum(my_tensor, dim=1, keepdim=True)
# tensor([[6], [22]])
prod() can get one or more product values from a 0D or more D tensor as shown below:
*Memos:
-
prod()
can be used withtorch
or a tensor. - The tensor of zero or more integers, floating-point numbers, complex numbers or boolean values can be used.
- The 2nd argument(
int
) withtorch
or the 1st argument(int
) with a tensor isdim
(Optional). - The 3rd argument(
bool
) withtorch
or the 2nd argument(bool
) with a tensor iskeepdim
(Optional-Default:False
) which keeps the dimension of the input tensor. *keepdim=
must be used withdim=
.
import torch
my_tensor = torch.tensor([0, 1, 2, 3])
torch.prod(my_tensor)
my_tensor.prod()
torch.prod(my_tensor, dim=0)
my_tensor.prod(dim=0)
torch.prod(my_tensor, dim=-1)
my_tensor.prod(dim=-1)
# tensor(0)
torch.prod(my_tensor, dim=0, keepdim=True)
# tensor([0])
my_tensor = torch.tensor([False, True, 2., 3+0j])
torch.prod(my_tensor)
# tensor(0.+0.j)
torch.prod(my_tensor, dim=0, keepdim=True)
# tensor([[0, 5, 12, 21]])
my_tensor = torch.tensor([[0, 1, 2, 3], [4, 5, 6, 7]])
torch.prod(my_tensor)
# tensor(0)
torch.prod(my_tensor, dim=0)
torch.prod(my_tensor, dim=-2)
# tensor([0, 5, 12, 21])
torch.prod(my_tensor, dim=1)
torch.prod(my_tensor, dim=-1)
# tensor([0, 840])
torch.prod(my_tensor, dim=0, keepdim=True)
# tensor([[0, 5, 12, 21]])
cartesian_prod() can do cartesian product with one or more 1D tensors as shown below:
*Memos:
-
cartesian_prod()
can be used withtorch
but not with a tensor. - The tensor of zero or more integers, floating-point numbers, complex numbers or boolean values can be used.
- The 2nd or more arguments with
torch
are*tensors
. *Memos:- Don't use
*tensors=
ortensors=
withtorch
. - Tensors must be the same type. *A tensor can have multiple types.
- Don't use
import torch
my_tensor = torch.tensor([0, 1, 2, 3])
torch.cartesian_prod(my_tensor)
# tensor([0, 1, 2, 3])
my_tensor = torch.tensor([False, True, 2., 3+0j])
torch.cartesian_prod(my_tensor)
# tensor([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j])
my_tensor1 = torch.tensor([0, 1, 2, 3])
my_tensor2 = torch.tensor([4, 5])
torch.cartesian_prod(my_tensor1, my_tensor2)
# tensor([[0, 4],
# [0, 5],
# [1, 4],
# [1, 5],
# [2, 4],
# [2, 5],
# [3, 4],
# [3, 5]])
my_tensor1 = torch.tensor([0, 1, 2, 3])
my_tensor2 = torch.tensor([4, 5])
my_tensor3 = torch.tensor([6, 7, 8])
torch.cartesian_prod(my_tensor1, my_tensor2, my_tensor3)
# tensor([[0, 4, 6],
# [0, 4, 7],
# [0, 4, 8],
# [0, 5, 6],
# [0, 5, 7],
# [0, 5, 8],
# [1, 4, 6],
# [1, 4, 7],
# [1, 4, 8],
# [1, 5, 6],
# [1, 5, 7],
# [1, 5, 8],
# [2, 4, 6],
# [2, 4, 7],
# [2, 4, 8],
# [2, 5, 6],
# [2, 5, 7],
# [2, 5, 8],
# [3, 4, 6],
# [3, 4, 7],
# [3, 4, 8],
# [3, 5, 6],
# [3, 5, 7],
# [3, 5, 8]])
Top comments (0)