介绍

一提到机器学习,总是让人望而生畏。幸运的是,Azure正在想方设法让开发人员更容易进入机器学习。ML.NET是Microsoft Research专为.NET开发人员开发的机器学习框架,因此您可以在Visual Studio中完成所有工作。如果你还没有玩过它,我想你会爱上它。当您准备好部署ML.NET算法时,您可以通过Azure Function使用无服务器架构- 而不必担心运行时会把服务器和容器弄得一团糟。

无服务器机器学习

受到Luis Quintanilla的文章启发,有关ML.NETAzure Function,我们尝试把二者结合起来使用。您将在本地使用ML.NET来训练您的机器学习模型。然后,您将创建一个Azure环境,其中包含存储帐户和Azure Function,以托管您的机器学习应用程序。使用您的模型构建应用程序的最后一步将在下一篇文章中介绍。

创建您的模型

对于这个快速项目的ML.NET部分,让我们从ML.NET10分钟入门教程中构建鸢尾花分类模型。作为先决条件,您需要安装Azure CLI 2.0, Azure Function Core Tools和最新版本的.NET Core

打开命令提示符并为ML.NET项目创建一个新文件夹。

> mkdir demo
> cd demo

接下来,创建一个新的解决方案以及一个新的控制台项目并安装ML.NET包。

> dotnet new solution
> dotnet new console -o model
> dotnet sln add model/model.csproj
> cd model
> dotnet add package Microsoft.ML --version 0.4.
> dotnet restore

在模型下创建数据目录。

> mkdir data

打开UCI机器学习库:Iris数据集,将数据复制并粘贴到VS Code或TextEdit或Notepad中,并将其保存为数据目录中的iris-data.txt。现在是时候写一些代码了。在Visual Studio Code中打开项目并创建几个数据结构类:IrisData.csIrisPrediction.cs

using Microsoft.ML.Runtime.Api;

        public class IrisData
{
[Column("")]
public float SepalLength; [Column("")]
public float SepalWidth; [Column("")]
public float PetalLength; [Column("")]
public float PetalWidth; [Column("")]
[ColumnName("Label")]
public string Label;
} public class IrisPrediction
{
[ColumnName("PredictedLabel")]
public string PredictedLabels;
}

添加模型类以执行机器学习训练。

using System.Threading.Tasks;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms; class Model
{
public static async Task<PredictionModel<IrisData, IrisPrediction>> Train(LearningPipeline pipeline, string dataPath, string modelPath)
{
// Load Data
pipeline.Add(new TextLoader(dataPath).CreateFrom<IrisData>(separator: ',')); // Transform Data
// Assign numeric values to text in the "Label" column, because
// only numbers can be processed during model training
pipeline.Add(new Dictionarizer("Label")); // Vectorize Features
pipeline.Add(new ColumnConcatenator("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")); // Add Learner
pipeline.Add(new StochasticDualCoordinateAscentClassifier()); // Convert Label back to text
pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" }); // Train Model
var model = pipeline.Train<IrisData, IrisPrediction>(); // Persist Model
await model.WriteAsync(modelPath); return model;
}
}

将您的逻辑放在Program.cs文件中以运行该过程:

 class Program
{
static void Main(string[] args)
{
string dataPath = "/Users/mbcrump/Documents/demo/model/data/iris-data.txt"; string modelPath = "/Users/mbcrump/Documents/demo/model/model.zip"; var model = Model.Train(new LearningPipeline(), dataPath, modelPath).Result; // Test data for prediction
var prediction = model.Predict(new IrisData()
{
SepalLength = 3.3f,
SepalWidth = 1.6f,
PetalLength = 0.2f,
PetalWidth = 5.1f
}); Console.WriteLine($"Predicted flower type is: {prediction.PredictedLabels}"); }
}

运行模型项目以在根目录中创建新的model.zip文件。以下是我得到的结果。

