Model Debugging

This tutorial demonstrates how to debug a model specification Python script during model training. This allows for single-step debugging while the model is being actively trained.

Overview

An MLTK machine learning model is defined in a model specification script which is a standard Python script. As such, any tool used to debug a Python script may be used to debug the model script.

In this tutorial, we will use Visual Studio Code with the default Python Extension. With this, we will be able to single-step debug the machine learning model script as it is actively being trained.

Setup Environment

1) Install the MLTK Python Package

First, install the MLTK Python package. It is recommended to create a virtual environment for the installation.

2) Install VS Code and the Python Extension

Next, install Visual Studio Code and the Python Extension.

Be sure to select the “Python Interpreter” that matches the Python used for step 1)
i.e. if you used a virtual environment in step 1, then ensure you select that interpreter in VS Code.

Debug model: basic_example

For this tutorial, we’ll debug the basic_example that comes with the MLTK. Download the basic_example Python script to your local PC and open it in Visual Studio Code.

Next, using VSCode add some breakpoints to the basic_example.py file. After adding the breakpoints, on the top-right, select the Debug Python File button which will launch the Python debugger.

Once the debugger is launched, it should stop at your first breakpoint. Using VS Code, you should be able to view the callstack as well as the contents of the various variables. Using the controls on the top-right, you can “step over” or “continue” debugging.

Refer to Python debugging in VS Code for more details.

How does debugging work?

Recall that a model specification script is a standard Python script. So it executes just like any other Python script.

As such, if we add something like the following to the end of the model script:

if __name__ == '__main__':
    mltk_core.train_model(my_model, clean=True, test=True)

then when the model script is invoked, e.g.:

python basic_example.py

the script will execute the IF statement: if __name__ == '__main__':

Inside this IF block, we can add any Python code we like, including calling the train_model API.

NOTE: If we invoke our model from the train command, e.g.:

mltk train basic_example.py

then the IF statement: if __name__ == '__main__': will not execute as the IF statement is not true in this case.