clamp() in PyTorch

Buy Me a Coffee☕

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 argume…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee

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) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • The 2nd argument with torch or the 1st argument is min(Optional-Type:scalar of int or float or tensor of int, float or bool).
  • The 3rd argument with torch or the 1st argument is max(Optional-Type:scalar of int or float or tensor of int, float or bool).
  • The combination of min and max cannot be a scalar and tensor and vice versa and both None.
  • The combination of min and max cannot be both tensors(bool) but a tensor(bool) and None and vice versa is possible.
  • If a min is greater than a max value, the max 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)


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » clamp() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Tuesday September 3, 2024, https://www.scien.cx/2024/09/03/clamp-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Tuesday September 3, 2024 » clamp() in PyTorch., viewed ,<https://www.scien.cx/2024/09/03/clamp-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » clamp() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/03/clamp-in-pytorch/
CHICAGO
" » clamp() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/09/03/clamp-in-pytorch/
IEEE
" » clamp() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/09/03/clamp-in-pytorch/. [Accessed: ]
rf:citation
» clamp() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.