行业网站建设报价,织梦婚纱网站模板,北京响应式网站建设,优而思 网站本文将指导你探索 Azure 机器学习服务的主要功能。在这里#xff0c;你将学习如何创建、注册并发布模型。此教程旨在让你深入了解 Azure 机器学习的基础知识和常用操作。 关注TechLead#xff0c;分享AI全维度知识。作者拥有10年互联网服务架构、AI产品研发经验、团队管理经验… 本文将指导你探索 Azure 机器学习服务的主要功能。在这里你将学习如何创建、注册并发布模型。此教程旨在让你深入了解 Azure 机器学习的基础知识和常用操作。 关注TechLead分享AI全维度知识。作者拥有10年互联网服务架构、AI产品研发经验、团队管理经验同济本复旦硕复旦机器人智能实验室成员阿里云认证的资深架构师项目管理专业人士上亿营收AI产品研发负责人。 一、开始之前的准备
要深入 Azure 机器学习首先确保你有一个工作区。如果你还未设置工作区那么请按照指引完成必要的资源配置来搭建你的工作区并了解其基本操作。
请登录到Azure工作室并选择你的工作区如果它还未被激活。
接下来在工作区内你可以选择启动或新建一个笔记本
如果你打算把代码复制到某个单元那么请新建一个笔记本。作为另一种选择你可以在工作室的“示例”区域找到 tutorials/get-started-notebooks/quickstart.ipynb。打开后点击“克隆”这样这个笔记本就会被保存到你的“文件”里。 二、配置内核
当你打开笔记本时可以在顶部的工具栏中找到并设定一个计算实例前提是你之前还没有设立过。
如果发现计算实例处于暂停状态请点击“启动计算”并耐心等待其启动完成。
当出现提示横幅要求你完成身份验证时请点击“身份验证”进行操作。 三、建立工作区连接
在开始编写代码之前我们要确保有办法正确引用工作区。工作区是 Azure 机器学习的核心资源它为你在 Azure 机器学习上创建的所有项目提供了统一的管理点。
你会为这个工作区连接创建名为 ml_client 的句柄。之后你可以利用 ml_client 来统筹各种资源和任务。
请在下方的代码单元格里输入你的订阅ID、资源组名以及工作区名。要找到这些信息的方法如下
在 Azure 机器学习工作室界面的右上角点击你的工作区名称。从显示的信息中复制工作区、资源组和订阅ID。一次复制一个信息粘贴到代码中后再返回继续复制下一个。 from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential# authenticate
credential DefaultAzureCredential()# Get a handle to the workspace
ml_client MLClient(credentialcredential,subscription_idSUBSCRIPTION_ID,resource_group_nameRESOURCE_GROUP,workspace_nameAML_WORKSPACE_NAME,
)
四、编写训练代码
首先我们需要制定训练代码并保存为 Python 文件命名为 main.py。 开始时为这个脚本设置一个专门的源代码目录。
import ostrain_src_dir ./src
os.makedirs(train_src_dir, exist_okTrue)这段代码负责数据预处理对数据进行训练和测试的划分。接着脚本将利用这些数据来培训一个基于树的机器学习模型并输出该模型。 在整个管道运行过程中我们会利用 MLFlow 来记录相关参数和性能指标。 接下来的代码单元将使用 IPython magic 命令把训练脚本保存到你刚刚设定的目录中。
%%writefile {train_src_dir}/main.py
import os
import argparse
import pandas as pd
import mlflow
import mlflow.sklearn
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_splitdef main():Main function of the script.# input and output argumentsparser argparse.ArgumentParser()parser.add_argument(--data, typestr, helppath to input data)parser.add_argument(--test_train_ratio, typefloat, requiredFalse, default0.25)parser.add_argument(--n_estimators, requiredFalse, default100, typeint)parser.add_argument(--learning_rate, requiredFalse, default0.1, typefloat)parser.add_argument(--registered_model_name, typestr, helpmodel name)args parser.parse_args()# Start Loggingmlflow.start_run()# enable autologgingmlflow.sklearn.autolog()####################prepare the data###################print( .join(f{k}{v} for k, v in vars(args).items()))print(input data:, args.data)credit_df pd.read_csv(args.data, header1, index_col0)mlflow.log_metric(num_samples, credit_df.shape[0])mlflow.log_metric(num_features, credit_df.shape[1] - 1)train_df, test_df train_test_split(credit_df,test_sizeargs.test_train_ratio,)#####################/prepare the data#######################################train the model################### Extracting the label columny_train train_df.pop(default payment next month)# convert the dataframe values to arrayX_train train_df.values# Extracting the label columny_test test_df.pop(default payment next month)# convert the dataframe values to arrayX_test test_df.valuesprint(fTraining with data of shape {X_train.shape})clf GradientBoostingClassifier(n_estimatorsargs.n_estimators, learning_rateargs.learning_rate)clf.fit(X_train, y_train)y_pred clf.predict(X_test)print(classification_report(y_test, y_pred))####################/train the model##############################################save and register model########################### Registering the model to the workspaceprint(Registering the model via MLFlow)mlflow.sklearn.log_model(sk_modelclf,registered_model_nameargs.registered_model_name,artifact_pathargs.registered_model_name,)# Saving the model to a filemlflow.sklearn.save_model(sk_modelclf,pathos.path.join(args.registered_model_name, trained_model),)############################/save and register model############################ Stop Loggingmlflow.end_run()if __name__ __main__:main()正如你将在脚本中看到的一旦模型训练完毕它会被保存并在工作区中进行注册。这样这个已注册的模型就可以被用于推理节点了。
为了在“文件”区域看到新创建的文件夹和脚本你可能需要点击“刷新”按钮。
五、配置计算集群
为训练任务提供弹性处理能力 虽然你已有一个计算实例来执行笔记本操作但下一步你需要设置一个计算集群专门用于处理训练任务。不同于计算实例的单节点计算集群能够支持单节点或多节点的 Linux 或 Windows 操作系统甚至是特定的计算配置如 Spark。
此处你应当预先设置一个 Linux 计算集群。关于虚拟机的规格和价格你可以查阅相关资料。
对于本例子你只需简单的集群配置选择 Standard_DS3_v2拥有 2 个 vCPU 核心和 7 GB 的 RAM。
from azure.ai.ml.entities import AmlCompute# Name assigned to the compute cluster
cpu_compute_target cpu-clustertry:# lets see if the compute target already existscpu_cluster ml_client.compute.get(cpu_compute_target)print(fYou already have a cluster named {cpu_compute_target}, well reuse it as is.)except Exception:print(Creating a new cpu compute target...)# Lets create the Azure Machine Learning compute object with the intended parameters# if you run into an out of quota error, change the size to a comparable VM that is available.\# Learn more on https://azure.microsoft.com/en-us/pricing/details/machine-learning/.cpu_cluster AmlCompute(namecpu_compute_target,# Azure Machine Learning Compute is the on-demand VM servicetypeamlcompute,# VM FamilysizeSTANDARD_DS3_V2,# Minimum running nodes when there is no job runningmin_instances0,# Nodes in clustermax_instances4,# How many seconds will the node running after the job terminationidle_time_before_scale_down180,# Dedicated or LowPriority. The latter is cheaper but there is a chance of job terminationtierDedicated,)print(fAMLCompute with name {cpu_cluster.name} will be created, with compute size {cpu_cluster.size})# Now, we pass the object to MLClients create_or_update methodcpu_cluster ml_client.compute.begin_create_or_update(cpu_cluster)六、命令设置
既然我们已有了执行任务的脚本和对应的计算集群接下来你将设置一系列的命令行操作这些操作或直接调用系统命令或执行特定脚本。
在这一部分你需要定义输入变量比如输入数据、数据拆分比例、学习率以及模型的注册名。你的命令脚本将做以下事情
利用计算集群执行命令。 使用 Azure 机器学习提供的预设环境来运行训练脚本这些环境内包含了训练脚本所需的软件和运行时库。后续在其他教程中你将了解如何自定义这些环境。 设定命令行操作例如 python main.py。你可以使用 ${{ … }} 这样的语法在命令中传递输入/输出参数。 在这一示例中我们将直接从互联网获取数据。
from azure.ai.ml import command
from azure.ai.ml import Inputregistered_model_name credit_defaults_modeljob command(inputsdict(dataInput(typeuri_file,pathhttps://azuremlexamples.blob.core.windows.net/datasets/credit_card/default_of_credit_card_clients.csv,),test_train_ratio0.2,learning_rate0.25,registered_model_nameregistered_model_name,),code./src/, # location of source codecommandpython main.py --data ${{inputs.data}} --test_train_ratio ${{inputs.test_train_ratio}} --learning_rate ${{inputs.learning_rate}} --registered_model_name ${{inputs.registered_model_name}},environmentAzureML-sklearn-1.0-ubuntu20.04-py38-cpulatest,computecpu-cluster, #delete this line to use serverless computedisplay_namecredit_default_prediction,
)七、任务提交
现在你可以在 Azure 机器学习平台上提交一个作业了。这次你需要对 ml_client 使用 create_or_update 功能。
ml_client.create_or_update(job)八、查看任务结果并等待完成
你可以通过点击前一个代码单元的输出链接在 Azure 机器学习工作室里查看任务的进展。
任务的各类输出比如指标、结果等都可以在 Azure 机器学习工作室里查看。当任务完成后其训练出的模型会被注册到你的工作区。
九、部署模型为在线服务
是时候将你的机器学习模型作为一个 Web 服务部署到 Azure 云上了。 为了部署这个服务你应当使用已经注册过的机器学习模型。持有一个已经注册过的模型接下来你可以着手搭建一个在线端点。需要确保你为端点选择的名称在整个Azure地区是独一无二的。为了确保名字的唯一性在这个教程里我们建议采用UUID作为端点名称。
import uuid# Creating a unique name for the endpoint
online_endpoint_name credit-endpoint- str(uuid.uuid4())[:8]
pythonpython
# Expect the endpoint creation to take a few minutes
from azure.ai.ml.entities import (ManagedOnlineEndpoint,ManagedOnlineDeployment,Model,Environment,
)# create an online endpoint
endpoint ManagedOnlineEndpoint(nameonline_endpoint_name,descriptionthis is an online endpoint,auth_modekey,tags{training_dataset: credit_defaults,model_type: sklearn.GradientBoostingClassifier,},
)endpoint ml_client.online_endpoints.begin_create_or_update(endpoint).result()print(fEndpoint {endpoint.name} provisioning state: {endpoint.provisioning_state})十、模型部署到终结点
端点构建完毕后你可以采用入口脚本将模型部署到此端点。值得注意的是一个端点可以支持多个部署版本并能够设定特定规则来分流到不同的部署版本。在此我们会为你创建一个部署版本它将处理所有的流入流量。对于部署的命名我们提供了一些建议如“蓝色”、“绿色”和“红色”你可以根据自己的喜好选择。
你还可以浏览Azure机器学习工作室的“模型”页面这有助于你识别已注册模型的最新版本。另外你也可以利用下面的代码来获取最新的版本信息。
# Lets pick the latest version of the model
latest_model_version max([int(m.version) for m in ml_client.models.list(nameregistered_model_name)]
)
print(fLatest model is version {latest_model_version} )# picking the model to deploy. Here we use the latest version of our registered model
model ml_client.models.get(nameregistered_model_name, versionlatest_model_version)# Expect this deployment to take approximately 6 to 8 minutes.
# create an online deployment.
# if you run into an out of quota error, change the instance_type to a comparable VM that is available.\
# Learn more on https://azure.microsoft.com/en-us/pricing/details/machine-learning/.blue_deployment ManagedOnlineDeployment(nameblue,endpoint_nameonline_endpoint_name,modelmodel,instance_typeStandard_DS3_v2,instance_count1,
)blue_deployment ml_client.begin_create_or_update(blue_deployment).result()十一、实例推理测试
完成模型的部署之后你现在可以对它进行推理测试了。
按照评分脚本中run函数的要求制定一个示例请求文件。
deploy_dir ./deploy
os.makedirs(deploy_dir, exist_okTrue)%%writefile {deploy_dir}/sample-request.json
{input_data: {columns: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22],index: [0, 1],data: [[20000,2,2,1,24,2,2,-1,-1,-2,-2,3913,3102,689,0,0,0,0,689,0,0,0,0],[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8]]}
}# test the blue deployment with some sample data
ml_client.online_endpoints.invoke(endpoint_nameonline_endpoint_name,request_file./deploy/sample-request.json,deployment_nameblue,
)十二、节点删除
如果你暂时不需要使用该端点请记得删除以避免不必要的费用。在进行删除之前请确保没有其他部署正在使用这个端点。
ml_client.online_endpoints.begin_delete(nameonline_endpoint_name)十三、停止计算实例
如果你暂时不使用计算实例建议暂停 在工作室左侧导航栏点击“计算”。 选择“计算实例”选项卡。 从列表中选择对应的计算实例。 点击顶部工具栏的“停止”按钮。
十四、资源清理
若你决定不再使用已创建的资源为避免费用请进行清理 在Azure门户里点击左侧的“资源组”。 从列表中找到并选择你所创建的资源组。 点击“删除资源组”在弹出的确认框里输入资源组名称并点击“删除”。 关注TechLead分享AI全维度知识。作者拥有10年互联网服务架构、AI产品研发经验、团队管理经验同济本复旦硕复旦机器人智能实验室成员阿里云认证的资深架构师项目管理专业人士上亿营收AI产品研发负责人。
文章转载自: http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.ckctj.cn.gov.cn.ckctj.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.nqgds.cn.gov.cn.nqgds.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.yyzgl.cn.gov.cn.yyzgl.cn http://www.morning.khxyx.cn.gov.cn.khxyx.cn http://www.morning.kaweilu.com.gov.cn.kaweilu.com http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.nkrmh.cn.gov.cn.nkrmh.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.nnwmd.cn.gov.cn.nnwmd.cn http://www.morning.rhpgk.cn.gov.cn.rhpgk.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.hilmwmu.cn.gov.cn.hilmwmu.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.txfxy.cn.gov.cn.txfxy.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.fqyqm.cn.gov.cn.fqyqm.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.mlyq.cn.gov.cn.mlyq.cn http://www.morning.fksxs.cn.gov.cn.fksxs.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.tdnbw.cn.gov.cn.tdnbw.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.rsnd.cn.gov.cn.rsnd.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn http://www.morning.ydrn.cn.gov.cn.ydrn.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.zknjy.cn.gov.cn.zknjy.cn http://www.morning.dfwkn.cn.gov.cn.dfwkn.cn http://www.morning.wqbzt.cn.gov.cn.wqbzt.cn http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn http://www.morning.ryxyz.cn.gov.cn.ryxyz.cn http://www.morning.lnrhk.cn.gov.cn.lnrhk.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.dxpzt.cn.gov.cn.dxpzt.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.3jiax.cn.gov.cn.3jiax.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.roymf.cn.gov.cn.roymf.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.fygbq.cn.gov.cn.fygbq.cn http://www.morning.yrdn.cn.gov.cn.yrdn.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn http://www.morning.jwqqd.cn.gov.cn.jwqqd.cn http://www.morning.mzpd.cn.gov.cn.mzpd.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn