python-3.x - 如何在 Azure ML 实验脚本上导入自定义函数?

标签 python-3.x azure scikit-learn azure-machine-learning-service

我可以成功提交实验以在 Azure ML 上的远程计算目标上进行处理。

在我的笔记本中,为了提交实验,我有:

# estimator
estimator = Estimator(
    source_directory='scripts',
    entry_script='exp01.py',
    compute_target='pc2',
    conda_packages=['scikit-learn'],
    inputs=[data.as_named_input('my_dataset')],
    )

# Submit
exp = Experiment(workspace=ws, name='my_exp')

# Run the experiment based on the estimator
run = exp.submit(config=estimator)
RunDetails(run).show()
run.wait_for_completion(show_output=True)

但是,为了保持简洁,我想在辅助脚本上定义我的通用函数,因此第一个将导入它。

在我的脚本实验文件 exp01.py 中,我想要:

import custom_functions as custom

# azure experiment start
run = Run.get_context()

# the data from azure datasets/datastorage
df = run.input_datasets['my_dataset'].to_pandas_dataframe()

# prepare data
df_transformed = custom.prepare_data(df)

# split data
X_train, X_test, y_train, y_test = custom.split_data(df_transformed)

# run my models.....
model_name = 'RF'
model = custom.model_x(model_name, a_lot_of_args)

# log the results
run.log(model_name, results)

# azure finish
run.complete()

问题是:Azure 不允许我导入 custom_functions.py。

你做得怎么样?

最佳答案

TL;DR 在您的情况下,您放入 source_directory 中的任何文件、脚本 都将可供估算器使用。

要实现这一点,只需在包含 prepare_data()scripts 文件夹中创建一个名为 custom_functions.py 的文件,split_data()model_x() 函数。

我还建议您在 source_directory 文件夹中仅包含您需要的内容,并为每个估算器创建不同的文件夹,因为:

  • 当您使用远程compute_target时,整个文件夹的内容将被上传,并且
  • 当您开始使用 ML Pipeilnes(非常棒)时,PythonScriptStepallow_reuse 参数将查看 source_directory 中是否有任何文件在确定步骤是否需要再次运行时发生了变化。

最后,当您希望在 PythonScriptStep 或 Estimators 之间共享通用实用函数而无需复制和粘贴代码时,您可能需要考虑创建自定义 Python 包。

关于python-3.x - 如何在 Azure ML 实验脚本上导入自定义函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62200162/

相关文章:

python - 如何在不更改导入的情况下将 Python 3 模块更改为包?

python - 如何创建局部动态变量

python-3.x - 使用Pydrive按modifiedTime查询文件

azure - 格式化 Powershell 脚本对象输出?

python - ModuleNotFoundError : No module named 'sklearn'

python - Scikit-Learn n_jobs 多处理锁定到一个核心

python - Algolia reindex 命令在 urllib3 中失败并出现异常

azure - Windows Azure 上的网站、云服务和虚拟机应如何选择

azure - 如何连接 Azure 中现有的自动化帐户和 Log Analytics 工作区?

python - ScikitLearn 随机森林中的欠采样与 class_weight