Python - PyTorch clamp() method Last Updated : 26 May, 2020 Comments Improve Suggest changes Like Article Like 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: Python3 # 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: Python3 # 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]? Comment More infoAdvertise with us Next Article Python - PyTorch clamp() method P PranchalKatiyar Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads Python - PyTorch ceil() method PyTorch torch.ceil() method returns a new tensor having the ceil value of the elements of input, Which is the smallest integer larger than or equal to each element. Syntax: torch.ceil(inp, out=None) Arguments inp: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see t 1 min read Python - PyTorch abs() method PyTorch torch.abs() method computes the element-wise absolute value of the given input tensor. Syntax: torch.abs(inp, out=None) ? Tensor Arguments inp: This is input tensor. out: This is optional parameter and is the output tensor. Return: It returns a Tensor having absolute value of input inp. Let' 1 min read Python - PyTorch add() method PyTorch torch.add() method adds a constant value to each element of the input tensor and returns a new modified tensor. Syntax: torch.add(inp, c, out=None) Arguments inp: This is input tensor. c: The value that is to be added to every element of tensor. out: This is optional parameter and it is the 1 min read Python - PyTorch log() method PyTorch torch.log() method gives a new tensor having the natural logarithm of the elements of input tensor. Syntax: torch.log(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept with the help of few examples: Example 1: 1 min read Python - PyTorch frac() method PyTorch torch.frac() method computes the fractional portion of each element in input. Syntax: torch.frac(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept with the help of few examples: Example 1: Python3 # Importing t 1 min read Like