1. torch.Tensor
生成一个 tensor 类型。
>>> import torch
>>> x=torch.Tensor([[1,2,3],[4,5,6]])
>>> x
tensor([[1., 2., 3.],
[4., 5., 6.]])
参考: https://www.jianshu.com/p/314b6cfce1c3
https://pytorch-cn.readthedocs.io/zh/latest/package_references/Tensor/
https://blog.csdn.net/weixin_42018112/article/details/91383574
2. torch.nn.functional.softmax(input, dim=None, _stacklevel=3, dtype=None)
用于对设置的维度上的数进行收缩,让他的和为 1.
>>> import torch
>>> from torch import nn as nn
>>> input = torch.randn(2, 3, 4)
>>> input
tensor([[[-0.1335, 0.1574, -0.4618, -0.1629],
[-1.1302, -0.2782, 0.2689, 1.4722],
[ 1.8547, 3.0593, 1.7146, -0.4395]],
[[-0.0102, 1.4679, 0.0138, 0.5245],
[ 2.2345, 2.0089, 2.0074, 0.4197],
[-1.4187, -0.0887, 0.9257, 0.2516]]])
>>> torch.nn.functional.softmax(input)
tensor([[[0.4692, 0.2124, 0.3833, 0.3346],
[0.0334, 0.0922, 0.1495, 0.7413],
[0.9635, 0.9588, 0.6876, 0.3338]],
[[0.5308, 0.7876, 0.6167, 0.6654],
[0.9666, 0.9078, 0.8505, 0.2587],
[0.0365, 0.0412, 0.3124, 0.6662]]])
>>>
>>> torch.nn.functional.softmax(input, dim=0)
tensor([[[0.4692, 0.2124, 0.3833, 0.3346],
[0.0334, 0.0922, 0.1495, 0.7413],
[0.9635, 0.9588, 0.6876, 0.3338]],
[[0.5308, 0.7876, 0.6167, 0.6654],
[0.9666, 0.9078, 0.8505, 0.2587],
[0.0365, 0.0412, 0.3124, 0.6662]]])
>>> torch.nn.functional.softmax(input, dim=1)
tensor([[[0.1153, 0.0504, 0.0841, 0.1452],
[0.0426, 0.0326, 0.1746, 0.7447],
[0.8421, 0.9171, 0.7413, 0.1101]],
[[0.0936, 0.3415, 0.0923, 0.3757],
[0.8835, 0.5866, 0.6779, 0.3383],
[0.0229, 0.0720, 0.2298, 0.2860]]])
>>> torch.nn.functional.softmax(input, dim=2)
tensor([[[0.2482, 0.3320, 0.1788, 0.2410],
[0.0479, 0.1122, 0.1939, 0.6460],
[0.1885, 0.6287, 0.1638, 0.0190]],
[[0.1232, 0.5403, 0.1262, 0.2103],
[0.3626, 0.2894, 0.2889, 0.0591],
[0.0487, 0.1843, 0.5081, 0.2589]]])
默认是对于第一维进行伸缩。
dim = 0, 0.4692 + 0.5308 = 1
dim = 1, 0.1153 + 0.0426 + 0.8421 = 1
dim = 2, 0.2482 + 0.3320 + 0.1788 + 0.2410 = 1
参考: https://blog.csdn.net/m0_46653437/article/details/111610571
https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.softmax
https://blog.csdn.net/chengyq116/article/details/106893773
3. torch.clamp(input, min, max, *, out=None) → Tensor
用于限制数值的上限和下限,把所有数据限制在设定好的上下限之间。
>>> a = torch.randn(4)
>>> a
tensor([-1.7120, 0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000, 0.1734, -0.0478, -0.0922])
参考: https://pytorch.org/docs/stable/generated/torch.clamp.html
参考数目:
https://pytorch.apachecn.org/docs/1.0/tensors.html