torch.clamp(input, min, max, out=None)函数的使用举例

参考链接: torch.clamp(input, min, max, out=None)

torch.clamp(input, min, max, out=None)函数的使用举例_第1张图片

功能:
	将给定的张量的所有元素的取值限定在一个指定的范围之内

代码实验举例:

(base) PS C:\Users\chenxuqi> python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000001B4B4783A90>
>>>
>>> a = torch.randn(4)
>>> a
tensor([ 0.2824, -0.3715,  0.9088, -1.7601])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([ 0.2824, -0.3715,  0.5000, -0.5000])
>>> a.clamp(min=-0.5, max=0.5)
tensor([ 0.2824, -0.3715,  0.5000, -0.5000])
>>> a
tensor([ 0.2824, -0.3715,  0.9088, -1.7601])
>>>
>>>
>>>
>>> a.clamp_(min=-0.5, max=0.5)
tensor([ 0.2824, -0.3715,  0.5000, -0.5000])
>>> a
tensor([ 0.2824, -0.3715,  0.5000, -0.5000])
>>>
>>>
>>>
>>>
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000001B4B4783A90>
>>> a = torch.randn(4)
>>> a
tensor([ 0.2824, -0.3715,  0.9088, -1.7601])
>>> torch.clamp(a, min=0.5)
tensor([0.5000, 0.5000, 0.9088, 0.5000])
>>>
>>> a
tensor([ 0.2824, -0.3715,  0.9088, -1.7601])
>>> torch.clamp(a, max=0.5)
tensor([ 0.2824, -0.3715,  0.5000, -1.7601])
>>> a
tensor([ 0.2824, -0.3715,  0.9088, -1.7601])
>>>
>>>
>>>  

你可能感兴趣的:(torch.clamp(input, min, max, out=None)函数的使用举例)