在 Azure 机器学习设计器管道中启用日志记录Enable logging in Azure Machine Learning designer pipelines

本文介绍如何将日志记录代码添加到设计器管道。In this article, you learn how to add logging code to designer pipelines. 此外,还介绍了如何使用 Azure 机器学习工作室 Web 门户查看这些日志。You also learn how to view those logs using the Azure Machine Learning studio web portal.

有关如何使用 SDK 创作体验来记录指标的详细信息,请参阅监视 Azure ML 试验运行和指标For more information on logging metrics using the SDK authoring experience, see Monitor Azure ML experiment runs and metrics.

使用“执行 Python 脚本”启用日志记录Enable logging with Execute Python Script

使用“执行 Python 脚本”模块在设计器管道中启用日志记录。Use the Execute Python Script module to enable logging in designer pipelines. 虽然可以使用此工作流记录任何值,但是记录“评估模型”模块中的指标以跟踪各运行期间的模型性能特别有用。Although you can log any value with this workflow, it's especially useful to log metrics from the Evaluate Model module to track model performance across runs.

以下示例演示如何使用“评估模型”和“执行 Python 脚本”模块来记录两个训练模型的均方误差。The following example shows you how to log the mean squared error of two trained models using the Evaluate Model and Execute Python Script modules.

  1. 将“执行 Python 脚本”模块连接到“评估模型”模块的输出 。Connect an Execute Python Script module to the output of the Evaluate Model module.

    将“执行 Python 脚本”模块连接到“评估模型”模块

  2. 将以下代码粘贴到“执行 Python 脚本”代码编辑器中,以记录训练模型的平均绝对误差。Paste the following code into the Execute Python Script code editor to log the mean absolute error for your trained model. 可以使用类似的模式在设计器中记录任何其他值:You can use a similar pattern to log any other value in the designer:

    # dataframe1 contains the values from Evaluate Model
    def azureml_main(dataframe1=None, dataframe2=None):
        print(f'Input pandas.DataFrame #1: {dataframe1}')
    
        from azureml.core import Run
    
        run = Run.get_context()
    
        # Log the mean absolute error to the parent run to see the metric in the run details page.
        # Note: 'run.parent.log()' should not be called multiple times because of performance issues.
        # If repeated calls are necessary, cache 'run.parent' as a local variable and call 'log()' on that variable.
        parent_run = Run.get_context().parent
    
        # Log left output port result of Evaluate Model. This also works when evaluate only 1 model.
        parent_run.log(name='Mean_Absolute_Error (left port)', value=dataframe1['Mean_Absolute_Error'][0])
        # Log right output port result of Evaluate Model.
        parent_run.log(name='Mean_Absolute_Error (right port)', value=dataframe1['Mean_Absolute_Error'][1])
    
        return dataframe1,
    

此代码使用 Azure 机器学习 Python SDK 来记录值。This code uses the Azure Machine Learning Python SDK to log values. 它使用 Run.get_context() 来获取当前运行的上下文。It uses Run.get_context() to get the context of the current run. 然后,它会使用 run.parent.log() 方法将值记录到该上下文中。It then logs values to that context with the run.parent.log() method. 它使用 parent 将值记录到父管道运行,而不是模块运行。It uses parent to log values to the parent pipeline run rather than the module run.

有关如何使用 Python SDK 来记录值的详细信息,请参阅在 Azure ML 训练运行中启用日志记录For more information on how to use the Python SDK to log values, see Enable logging in Azure ML training runs.

查看日志View logs

管道运行完成后,“试验”页中将显示 Mean_Absolute_Error。After the pipeline run completes, you can see the Mean_Absolute_Error in the Experiments page.

  1. 导航到“试验”部分。Navigate to the Experiments section.

  2. 选择试验。Select your experiment.

  3. 选择要查看的试验中的运行。Select the run in your experiment you want to view.

  4. 选择“指标”。Select Metrics.

    在工作室中查看运行指标

后续步骤Next steps

本教程已介绍如何在设计器中使用日志。In this article, you learned how to use logs in the designer. 有关后续步骤,请参阅以下相关文章:For next steps, see these related articles: