This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
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
). - 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]])
This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito) | Sciencx (2024-09-03T19:48:37+00:00) clamp() in PyTorch. Retrieved from https://www.scien.cx/2024/09/03/clamp-in-pytorch/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.