--- title: Inference keywords: fastai sidebar: home_sidebar summary: "This contains the code required for inference." description: "This contains the code required for inference." nb_path: "nbs/052a_inference.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}

Learner.get_X_preds[source]

Learner.get_X_preds(X, y=None, bs=64, with_input=False, with_decoded=True, with_loss=False)

{% endraw %} {% raw %}
{% endraw %}

Get the predictions and targets, optionally with_input and with_loss.

with_decoded will also return the decoded predictions (it reverses the transforms applied).

The order of the output is the following:

  • input (optional): if with_input is True
  • probabiblities (for classification) or predictions (for regression)
  • target: if y is provided. Otherwise None.
  • predictions: predicted labels. Predictions will be decoded if with_decoded=True.
  • loss (optional): if with_loss is set to True and y is not None.
{% raw %}
from tsai.data.external import get_UCR_data
dsid = 'OliveOil'
X, y, splits = get_UCR_data(dsid, split_data=False)
X_test = X[splits[1]]
y_test = y[splits[1]]
{% endraw %} {% raw %}
learn = load_learner("./models/test.pth")
{% endraw %}

⚠️ Warning: load_learner (from fastai) requires all your custom code be in the exact same place as when exporting your Learner (the main script, or the module you imported it from).

{% raw %}
test_probas, test_targets, test_preds = learn.get_X_preds(X_test, with_decoded=True)
test_probas, test_targets, test_preds
(TSTensor(vars:30, len:4, device=cpu),
 None,
 array(['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
        '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
        '4', '4', '4', '4'], dtype='<U1'))
{% endraw %} {% raw %}
test_probas2, test_targets2, test_preds2 = learn.get_X_preds(X_test, y_test, with_decoded=True)
test_probas2, test_targets2, test_preds2
(TSTensor(vars:30, len:4, device=cpu),
 TensorCategory([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
         3, 3, 3, 3, 3, 3]),
 array(['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
        '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
        '4', '4', '4', '4'], dtype='<U1'))
{% endraw %} {% raw %}
test_probas3, test_targets3, test_preds3, test_losses3 = learn.get_X_preds(X_test, y_test, with_loss=True, with_decoded=True)
test_probas3, test_targets3, test_preds3, test_losses3
(TSTensor(vars:30, len:4, device=cpu),
 TensorCategory([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
         3, 3, 3, 3, 3, 3]),
 array(['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
        '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
        '4', '4', '4', '4'], dtype='<U1'),
 TensorBase([1.4530, 1.4531, 1.4529, 1.4528, 1.4529, 1.5898, 1.5897, 1.5898, 1.5898,
         1.5898, 1.5899, 1.5899, 1.5897, 1.5897, 1.4280, 1.4281, 1.4282, 1.4281,
         1.1323, 1.1321, 1.1321, 1.1321, 1.1322, 1.1322, 1.1322, 1.1320, 1.1321,
         1.1322, 1.1319, 1.1322]))
{% endraw %} {% raw %}
from fastcore.test import test_eq
test_eq(test_probas, test_probas2)
test_eq(test_preds, test_preds2)
test_eq(test_probas, test_probas3)
test_eq(test_preds, test_preds3)
{% endraw %}