[Python-torch] torch.clamp() function analysis

[Python-torch] torch.clamp() function analysis

1. Analysis

torch.clamp(input, min, max, out=None) → Tensor

1) Parameter list

  • input: input tensor;
  • min: the lower limit of the limit range;
  • max: the upper limit of the limit range;
  • out: output tensor.

2) Function

  • The function of the clamp() function compresses the value of each element of the input tensor to the interval [min,max], and returns the result to a new tensor.

3) Example

a=torch.randint(low=0,high=10,size=(10,1))
print(a)
b=torch.clamp(a,3,9)
print(b)

output:

tensor([[7],
        [5],
        [5],
        [4],
        [4],
        [9],
        [0],
        [1],
        [4],
        [1]])
tensor([[7],
        [5],
        [5],
        [4],
        [4],
        [9],
        [3],
        [3],
        [4],
        [3]])

2. Compare

The difference between clamp_() and clamp():

  • In pytorch, generally speaking, if an underscore is added to a tensor function, it indicates that it is an in-place type.
    • The in-place type means that after operating on a tensor, the tensor is directly modified instead of returning a new tensor and not modifying the old tensor.

Guess you like

Origin blog.csdn.net/qq_51392112/article/details/130567142