torch.max()
单个Tensor
arr = torch.rand(3, 4)
top_value, top_indices = torch.max(arr, dim=1)
>>>
tensor([[0.0038, 0.2420, 0.6851, 0.1004],
[0.2818, 0.0513, 0.2679, 0.7817],
[0.3812, 0.3211, 0.9846, 0.1714]])
沿着dim=1求最大值, 所以输出值有3个
tensor([0.6851, 0.7817, 0.9846])
tensor([2, 3, 2])
两个Tensor, 这两个Tensor的shape一致
依次比较每一个元素, 并取最大值
arr1 = torch.rand(3, 4)
arr2 = torch.rand(3, 4)
res = torch.max(arr1, arr2)
>>>
tensor([[0.7124, 0.1979, 0.9612, 0.3003],
[0.4030, 0.6211, 0.4740, 0.9530],
[0.1326, 0.2359, 0.3715, 0.2889]])
tensor([[0.4937, 0.9679, 0.6621, 0.3993],
[0.4538, 0.2039, 0.5995, 0.2262],
[0.5887, 0.4631, 0.7709, 0.5620]])
tensor([[0.7124, 0.9679, 0.9612, 0.3993],
[0.4538, 0.6211, 0.5995, 0.9530],
[0.5887, 0.4631, 0.7709, 0.5620]])
numpy.max()
arr = np.random.randint(low=1, high=10, size=(3, 4))
top_values = np.max(arr, axis=1)
top_indices = np.argmax(arr, axis=1)
>>>
[[8 5 4 2]
[7 1 7 3]
[6 8 7 3]]
[8 7 8]
[0 0 1]