2

After using get_preds I get a list of tensors. Something like:

[
    [0.9, 0.1],
    [0.85, 0.15],
    [0.92, 0.08],
    ...
]

And my classes are [0,1]. How should I translate these tensors to the corresponding (the most likely) class? Please see my current approach below

probs = learn.get_preds(ds_type=DatasetType.Test)[0]

def probs2class(item): 
    return max(range(len(item)), key=item.__getitem__) 

print(map(probs2class, probs))

I searched the documentation like crazy but maybe for wrong terms? What is the general way to go from probs to class predictions?

ajthinking
  • 3,386
  • 8
  • 45
  • 75
  • What's wrong about your current approach? You can also use Numpy: `np.arange(probs.shape[1])[np.argmax(probs, axis=1)]`. – a_guest Apr 07 '19 at 22:45
  • It does get the work done, but since this is a general task that needs to be solved after all classification problems I would assume there was an interpreter available either as a global helper function or as a method on the learn object? At the moment this feel like cluttering my main code file. Makes sense? – ajthinking Apr 08 '19 at 02:54
  • Well you'll have to add that code anyway. Actually for class labels starting at zero you can directly use `argmax`, i.e. `torch.argmax(probs, dim=1)`. No need for the `arange`. There's another question on this topic, I'll mark as duplicate. – a_guest Apr 08 '19 at 08:39
  • 1
    Ok, so you are saying that there is no helper for that and that would be a good answer to my question. I would not understand why that would be a duplicate question to the one referred to though? I asked about how to do that with fast.ai which sits on top of pytorch. Indeed that question is similar question but has nothing to do with fast.ai @Shai – ajthinking Apr 09 '19 at 03:04
  • I mean it's exactly one additional function call, it doesn't even necessarily add a line to your code. Instead of `probs = network.forward(x)` you just write `classes = network.forward(x).argmax(dim=1)`. Why would you need a helper for that? – a_guest Apr 09 '19 at 04:13
  • 1
    That does gets me the indexes, but in general, I would like to get the corresponding *class*. In this case the indexes happen to match the class so no need for lookup, but that might be `['Dog', 'Cat', 'Horse']`. Also, using `argmax` with a dimension is slightly less readable and it is kind of explicit compared to everything else that fast.ai wraps into describing function names. – ajthinking Apr 09 '19 at 05:26
  • 1
    Well then it is one more step to apply these indices to the class labels (`classes = labels[indices]`). Feel free to write your own utility function, post it as an answer and you could even create a merge request at [fastai](https://github.com/fastai/fastai). – a_guest Apr 09 '19 at 15:51
  • I think the answer is: `for prob in probs: print(learn.data.classes[np.argmax(prob, 0)])` which prints eg 'Dog', 'Dog', 'Cat'. `learn.data.classes` is a List of the classes. get_preds might need ordered=True adding depending on the type of learner. Can't post an answer properly as the question is (IMO incorrectly) closed. – Jethro Aug 26 '20 at 17:18

0 Answers0