Python – PyTorch clamp() method Improve Improve Like Article Like Save Share Report PyTorch torch.clamp() method clamps all the input elements into the range [ min, max ] and return a resulting tensor. Syntax: torch.clamp(inp, min, max, out=None) Arguments inp: This is input tensor. min: This is a number and specifies the lower-bound of the range to which input to be clamped. max: This is a number and specifies the upper-bound of the range to which input to be clamped. out: The output tensor. Return: It returns a Tensor. Let’s see this concept with the help of few examples: Example 1: # Importing the PyTorch library import torch # A constant tensor of size n a = torch.randn(6) print(a) # Applying the clamp function and # storing the result in 'out' out = torch.clamp(a, min = 0.5, max = 0.9) print(out) Output: -0.9214 -0.1268 1.1570 -0.2753 -0.0746 0.7957 [torch.FloatTensor of size 6] 0.5000 0.5000 0.9000 0.5000 0.5000 0.7957 [torch.FloatTensor of size 6] Example 2: # Importing the PyTorch library import torch # A constant tensor of size n a = torch.FloatTensor([1, 4, 6, 8, 10, 14]) print(a) # Applying the clamp function and # storing the result in 'out' out = torch.clamp(a, min = 5, max = 10) print(out) Output: 1 4 6 8 10 14 [torch.FloatTensor of size 6] 5 5 6 8 10 10 [torch.FloatTensor of size 6]? Last Updated : 26 May, 2020 Like Article Save Article Previous How to perform random affine transformation of an image in PyTorch Next Python - PyTorch div() method Share your thoughts in the comments Add Your Comment Please Login to comment... Similar Reads Wand clamp() function - Python How to clamp floating numbers in Python PyTorch vs PyTorch Lightning Python | PyTorch sin() method Python | PyTorch sinh() method Python | PyTorch cosh() method Python | PyTorch tanh() method Python | PyTorch cos() method Python | PyTorch tan() method Python | PyTorch asin() method Like P PranchalKatiyar Follow Article Tags : Python-PyTorch Python Practice Tags : python