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) withtorchor using a tensor(Required-Type:tensorofint,floatorbool). - The 2nd argument with
torchor the 1st argument ismin(Optional-Type:scalarofintorfloatortensorofint,floatorbool). - The 3rd argument with
torchor the 1st argument ismax(Optional-Type:scalarofintorfloatortensorofint,floatorbool). - There is
outargument withtorch(Optional-Default:None-Type:tensor): *Memos:-
out=must be used. -
My post explains
outargument.
-
- The combination of
minandmaxcannot be a scalar and tensor and vice versa and bothNone. - The combination of
minandmaxcannot be both tensors(bool) but a tensor(bool) andNoneand vice versa is possible. - If a
minis greater than amaxvalue, themaxvalue 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)