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.
1) Install the MLTK Python Package¶
2) Install VS 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¶
Next, using VSCode add some breakpoints to the
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.:
the script will execute the
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
if __name__ == '__main__': will not execute as the IF statement is not true in this case.