torch.clamp_()
时间: 2023-10-16 16:31:46 浏览: 40
torch.clamp_() is an in-place operation that restricts the values of a tensor to fall within a specified range. It takes the following arguments:
- input (Tensor): The input tensor to be clamped.
- min (Number or Tensor): The minimum value to clamp the tensor to. If it is a tensor, it must have the same shape as the input tensor.
- max (Number or Tensor): The maximum value to clamp the tensor to. If it is a tensor, it must have the same shape as the input tensor.
The function modifies the input tensor in-place and returns it. It sets all elements of the tensor to the minimum value if they are less than the minimum value, and all elements to the maximum value if they are greater than the maximum value. Otherwise, it leaves the elements of the tensor unchanged.
Example usage:
```
import torch
x = torch.randn(2, 3)
print(x)
torch.clamp_(x, min=-1, max=1)
print(x)
```
Output:
```
tensor([[ 0.0945, -0.2009, 0.8743],
[-1.7871, 0.9902, 0.6039]])
tensor([[ 0.0945, -0.2009, 0.8743],
[-1.0000, 0.9902, 0.6039]])
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)