3

我正在 Azure 机器学习服务上构建数据转换和训练管道。我想将我安装的变压器(例如 tf-idf)保存到 blob,以便我的预测管道以后可以访问它。

transformed_data = PipelineData("transformed_data", 
                               datastore = default_datastore,
                               output_path_on_compute="my_project/tfidf")

step_tfidf = PythonScriptStep(name = "tfidf_step",
                              script_name = "transform.py",
                              arguments = ['--input_data', blob_train_data, 
                                           '--output_folder', transformed_data],
                              inputs = [blob_train_data],
                              outputs = [transformed_data],
                              compute_target = aml_compute,
                              source_directory = project_folder,
                              runconfig = run_config,
                              allow_reuse = False)

上面的代码将转换器保存到当前运行的文件夹中,该文件夹在每次运行期间动态生成。

我想将转换器保存到 blob 上的固定位置,以便稍后在调用预测管道时访问它。

我尝试使用DataReference类的实例作为PythonScriptStep输出,但会导致错误: ValueError: Unexpected output type: <class 'azureml.data.data_reference.DataReference'>

这是因为PythonScriptStep只接受PipelineDataOutputPortBinding对象作为输出。

我怎样才能保存我安装的变压器,以便以后可以通过任何任意过程(例如我的预测管道)访问它?

4

3 回答 3

1

这可能对您的需求不够灵活(另外,我还没有测试过),但是如果您使用 scikit-learn,一种可能性是将 tf-idf/transformation 步骤包含到 scikit-learnPipeline对象中并注册进入您的工作区。

因此,您的训练脚本将包含:

pipeline = Pipeline([
    ('vectorizer', TfidfVectorizer(stop_words = list(text.ENGLISH_STOP_WORDS))),
    ('classifier', SGDClassifier()
])

pipeline.fit(train[label].values, train[pred_label].values)

# Serialize the pipeline
joblib.dump(value=pipeline, filename='outputs/model.pkl')

并且您的实验提交脚本将包含

run = exp.submit(src)
run.wait_for_completion(show_output = True)
model = run.register_model(model_name='my_pipeline', model_path='outputs/model.pkl')

然后,您可以使用注册的“模型”并将其部署为文档中解释的服务,方法是将其加载到评分脚本中

model_path = Model.get_model_path('my_pipeline')
# deserialize the model file back into a sklearn model
model = joblib.load(model_path) 

但是,这会在您的管道中进行转换,因此不会像您要求的那样模块化......

于 2019-06-16T08:20:44.653 回答
1

另一种选择是使用DataTransferStep并使用它将输出复制到“已知位置”。此笔记本包含使用 DataTransferStep 从各种受支持的数据存储复制数据的示例。

from azureml.data.data_reference import DataReference
from azureml.exceptions import ComputeTargetException
from azureml.core.compute import ComputeTarget, DataFactoryCompute
from azureml.pipeline.steps import DataTransferStep

blob_datastore = Datastore.get(ws, "workspaceblobstore")

blob_data_ref = DataReference(
    datastore=blob_datastore,
    data_reference_name="knownloaction",
    path_on_datastore="knownloaction")

data_factory_name = 'adftest'

def get_or_create_data_factory(workspace, factory_name):
    try:
        return DataFactoryCompute(workspace, factory_name)
    except ComputeTargetException as e:
        if 'ComputeTargetNotFound' in e.message:
            print('Data factory not found, creating...')
            provisioning_config = DataFactoryCompute.provisioning_configuration()
            data_factory = ComputeTarget.create(workspace, factory_name, provisioning_config)
            data_factory.wait_for_completion()
            return data_factory
        else:
            raise e

data_factory_compute = get_or_create_data_factory(ws, data_factory_name)

# Assuming output data is your output from the step that you want to copy

transfer_to_known_location = DataTransferStep(
    name="transfer_to_known_location",
    source_data_reference=[output_data],
    destination_data_reference=blob_data_ref,
    compute_target=data_factory_compute
    )

from azureml.pipeline.core import Pipeline
from azureml.core import Workspace, Experiment

pipeline_01 = Pipeline(
    description="transfer_to_known_location",
    workspace=ws,
    steps=[transfer_to_known_location])

pipeline_run_01 = Experiment(ws, "transfer_to_known_location").submit(pipeline_01)
pipeline_run_01.wait_for_completion()
于 2019-08-08T15:48:24.937 回答
1

另一种解决方案是DataReference作为输入传递给您的PythonScriptStep.

然后在里面transform.py你可以把它DataReference作为命令行参数来读。

您可以解析它并将其用作任何常规路径来保存矢量化器。

例如,您可以:

step_tfidf = PythonScriptStep(name = "tfidf_step",
                              script_name = "transform.py",
                              arguments = ['--input_data', blob_train_data, 
                                           '--output_folder', transformed_data,
                                           '--transformer_path', trained_transformer_path],
                              inputs = [blob_train_data, trained_transformer_path],
                              outputs = [transformed_data],
                              compute_target = aml_compute,
                              source_directory = project_folder,
                              runconfig = run_config,
                              allow_reuse = False)

然后在你的脚本中(transform.py在上面的例子中)你可以例如:

import argparse
import joblib as jbl
import os

from sklearn.feature_extraction.text import TfidfVectorizer

parser = argparse.ArgumentParser()
parser.add_argument('--transformer_path', dest="transformer_path", required=True)
args = parser.parse_args()

tfidf = ### HERE CREATE AND TRAIN YOUR VECTORIZER ###

vect_filename = os.path.join(args.transformer_path, 'my_vectorizer.jbl')


额外:第三种方法是将矢量化器注册为工作区中的另一个模型。然后,您可以像使用任何其他注册模型一样使用它。(尽管此选项不涉及对 blob 的显式写入 - 如上述问题中所述)

于 2019-07-08T11:40:40.653 回答