If your code does not run as expected, you must locate the cause. To locate the cause, you can carefully check the code. You can also enable the debug logging feature and use an online debugging tool to check the variables and logic of the code based on debug logs. This topic describes how to use WebIDE of Data Science Workshop (DSW) to debug the Python code in the sample notebook that is provided by DSW of Machine Learning Platform for AI (PAI).

Background information

DSW provides a few examples to demonstrate how to tune hyperparameters by using AutoML. The code in the example "Quick start to hyperparameter tuning by using AutoML" is often stuck at the tuner.fit() method. The following figure shows the error logs in an output cell of the sample notebook.Error logsThe error logs do not provide enough information for you to locate the cause. You can check the code of tuner and debug the code. To check the code of tuner, perform the following steps:
  1. Find the location where the class of tuner is defined.
    The class of tuner is AutoTuner and is defined in the pai.automl.hpo Python package, as shown in the following figure.Code of tuner
  2. Search for the AutoTuner class to obtain the path of the AutoTuner class in DSW. In this example, the path is /home/admin/.local/lib/python3.6/site-packages/pai/automl. Open the file of the AutoTuner class in WebIDE as a project, as shown in the following figure.Code of tuner
Combing all of the code is time-consuming. You can use a debugger to set a breakpoint at the position where the code is stuck. This helps you diagnose issues. You can use WebIDE to debug programs that are run inside or outside WebIDE. To debug a program that is run outside WebIDE, you must add the Python Tools for Visual Studio Debug Server (PTVSD) code to the program. After the PTVSD code is added to a program, the program is paused at the PTVSD code until a debugger is attached to the program. In this topic, the code in the sample notebook is run in JupyterLab and WebIDE is used to debug the code step by step.

Procedure

  1. Add the PTVSD code to the program that you want to debug.
    1. Optional:If you have not installed PTVSD, run the following code to install it:
      pip install --upgade ptvsd.
    2. Add the following code to the program that you want to debug:
      import ptvsd
      print("Waiting for debugger attach")
      # Allow other computers to attach to ptvsd at this IP address and port.
      ptvsd.enable_attach(address=('192.0.2.1', 3000), redirect_output=True)
      # Pause the program until a remote debugger is attached
      ptvsd.wait_for_attach()
      In the PTVSD code, 192.0.2.1 indicates the IP address of the server on which the program is run, and 3000 indicates the port on which the debugger listens. In this example, add the PTVSD code to the autotuner.py file in /home/admin/.local/lib/python3.6/site-packages/pai/automl/hpo/ in WebIDE. The following figure shows the position of the PTVSD code in the autotuner.py file.Add the PTVSD code
    3. Rerun the code in the sample notebook in JupyterLab. Then, view the running result in WebIDE.
      Running resultIf Waiting for debugger attach is displayed below the cell of tunner.fit(), the debugger configuration is in effect.
  2. In WebIDE, connect the debugger to PTVSD.
    To connect the debugger to PTVSD, you must add the debugger configuration to the launch.json file in the .vscode folder, as shown in the following figure.Add the debugger configurationThe debugger configuration includes the following key parameters:
    • host: the IP address of the server on which the debugger resides. In this example, the debugger and the program that you want to debug reside on the same server. Therefore, set the host parameter to localhost.
    • port: the port on which the debugger listens. This port is also used to connect to PTVSD. In this example, set the port parameter to 3000.
    • justMyCode: specifies whether to enable the Just My Code feature. Set this parameter to false. Otherwise, the breakpoint does not take effect.