torch.max(input) → Tensor
用于计算张量(tensor)中的最大值的值和索引。
import torch
a = torch.randn(1, 3)
print(a)
print(torch.max(a))
tensor([[-0.6500, -0.3239, -0.2899]])
tensor(-0.2899)
dim=1表示从每行中选取最大
import torch
a = torch.randn(4, 4)
print(a)
print(torch.max(input=a, dim=1))
tensor([[-1.2154, 1.1472, -2.3489, 0.6813],
[ 1.4553, 1.9520, -0.8385, 0.7121],
[ 0.2057, 1.2206, 1.3722, -0.7365],
[-0.0992, 0.3109, 1.5323, -0.5297]])
torch.return_types.max(
values=tensor([1.1472, 1.9520, 1.3722, 1.5323]),
indices=tensor([1, 1, 2, 2]))
dim=0表示从每列中选取最大
import torch
a = torch.randn(4, 4)
print(a)
print(torch.max(input=a, dim=0))
tensor([[-0.2915, -0.1751, 0.2905, 0.5155],
[ 1.3450, -1.0030, -0.1825, -0.0415],
[ 1.4867, -1.4277, 0.1875, -1.9632],
[-0.0909, -1.7179, -0.8297, -1.4644]])
torch.return_types.max(
values=tensor([ 1.4867, -0.1751, 0.2905, 0.5155]),
indices=tensor([2, 0, 0, 0]))
import torch
a = torch.randn(4, 4)
print(a)
torch_data = torch.max(input=a, dim=1)
print(torch_data[0])
# tensor([0.9787, 0.0933, 0.9512, 0.9254])
print(torch_data[0].view(torch_data[0].size()[0], 1))
# tensor([[ 1.6914],
# [-0.1405],
# [ 0.8139],
# [-0.3309]])