1.torch.max (input)
??????? 输入input:任何形状的张量
??????? 输出:返回输入张量input中所有元素的最大值 。
import torch
a = torch.randn((3,4,5))
b = torch.max(a) # 返回所有张量中的最大值
print(a)
tensor([[[-0.5234, -2.3275, 1.8327, 0.5354, 1.1100],
[ 0.7137, 0.4375, -1.1837, 0.8168, 0.4115],
[ 1.1823, -0.3772, -0.3378, 0.0248, 0.2865],
[ 0.1679, 0.8553, 0.4570, -1.5596, 0.5309]],
[[-0.3231, -0.3478, -0.3662, -0.2204, 0.9318],
[ 0.5757, -1.7895, 0.6656, -2.1833, 0.4355],
[ 1.7979, 0.4196, -0.2651, 0.2820, 0.2115],
[-0.3352, 0.1453, -0.7170, -0.0627, 0.3263]],
[[-1.2078, 1.0757, -0.9695, -1.9247, -1.9016],
[ 1.4642, 0.2526, 1.3293, 0.7860, 0.3486],
[ 0.7677, 0.0916, -0.4585, -0.5579, -1.2199],
[-1.9069, -1.6860, 1.0152, 0.6269, -1.9427]]])
print(a.size())
torch.Size([3, 4, 5])
print(b)
tensor(1.8327)
torch.max (input, dim)
??????? 输入input:? 任何形状的张量? 如输入为 a = torch.randn((3,4,5))?
????????????????????????????????????????????????? ?? 则张量a包含三个维度(3, 4 ,5)分别对应为0 1? 2三个维度
??????? 输入dim:? 要压缩掉的维度??? 如dim=1 则输出a的形状由(3,4,5)变为(3, 5)
???????????????????????????????????????????????????? 如dim=2? 则输出a的形状由(3,4,5)变为(3, 4)
!!! dim等于几? 就将几的维度压缩掉? 注意是压缩掉 就是我只要这个维度的最大值? 其余数据舍弃 !!!
??????? 输出out:?? 输出一个包含两个张量的元组,
????????????????????????两个张量分别是压缩指定dim后得到的最大值新张量和最大值在指定dim的索引值
c = torch.max(a,1) # tensor a还是上边的数据,此外max(a,1)是将a的1维压缩掉.
print(a)
tensor([[[-0.5234, -2.3275, 1.8327, 0.5354, 1.1100],
[ 0.7137, 0.4375, -1.1837, 0.8168, 0.4115],
[ 1.1823, -0.3772, -0.3378, 0.0248, 0.2865],
[ 0.1679, 0.8553, 0.4570, -1.5596, 0.5309]],
[[-0.3231, -0.3478, -0.3662, -0.2204, 0.9318],
[ 0.5757, -1.7895, 0.6656, -2.1833, 0.4355],
[ 1.7979, 0.4196, -0.2651, 0.2820, 0.2115],
[-0.3352, 0.1453, -0.7170, -0.0627, 0.3263]],
[[-1.2078, 1.0757, -0.9695, -1.9247, -1.9016],
[ 1.4642, 0.2526, 1.3293, 0.7860, 0.3486],
[ 0.7677, 0.0916, -0.4585, -0.5579, -1.2199],
[-1.9069, -1.6860, 1.0152, 0.6269, -1.9427]]])
print(c)
torch.return_types.max(
values=tensor([[1.1823, 0.8553, 1.8327, 0.8168, 1.1100],
[1.7979, 0.4196, 0.6656, 0.2820, 0.9318],
[1.4642, 1.0757, 1.3293, 0.7860, 0.3486]]),
indices=tensor([[2, 3, 0, 1, 0],
[2, 2, 1, 2, 0],
[1, 0, 1, 1, 1]]))
3.torch.min() 同torch.max()
4.torch. squ eez e (input, dim=None)
??????? 输入是input,dim默认,则输出是一个将输入张量input中的所有维度中尺寸为1的维度压缩掉后的新张量,例如,如果输入为形状:(A×1×B×C×1×D),则输出张量将为形状:(A×B×C×D),就将尺寸为1的维度都扔掉.
??????? 当dim给定时,压缩(挤压)操作只在给定的dim进行。如果输入的形状为:(A×1×B),torch.squeeze(input, 0)将使张量保持不变,而torch.squeeze(input, 1)将把张量压缩为(A×B)。
5.torch. unsqu eez e (input, dim)
??????? 即为在输入张量input的指定维度dim上增加一个尺寸为1的维度,
a = torch.randn(3,4,5)
a.size() # torch.Size([3, 4, 5])
a = torch.unsqueeze(a,1) # unsqueeze 在a的1维添加一个尺寸为1的维度
a.size() # torch.Size([3, 1, 4, 5])
a = torch.squeeze(a) # 将a中尺寸为1的任意维度压缩掉
a.size() # torch.Size([3, 4, 5])
?6.torch.rand(size)
??????? 返回一个和size形状的张量,张量中的数是来自[0,1)之间的随机数.
>>> torch.rand(4)
tensor([ 0.5204, 0.2503, 0.3525, 0.5673])
>>> torch.rand(2, 3)
tensor([[ 0.8237, 0.5781, 0.6879],
[ 0.3816, 0.7249, 0.0998]])
?6.torch.randn(size)
??????? 返回一个和size形状的张量,张量中的数是来自标准正态分布的随机数,均值为0,方差为1(也称为标准正态分布)。
>>> torch.randn(4)
tensor([-2.1436, 0.9966, 2.3426, -0.6366])
?6.torch.randint(low=0,high,size)
??????? 返回一个size形状的张量,该张量中的随机整数在low(包括)和high(不包括)之间生成。
>>>torch.randint(3, 10, (2, 2))
tensor([[4, 5],
[6, 7]])
|