Используйте Azure DevOps для создания кода CI/CD для обучения модели с помощью автоматизированного машинного обучения с помощью SDK.

Шаги

  • Импорт библиотек
import logging

# from matplotlib import pyplot as plt
import pandas as pd
import os

import azureml.core
from azureml.core.experiment import Experiment
from azureml.core.workspace import Workspace
from azureml.core.dataset import Dataset
from azureml.train.automl import AutoMLConfig
from azureml.core.compute import ComputeTarget, AmlCompute
from azureml.core.compute_target import ComputeTargetException
# from azureml.widgets import RunDetails
from sklearn.metrics import confusion_matrix

from azureml.core.authentication import ServicePrincipalAuthentication

import numpy as np
import itertools

import argparse 
import json
import os
  • Распечатать версию AML
print("This notebook was created using version 1.29.0 of the Azure ML SDK")
print("You are currently using version", azureml.core.VERSION, "of the Azure ML SDK")
  • Получить аргументы, переданные в командной строке
parse = argparse.ArgumentParser()
parse.add_argument("--tenantid")
parse.add_argument("--acclientid")
parse.add_argument("--accsecret")
    
args = parse.parse_args()
  • Проверка подлинности с помощью субъекта-службы
  • вся информация для аутентификации передается как аргументы командной строки
  • настроен в Azure DevOps
sp = ServicePrincipalAuthentication(tenant_id=args.tenantid, # tenantID
                                    service_principal_id=args.acclientid, # clientId
                                    service_principal_password=args.accsecret) # clientSecret
  • Получить информацию о рабочей области
ws = Workspace.get(name="mlopsdev",
                   auth=sp,
                   subscription_id="c46a9435-c957-4e6c-a0f4-b9a597984773", resource_group="mlops"
  • Теперь создайте название эксперимента
# choose a name for experiment
experiment_name = 'automl-classification-ccard-remote'

experiment=Experiment(ws, experiment_name)

output = {}
output['Subscription ID'] = ws.subscription_id
output['Workspace'] = ws.name
output['Resource Group'] = ws.resource_group
output['Location'] = ws.location
output['Experiment Name'] = experiment.name
pd.set_option('display.max_colwidth', -1)
outputDf = pd.DataFrame(data = output, index = [''])
outputDf.T

ws.get_details()
  • Настройка вычислительного кластера
# Choose a name for your CPU cluster
cpu_cluster_name = "cpu-cluster"

# Verify that cluster does not exist already
try:
    compute_target = ComputeTarget(workspace=ws, name=cpu_cluster_name)
    print('Found existing cluster, use it.')
except ComputeTargetException:
    compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_DS12_V2',
                                                           max_nodes=6)
    compute_target = ComputeTarget.create(ws, cpu_cluster_name, compute_config)

compute_target.wait_for_completion(show_output=True)
  • Скачайте необходимый набор данных
data = "https://automlsamplenotebookdata.blob.core.windows.net/automl-sample-notebook-data/creditcard.csv"
dataset = Dataset.Tabular.from_delimited_files(data)
training_data, validation_data = dataset.random_split(percentage=0.8, seed=223)
label_column_name = 'Class'
  • Настройка конфигурации AutoML
automl_settings = {
    "n_cross_validations": 3,
    "primary_metric": 'average_precision_score_weighted',
    "enable_early_stopping": True,
    "max_concurrent_iterations": 2, # This is a limit for testing purpose, please increase it as per cluster size
    "experiment_timeout_hours": 0.25, # This is a time limit for testing purposes, remove it for real use cases, this will drastically limit ablity to find the best model possible
    "verbosity": logging.INFO,
}

automl_config = AutoMLConfig(task = 'classification',
                             debug_log = 'automl_errors.log',
                             compute_target = compute_target,
                             training_data = training_data,
                             label_column_name = label_column_name,
                             **automl_settings
                            )
  • Отправить эксперимент
remote_run = experiment.submit(automl_config, show_output = False)

remote_run.wait_for_completion(show_output=True)
  • Дождитесь завершения эксперимента
  • Проверить модель
best_run, fitted_model = remote_run.get_output()
fitted_model

# convert the test data to dataframe
X_test_df = validation_data.drop_columns(columns=[label_column_name]).to_pandas_dataframe()
y_test_df = validation_data.keep_columns(columns=[label_column_name], validate=True).to_pandas_dataframe()

# call the predict functions on the model
y_pred = fitted_model.predict(X_test_df)
y_pred

Azure DevOps

  • Установите версию 3.6
  • Настройка зависимостей от агента
  • Установите все библиотеки, необходимые для запуска эксперимента.
  • Путь — AutomatedML/agent_dependicy.sh

  • Скопируйте данные в каталог сборки
  • Цель: $(Build.SourcesDirectory)

  • Путь сценария: AutomatedML/automlpipeline.py
  • Аргументы: — tenantid $(tenatid) — acclientid $(acclientid) — accsecret $(accsecret)
  • Как только все установлено
  • Нажмите Сохранить и поставить в очередь
  • Дождитесь завершения эксперимента

Первоначально опубликовано на https://github.com.