torch.max能同时返回最大值,及最大值索引
numpy.max返回最大值
axis = -1指倒数第一维度(本例子中等价于 axis=2)
axis = -2指倒数第二个维度(本例子中等价于 axis=1)
axis = -3指倒数第三个维度(本例子中等价于 axis=0)
import numpy
x = numpy.array([[[1, 2 ,3], [3, 0, 1]],
[[2, 0, 1], [66, 16, 7]]])
print("x")
print("------")
print(x)
print("------")
print("numpy.max(x, axis=0)\n",numpy.max(x, axis=0))
print("------")
print("numpy.max(x, axis=-3)\n",numpy.max(x, axis=-3))
print("------")
print("numpy.max(x, axis=1)\n",numpy.max(x, axis=1))
print("------")
print("numpy.max(x, axis=-2)\n",numpy.max(x, axis=-2))
print("------")
print("numpy.max(x, axis=2)\n",numpy.max(x, axis=2))
print("------")
print("numpy.max(x, axis=-1)\n",numpy.max(x, axis=-1))
print("------")
import torch
y = torch.from_numpy(x)
print("y")
print("------")
print(y)
print("------")
print("torch.max(y, 0)", torch.max(y, 0))
print("------")
print("torch.max(y, -3)", torch.max(y, -3))
print("------")
print("torch.max(y, 1)", torch.max(y, 1))
print("------")
print("torch.max(y, -2)", torch.max(y, -2))
print("------")
print("torch.max(y, 2)", torch.max(y, 2))
print("------")
print("torch.max(y, -1)", torch.max(y, -1))
print("------")
test1 = torch.max(y, -1)
print(test1[1])
x
------
[[[ 1 2 3]
[ 3 0 1]]
[[ 2 0 1]
[66 16 7]]]
------
numpy.max(x, axis=0)
[[ 2 2 3]
[66 16 7]]
------
numpy.max(x, axis=-3)
[[ 2 2 3]
[66 16 7]]
------
numpy.max(x, axis=1)
[[ 3 2 3]
[66 16 7]]
------
numpy.max(x, axis=-2)
[[ 3 2 3]
[66 16 7]]
------
numpy.max(x, axis=2)
[[ 3 3]
[ 2 66]]
------
numpy.max(x, axis=-1)
[[ 3 3]
[ 2 66]]
------
y
------
tensor([[[ 1, 2, 3],
[ 3, 0, 1]],
[[ 2, 0, 1],
[66, 16, 7]]])
------
torch.max(y, 0) torch.return_types.max(
values=tensor([[ 2, 2, 3],
[66, 16, 7]]),
indices=tensor([[1, 0, 0],
[1, 1, 1]]))
------
torch.max(y, -3) torch.return_types.max(
values=tensor([[ 2, 2, 3],
[66, 16, 7]]),
indices=tensor([[1, 0, 0],
[1, 1, 1]]))
------
torch.max(y, 1) torch.return_types.max(
values=tensor([[ 3, 2, 3],
[66, 16, 7]]),
indices=tensor([[1, 0, 0],
[1, 1, 1]]))
------
torch.max(y, -2) torch.return_types.max(
values=tensor([[ 3, 2, 3],
[66, 16, 7]]),
indices=tensor([[1, 0, 0],
[1, 1, 1]]))
------
torch.max(y, 2) torch.return_types.max(
values=tensor([[ 3, 3],
[ 2, 66]]),
indices=tensor([[2, 0],
[0, 0]]))
------
torch.max(y, -1) torch.return_types.max(
values=tensor([[ 3, 3],
[ 2, 66]]),
indices=tensor([[2, 0],
[0, 0]]))
------
tensor([[2, 0],
[0, 0]])