我可以成功提交实验以在 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(非常棒)时,
PythonScriptStep
的allow_reuse
参数将查看source_directory
中是否有任何文件在确定步骤是否需要再次运行时发生了变化。
最后,当您希望在 PythonScriptStep
或 Estimators 之间共享通用实用函数而无需复制和粘贴代码时,您可能需要考虑创建自定义 Python 包。
关于python-3.x - 如何在 Azure ML 实验脚本上导入自定义函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62200162/