Argmax with PyTorch
How can I use argmax with PyTorch? Meta: How could I have answered my own question? I searched the PyTorch docs and the PyTorch repo for “argmax” but got no results. Does it make sense to use argmax with a GPU?
MatthewKleinsmith
<SCODE>values, indices = tensor.max(0)
values, indices = torch.max(tensor, 0)
<ECODE>
fmassa
platero
Andre_Holzner
Thank you very much!
platero
Any ideas for how to use this max function in a differentiable way? A custom loss function i’m writing has to do with the indices of max values. Not sure how to redo the loss function such that it uses differentiable components.
bgenchel
I am not sure if I understand your problem. Do you want to have gradients with respect to indices? Well, indices are integers by definition and you cannot take derivatives of a function with respect to a variable that is defined over the integers only…
dpernes
what does the 0 do in the indexing?
Brando_Miranda
BlakeWest
you can now do torch.argmax(preds, dim=1) in version 0.4.0
jchillin
chenchr
what about argmax from the tensor itself like: <SCODE>import torch

x = torch.randn(3)
x.argmax(-1)
<ECODE> probably useful: Calculating accuracy of the current minibatch? 8 https://stackoverflow.com/questions/51503851/calculate-the-accuracy-every-epoch-in-pytorch 2 https://towardsdatascience.com/understanding-dimensions-in-pytorch-6edf9972d3be 3 How does one get the predicted classification label from a pytorch model? 6
Brando_Miranda
Brando_Miranda