之前我们用 AML 的介面来做 AutoML,现在我们就来用 SDK 做 AutoML 啦!目前主要支援分类、回归、时间序列预测这三种题目。今天我们继续拿铁达尼号出来测试。今天的内容会包含很多之前讲过的 SDK 功能哦!如果看不懂程序码的话,我们再来重读前面的内容吧!
先 pip install azureml-train-automl
。
我们先来取得之前建立好的铁达尼号资料集。
import azureml.core
from azureml.core import Workspace,Dataset
ws = Workspace.from_config()
titanic_ds = ws.datasets.get("titanic")
train_ds, test_ds = titanic_ds.random_split(percentage=0.7, seed=5566)
automl_utils.get_primary_metrics
来看有什麽指标可以让我们使用。import azureml.train.automl.utilities as automl_utils
for metric in automl_utils.get_primary_metrics('classification'):
print(metric)
# 会印出下面几种,我们选 AUC 吧!
# norm_macro_recall
# precision_score_weighted
# accuracy
# average_precision_score_weighted
# AUC_weighted
AutoMLConfig
来设定我们的 AutoML:from azureml.train.automl import AutoMLConfig
automl_config = AutoMLConfig(name='AutoML SDK Titanic ',
task='classification', # 这是我们要做的分类问题
compute_target='AutoMLCompute', # 这里是 cluster
training_data = train_ds,
validation_data = test_ds,
label_column_name='Survived', # 这里是你要预测的 column
iterations=10, # 如果未指定,预设值为1000次反覆运算。
primary_metric = 'AUC_weighted', # 这是我们刚刚选定的指标
max_concurrent_iterations=2,
featurization='auto'
)
from azureml.core.experiment import Experiment
from azureml.widgets import RunDetails
print('Submitting Auto ML experiment...')
automl_experiment = Experiment(ws, 'AutoML-SDK-Titanic')
automl_run = automl_experiment.submit(automl_config)
RunDetails(automl_run).show()
automl_run.wait_for_completion(show_output=True)
在 Notebook 里的话,会看到类似下面的画面,一直会变化哦:
for run in automl_run.get_children():
print('Run ID', run.id)
for metric in run.get_metrics():
print('\t', run.get_metrics(metric))
也可以到 AML 介面里的 Automated ML 里去看,如下图。
best_run, fitted_model = automl_run.get_output()
best_run_metrics = best_run.get_metrics()
for metric_name in best_run_metrics:
metric = best_run_metrics[metric_name]
print(metric_name, metric)
for step_ in fitted_model.named_steps:
print(step_)
from azureml.core import Model
best_run.register_model(model_path='outputs/model.pkl', model_name='titanic-automl-sdk',
tags={'Training strategy':'Auto ML'},
properties={'AUC': best_run_metrics['AUC_weighted'], 'Accuracy': best_run_metrics['accuracy']})
for model in Model.list(ws):
print(model.name, 'version:', model.version)
for tag_name in model.tags:
tag = model.tags[tag_name]
print ('\t',tag_name, ':', tag)
for prop_name in model.properties:
prop = model.properties[prop_name]
print ('\t',prop_name, ':', prop)
print('\n')
或是去图形化介面看,可以看到我们刚刚跑好的最佳模型已经被注册了。
有没有觉得 AutoML SDK 依然很容易操作上手呢?今天我们把之前学的 SDK 内容大部份都用上了罗!更多 AutoML 的设定参数可以看这里。
明天我们要再更深入来看 SDK 怎麽辅助我们调整训练 AI 模型时的超参数哦!
<<: Day26 - 【概念篇】Keycloak使用基本概念 - 第二部分: User & Group
相较於传统的 Android View,Jetpack Compose 在 Android 开发上还...
前言 昨天我们介绍了FER2013表情资料集,今天要来读取资料与做探索性资料分析。 Explorat...
CSS的简写通常很直觉,会取每个音节的第一个字母来用,但有些似乎是有重叠的关系,就会不太一样,需要特...
HI 版上各位资深前辈 小弟目前遇到一个问题,研究不出问题,不知道版上有无前辈有遇过类似问题 小弟公...
生活中的每个细节,有些人习惯使用图像的方式做纪录;有些人更喜欢使用文字去做纪录。 那在资讯领域中呢?...