Michaels-MacBook-Pro:model mbcrump$ dotnet run
Automatically adding a MinMax normalization transform, use 'norm=Warn' or 'norm=No' to turn this behavior off.
Using threads to train.
Automatically choosing a check frequency of .
Auto-tuning parameters: maxIterations = .
Auto-tuning parameters: L2 = 2.668802E-05.
Auto-tuning parameters: L1Threshold (L1/L2) = .
Using best model from iteration .
Not training a calibrator because it is not needed.
Predicted flower type is: Iris-virginica

恭喜!您已经使用ML.NET对机器学习模型进行了培训,对鸢尾花进行了分类。

使用Cloud Shell设置Azure环境

我们将使用Azure Cloud Shell,它使用Azure CLI来设置我们的Azure环境。最简单的方法是登录Azure门户帐户,然后单击下面显示的Cloud Shell图标以打开bash shell或转到shell.azure.com

登录后,在bash shell中为此项目创建一个新资源组(并用您自己的一个替换“mlnetdemo”以及该位置)。

$ az group create --name mlnetdemo --location westus

将存储添加到此资源组。

注意:您必须将以下名称更改为唯一的名称

$ az storage account create --name mlnetdemostorage --location westus --resource-group mlnetdemo --sku Standard_LRS

创建Azure Function并将其配置为使用支持.NET Core的beta运行时。

注意:您必须将以下名称更改为唯一的名称

 $ az functionapp create --name mlnetdemoazfunction1 --storage-account mlnetdemostorage1 --consumption-plan-location westus --resource-group mlnetdemo 

 $ az functionapp config appsettings set --name mlnetdemoazfunction1 --resource-group mlnetdemo --settings FUNCTIONS_EXTENSION_VERSION=beta 

部署您的机器学习模型

要将模型部署到服务器,您需要获取存储帐户的密钥。在bash窗口中使用以下命令来获取它。

$ az storage account keys list --account-name mlnetdemostorage1 --resource-group mlnetdemo 

你会看到以下内容:

[
{
"keyName": "key1",
"permissions": "Full",
"value": "YOURKEY"
},
{
"keyName": "key2",
"permissions": "Full",
"value": "NONEYOBUSINESS"
}
]

使用以下命令基于您的帐户密钥创建一个名为models的新目录,用于放入模型(可以在设置|访问键下的导航窗口中找到)。

$ az storage container create --name models --account-key YOURKEY --account-name mlnetdemostorage1

由于我们使用的是Cloud Shell,因此在此步骤中使用Azure Portal会更容易。如果您愿意,也可以使用Azure CLI。浏览到您的mlnetdemo资源组版本,并深入查看您之前创建的存储资源。以这些blob进行训练前,您会看到models文件夹下新的子目录,在硬盘上找到model.zip上传到这里。

第2部分中,我们将介绍构建由Azure Function托管的应用程序,该应用程序将针对您的鸢尾花图像进行分类。

