sum() can get the one or more sum elements of a 0D or more tensor from a 0D or more D tensor as shown below:
*Memos:
-
sum()
can be used with torch or a tensor. - The 1st argument(
tensor
ofint
,float
,complex
orbool
) withtorch
or using a tensor(tensor
ofint
,float
,complex
orbool
) isinput
(Required). - 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=
. - There is
dtype
argument(torch.dtype) (Optional) withtorch
. *Memos:- If
None
, the type ofinput
is used. -
dtype
can also accept int(), float() and bool() but not complex() which are python built-in functions. -
dtype=
must be used.
- If
import torch
my_tensor = torch.tensor([1, 2, 3])
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(6)
torch.sum(input=my_tensor, dim=0, keepdim=True)
# tensor([6])
my_tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
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(21)
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([5, 7, 9])
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([6, 15])
torch.sum(input=my_tensor, dim=(0, 1), keepdim=True)
# tensor([[21]])
my_tensor = torch.tensor([[1., 2., 3.], [4., 5., 6.]])
torch.sum(input=my_tensor, dtype=torch.float64)
torch.sum(input=my_tensor, dtype=float)
# tensor(21., dtype=torch.float64)
my_tensor = torch.tensor([[1.+9.j, 2.-4.j, 3.+6.j],
[4.+0.j, 5.+8.j, 6.-5.j]])
torch.sum(input=my_tensor, dtype=torch.complex64)
# tensor(21.+14.j)
my_tensor = torch.tensor([[True, False, True, False],
[False, True, False, True]])
torch.sum(input=my_tensor)
# tensor(4)
torch.sum(input=my_tensor, dtype=torch.bool)
torch.sum(input=my_tensor, dtype=bool)
# tensor(True)
prod() can get the one or more product values of a 0D or more D tensor from a 0D or more D tensor as shown below:
*Memos:
-
prod()
can be used withtorch
or a tensor. - The 1st argument(
tensor
ofint
,float
,complex
orbool
) withtorch
or using a tensor(tensor
ofint
,float
,complex
orbool
) isinput
(Required). - 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=
. - There is
dtype
argument(torch.dtype) (Optional) withtorch
. *Memos:- If
None
, the type ofinput
is used. -
dtype
can also accept int(), float() and bool() but not complex() which are python built-in functions. -
dtype=
must be used.
- If
import torch
my_tensor = torch.tensor([1, 2, 3])
torch.prod(input=my_tensor)
my_tensor.prod()
torch.prod(input=my_tensor, dim=0)
torch.prod(input=my_tensor, dim=-1)
# tensor(6)
torch.prod(input=my_tensor, dim=0, keepdim=True)
# tensor([6])
my_tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
torch.prod(input=my_tensor)
# tensor(720)
torch.prod(input=my_tensor, dim=0)
torch.prod(input=my_tensor, dim=-2)
# tensor([4, 10, 18])
torch.prod(input=my_tensor, dim=1)
torch.prod(input=my_tensor, dim=-1)
# tensor([6, 120])
torch.prod(input=my_tensor, dim=0, keepdim=True)
# tensor([[4, 10, 18]])
my_tensor = torch.tensor([[1., 2., 3.],
[4., 5., 6.]])
torch.prod(input=my_tensor, dtype=torch.float64)
torch.prod(input=my_tensor, dtype=float)
# tensor(720., dtype=torch.float64)
my_tensor = torch.tensor([[1.+9.j, 2.-4.j, 3.+6.j],
[4.+0.j, 5.+8.j, 6.-5.j]])
torch.prod(input=my_tensor, dtype=torch.complex64)
# tensor(-16440.+78360.j)
my_tensor = torch.tensor([[True, False, True, False],
[False, True, False, True]])
torch.prod(input=my_tensor)
# tensor(0)
torch.prod(input=my_tensor, dtype=torch.bool)
# tensor(False)
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 1st or more arguments(
tensor
ofint
,float
,complex
orbool
) withtorch
are*tensors
(Required at least one tensor). *Memos:- Don't use
*tensors=
ortensors=
withtorch
. - Tensors must be the same type.
- Don't use
import torch
my_tensor = torch.tensor([1, 2, 3])
torch.cartesian_prod(my_tensor)
# tensor([1, 2, 3])
my_tensor = torch.tensor([1., 2., 3.])
torch.cartesian_prod(my_tensor)
# tensor([1., 2., 3.])
my_tensor = torch.tensor([1.+9.j, 2.-4.j, 3.+6.j])
torch.cartesian_prod(my_tensor)
# tensor([1.+9.j, 2.-4.j, 3.+6.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])
tensor2 = torch.tensor([4, 5])
torch.cartesian_prod(tensor1, tensor2)
# tensor([[1, 4],
# [1, 5],
# [2, 4],
# [2, 5],
# [3, 4],
# [3, 5]])
tensor1 = torch.tensor([1, 2, 3])
tensor2 = torch.tensor([4, 5])
tensor3 = torch.tensor([6, 7, 8, 9])
torch.cartesian_prod(tensor1, tensor2, tensor3)
# tensor([[1, 4, 6],
# [1, 4, 7],
# [1, 4, 8],
# [1, 4, 9],
# [1, 5, 6],
# [1, 5, 7],
# [1, 5, 8],
# [1, 5, 9],
# [2, 4, 6],
# [2, 4, 7],
# [2, 4, 8],
# [2, 4, 9],
# [2, 5, 6],
# [2, 5, 7],
# [2, 5, 8],
# [2, 5, 9],
# [3, 4, 6],
# [3, 4, 7],
# [3, 4, 8],
# [3, 4, 9],
# [3, 5, 6],
# [3, 5, 7],
# [3, 5, 8],
# [3, 5, 9]])
Top comments (0)