Understanding the return values of Learner.predict

Hi,

First of all, I think the course and the library are great. This is not a rant.

But, as someone who is new to the library and isn’t a frequent python user, I struggle a little with understanding parts of the docs, like the excerpt below. I’m just spending way too much time trying to mentally digest it all when in the first place I only wanted to know what the method’s return values are.

Learner.predict

Learner.predict (item, rm_type_tfms=None, with_input=False)

Prediction on item, fully decoded, loss function decoded and probabilities

It returns a tuple of three elements with, in reverse order, - the prediction from the model, potentially passed through the activation of the loss function (if it has one) - the decoded prediction, using the potential decodes method from it - the fully decoded prediction, using the transforms used to build the Datasets/DataLoaders

So, I get to my question:
Would it be factually correct if I paraphrased and filtered down the above text to the following?

This method returns a tuple with three elements:

  • The predicted label as a value
  • The predicted label as an index in the returned tensor (i.e. see next returned element)
  • A 1D tensor of the predicted probabilities for each label encountered during learning

Did I capture the essence of it, or am I missing anything important?
Are there any other fastai library docs that could also help?

That is mostly correct for single-label classification. If there is a predicted label it will return it first, then the numerical class (which is also the index), and then the predicted probabilities for each class.

One note, the predicted probabilities are for the number of classes the model outputs, which by default with vision_learner is the number of classes in the dataset. Not the number of seen classes while training.

Here is an example:

('n01440764',
 tensor(0),
 tensor([0.8351, 0.0355, 0.0105, 0.0629, 0.0031, 0.0168, 0.0131, 0.0059, 0.0101,
         0.0072]))

If you want to read more, I wrote a tutorial on inferencing: Inference With fastai - Model Saving, Loading, and Prediction.

2 Likes