6.2 Error Handling

To demonstrate how to properly deal with misbehaving learners, mlr3 ships with the learner classif.debug:

This learner comes with special hyperparameters that let us control

  1. What conditions should be signaled (message, warning, error), and
  2. during which stage the conditions should be signaled (train or predict).

Alternatively, we can tell the Learner to provoke a segfault which tears down the complete R session. With its default settings, it will do nothing special: it learns a random label which is used to create constant predictions.

6.2.1 Encapsulation

By default,mlr3 does not catch conditions such as warnings or errors. Thus, the exception raised by the debug learner stops the execution allowing us to traceback() the error:

The learner execution can be encapsulated though. With encapsulation, exceptions do not stop the program flow and any output is logged to the learner instead of just printed to the console. One way to encapsulate the execution is provided by the package evaluate. The encapsulation can be enabled via mlr_control():

You can also enable the encapsulation for the predict step of a learner by setting encapsulate_predict in mlr_control().

Another possibility to encapsulate is by running everything in a callr session. callr spawns a new R process, and thus even guards the session from segfaults. On the downside, starting new processes comes with a computational overhead.

Without a model, it is not possible to predict:

6.2.2 Fallback learners

Fallback learners have the purpose to continue with the computation in cases where a Learner or a Measure are misbehaving in some sense. Some typical examples include:

  • The learner fails to fit a model during training. This can happen if some convergence criterion is not met or the learner ran out of memory.
  • The learner fails to predict for some or all observations. A typical case could be new factor levels in the test data which the model cannot handle.

The fallback learner from the package mlr3pipelines can be used for these scenarios. This is still work in progress.