clamp() can get the 0D or more D tensor of zero or more elements from the 0D or more D tensor of zero or more elements, bounded between min
and max
as shown below:
*Memos:
-
clamp()
can be used with torch or a tensor. - The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
orbool
). - The 2nd argument with
torch
or the 1st argument ismin
(Optional-Type:scalar
ofint
orfloat
ortensor
ofint
,float
orbool
). - The 3rd argument with
torch
or the 1st argument ismax
(Optional-Type:scalar
ofint
orfloat
ortensor
ofint
,float
orbool
). - There is
out
argument withtorch
(Optional-Default:None
-Type:tensor
): *Memos:-
out=
must be used. -
My post explains
out
argument.
-
- The combination of
min
andmax
cannot be a scalar and tensor and vice versa and bothNone
. - The combination of
min
andmax
cannot be both tensors(bool
) but a tensor(bool
) andNone
and vice versa is possible. - If a
min
is greater than amax
value, themax
value is set regardless of the value of an input tensor.
import torch
my_tensor = torch.tensor([0., 1., 2., 3., 4., 5., 6., 7.])
torch.clamp(input=my_tensor, min=2., max=5.)
my_tensor.clamp(min=2., max=5.)
torch.clamp(input=my_tensor,
min=torch.tensor(2.),
max=torch.tensor(5.))
torch.clamp(input=my_tensor,
min=torch.tensor([2., 2., 2., 2., 2., 2., 2., 2.]),
max=torch.tensor([5., 5., 5., 5., 5., 5., 5., 5.]))
torch.clamp(input=my_tensor,
min=torch.tensor(2.),
max=torch.tensor([5., 5., 5., 5., 5., 5., 5., 5.]))
torch.clamp(input=my_tensor,
min=torch.tensor([2., 2., 2., 2., 2., 2., 2., 2.]),
max=torch.tensor(5.))
# tensor([2., 2., 2., 3., 4., 5., 5., 5.])
torch.clamp(input=my_tensor, min=2.)
torch.clamp(input=my_tensor, min=torch.tensor(2.))
torch.clamp(input=my_tensor,
min=torch.tensor([2., 2., 2., 2., 2., 2., 2., 2.]))
# tensor([2., 2., 2., 3., 4., 5., 6., 7.])
torch.clamp(input=my_tensor, max=5.)
torch.clamp(input=my_tensor, max=torch.tensor(5.))
torch.clamp(input=my_tensor,
max=torch.tensor([5., 5., 5., 5., 5., 5., 5., 5.]))
# tensor([0., 1., 2., 3., 4., 5., 5., 5.])
torch.clamp(input=my_tensor, min=5., max=2.)
torch.clamp(input=my_tensor, min=torch.tensor(5.), max=torch.tensor(2.))
torch.clamp(input=my_tensor,
min=torch.tensor([5., 5., 5., 5., 5., 5., 5., 5.]),
max=torch.tensor([2., 2., 2., 2., 2., 2., 2., 2.]))
# tensor([2., 2., 2., 2., 2., 2., 2., 2.])
torch.clamp(input=my_tensor,
min=torch.tensor([2., 0., 2., 0., 2., 0., 2., 0.]),
max=torch.tensor([0., 5., 0., 5., 0., 5., 0., 5.]))
# tensor([0., 1., 0., 3., 0., 5., 0., 5.])
torch.clamp(input=my_tensor,
min=torch.tensor([2., 0., 2., 0., 2., 0., 2., 0.]))
# tensor([2., 1., 2., 3., 4., 5., 6., 7.])
torch.clamp(input=my_tensor,
max=torch.tensor([0., 5., 0., 5., 0., 5., 0., 5.]))
# tensor([0., 1., 0., 3., 0., 5., 0., 5.])
my_tensor = torch.tensor([[0., 1., 2., 3.],
[4., 5., 6., 7.]])
torch.clamp(input=my_tensor, min=2., max=5.)
torch.clamp(input=my_tensor,
min=torch.tensor(2.),
max=torch.tensor(5.))
torch.clamp(input=my_tensor,
min=torch.tensor([2., 2., 2., 2.]),
max=torch.tensor([5., 5., 5., 5.]))
torch.clamp(input=my_tensor,
min=torch.tensor(2.),
max=torch.tensor([5., 5., 5., 5.]))
torch.clamp(input=my_tensor,
min=torch.tensor([2., 2., 2., 2.]),
max=torch.tensor(5.))
# tensor([[2., 2., 2., 3.],
# [4., 5., 5., 5.]])
my_tensor = torch.tensor([[0, 1, 2, 3],
[4, 5, 6, 7]])
torch.clamp(input=my_tensor, min=2, max=5)
torch.clamp(input=my_tensor,
min=torch.tensor([2, 2, 2, 2]),
max=torch.tensor([5, 5, 5, 5]))
# tensor([[2., 2., 2., 3.],
# [4., 5., 5., 5.]])
my_tensor = torch.tensor([[True, False, True, False],
[False, True, False, True]])
torch.clamp(input=my_tensor,
min=torch.tensor([False, True, False, True]))
# tensor([[True, True, True, True],
# [False, True, False, True]])
torch.clamp(input=my_tensor,
max=torch.tensor([False, True, False, True]))
# tensor([[False, False, False, False],
# [False, True, False, True]])
Top comments (0)