Pro*_*mer 10 python matrix argmax pytorch tensor
我正在argmax研究 PyTorch 的功能,其定义为:
torch.argmax(input, dim=None, keepdim=False)
Run Code Online (Sandbox Code Playgroud)
考虑一个例子
a = torch.randn(4, 4)
print(a)
print(torch.argmax(a, dim=1))
Run Code Online (Sandbox Code Playgroud)
在这里,当我使用 dim=1 而不是搜索列向量时,该函数会搜索行向量,如下所示。
print(a) :
tensor([[-1.7739, 0.8073, 0.0472, -0.4084],
[ 0.6378, 0.6575, -1.2970, -0.0625],
[ 1.7970, -1.3463, 0.9011, -0.8704],
[ 1.5639, 0.7123, 0.0385, 1.8410]])
print(torch.argmax(a, dim=1))
tensor([1, 1, 0, 3])
Run Code Online (Sandbox Code Playgroud)
就我的假设而言,dim = 0 代表行,dim = 1 代表列。
kma*_*o23 17
它的时间来正确地理解如何axis或dim在PyTorch论证工作:
一旦你理解了上面的图片,下面的例子应该是有意义的:
Run Code Online (Sandbox Code Playgroud)| v dim-0 ---> -----> dim-1 ------> -----> --------> dim-1 | [[-1.7739, 0.8073, 0.0472, -0.4084], v [ 0.6378, 0.6575, -1.2970, -0.0625], | [ 1.7970, -1.3463, 0.9011, -0.8704], v [ 1.5639, 0.7123, 0.0385, 1.8410]] | v
# argmax (indices where max values are present) along dimension-1
In [215]: torch.argmax(a, dim=1)
Out[215]: tensor([1, 1, 0, 3])
Run Code Online (Sandbox Code Playgroud)
注意:('dimension' 的dim缩写)是NumPy中'axis'的火炬等价物。