使用ML.NET和Azure Function进行机器学习 - 第1部分的更多相关文章

  1. 使用ML.NET和Azure Function进行机器学习 - 第2部分

    本文是<使用ML.NET和AzureFunction进行机器学习 - 第1部分>的续篇. 像机器一样识别鸢尾花 回顾第1部分,您将使用Visual Studio创建一个新的Azure Fu ...

  2. 创建Azure Function

    azure function的用途在于运行一些逻辑简单的执行逻辑,比如batch job,定时任务,webhook等等.1. 创建azure function创建完毕后,进入app service,选 ...

  3. 使用Azure Function玩转Serverless

    Serverless&Azure Functions 通过无服务器计算,开发者无需管理基础结构,从而可以更快构建应用程序.通过无服务器应用程序,将由云服务提供商自动预配.缩放和管理运行代码所需 ...

  4. 【Azure Application Insights】在Azure Function中启用Application Insights后,如何配置不输出某些日志到AI 的Trace中

    问题描述 基于.NET Core的Function App如果配置了Application Insights之后,每有一个函数被执行,则在Application Insights中的Logs中的tra ...

  5. 【Azure 应用服务】App Service/Azure Function的出站连接过多而引起了SNAT端口耗尽,导致一些新的请求出现超时错误(Timeout)

    问题描述 当需要在应用中有大量的出站连接时候,就会涉及到SNAT(源地址网络转换)耗尽的问题.而通过Azure App Service/Function的默认监控指标图表中,却没有可以直接查看到SNA ...

  6. 【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题

    一切为了安全,所有的云上资源如支持内网资源访问,则都可以加入虚拟网络 问题描述 使用Azure Function处理Storage Account中Blob 新增,更新,删除等情况.Storage A ...

  7. 【Azure 应用服务】Azure Function App 执行PowerShell指令[Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt]错误

    问题描述 使用PowerShell脚本执行获取Azure订阅列表的指令(Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt).在本地 ...

  8. 【Azure 应用服务】Azure Function App使用SendGrid发送邮件遇见异常消息The operation was canceled,分析源码逐步最终源端

    问题描述 在使用Azure Function App的SendGrid Binging功能,调用SendGrid服务器发送邮件功能时,有时候遇见间歇性,偶发性异常.在重新触发SendGrid部分的Fu ...

  9. 【Azure 应用服务】Azure Function HTTP 触发后, 230秒就超时。而其他方式触发的Function, 执行5分钟后也超时,如何调整超时时间?

    问题描述 Azure Function HTTP 触发后, 230秒就超时,而其他方式触发的Function, 执行5分钟后也超时,如何调整超时时间? 问题分析 查阅官方文档,对函数应用超时持续时间有 ...

随机推荐

  1. 029 Es面试小节

    1.大纲 Es是什么?处理哪种业务逻辑用的多? Es类比数据库是什么? 对于数据库的字段.表等,在es中叫什么? Es的refresh把数据写到哪里? Es的数据如何变成检索和聚合索引的? Es的fl ...

  2. mysql分库分表,做到永不迁移数据和避免热点

    作者:老顾聊技术   搜云库技术团队  来源:https://www.toutiao.com/i6677459303055491597 一.前言 中大型项目中,一旦遇到数据量比较大,小伙伴应该都知道就 ...

  3. Linux学习之shell

    通配符 *:表示从它所在位置开始到某个符合条件的结束符之间的任何字符 ?:表示它所在位置上的任何可能的单个字符 []:表示[]中所包含字符的任何一个 特殊键 ctrl+c  #停止当前程序执行 ctr ...

  4. 2013年省赛H题

    2013年省赛H题你不能每次都快速幂算A^x,优化就是预处理,把10^9预处理成10^5和10^4.想法真的是非常巧妙啊N=100000构造两个数组,f1[N],间隔为Af2[1e4]间隔为A^N,中 ...

  5. 【Java并发编程一】线程安全问题

    1.多线程的实现 多线程有两种实现方式: 1.1.继承Thread类 =>示例:A a=new A(); a.start();   1.2.实现Runnable接口 =>示例:A a=ne ...

  6. ELK:logstash和filebeat6.0及以上版本的配置

    filebeat6.0版本以上没有document_type字段,因此需要另外标记下或者代替document_type字段的功能 案例如下: fielbeat5.5的配置 logstash5.5的配置 ...

  7. [R]R语言的module工程化

    很遗憾,这还是一个挖坑的问题,解决方案并不是很确定. 需求是,大多数的语言都提供import包或module的功能,避免全部代码写到一个文件中,方便管理与维护. 如常用的database模块,每次写R ...

  8. [HEOI/TJOI2016]序列

    Description: 给你一个序列,每个数可能变化为另一个数,每次最多有一个数变化 求最长的子序列,无论如何变化,这个子序列都不下降 Hint: \(n \le 10^5\) Solution: ...

  9. sqlzoo:2

    顯示具有至少2億人口的國家名稱. 2億是200000000,有八個零. SELECT name FROM world 找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值. select n ...

  10. Gedit —— 推荐于NOI系列考试(NOIlinux)的轻量编程环境

    由于Vim,Emacs上手艰难,Guide又特别难用,Anjuta还闪退 故推荐一款轻量化的编程环境:Gedit(文本编辑器) 配置方法: 1:在桌面上新建main.cpp,打开方式选择使用gedit ...