在Databricks上介绍端到端机器学习的MLflow

解决数据科学问题不仅仅是建立一个模型。它需要数据清理、探索、建模和调优、生产部署以及管理这些步骤的工作流。在这个简单的例子中,我们将看看如何使用健康数据来预测预期寿命。它将从Apache Spark中的数据工程、数据探索、模型调优和使用hyperopt和MLflow进行日志记录开始。本文将继续举例说明模型注册中心如何管理模型升级,以及如何将MLflow作为作业或仪表板简单地部署到生产环境。

点击这里观看更多Spark + AI课程

免费试用Databricks

视频记录

-[导师]大家好,欢迎来到星火峰会。

我是肖恩·欧文。我是Databricks的首席解决方案架构师,在这里,我和其他几个人一起专注于数据科学和机器学习。所以,每周我们都会与那些试图弄清楚如何大规模进行数据科学的客户交谈,可能会使用一大堆工具,可能包括Spark,也可能包括这个新项目MLflow。这是一个来自Dabob下载地址tabricks的开源项目,但实际上你可以在任何地方使用。今天我想向你们展示MLflow给那些可能还没见过它并且对它的功能很好奇的人。在这个过程中,我也向你们展示了一些Databricks,对于那些还没有看过它的人,但是我真的想和你们谈谈MLflow。它是一种粘合剂,可以帮助将数据科学生命周期的几部分粘合在一起,有时可能有点困难。这包括你如何从模型到跟踪实验以及你如何从这些实验到生产。首先,这是Databricks给那些没见过它的人,这是一个基于网络的环境。当你使用Databricks时,你看到的大部分是这样的。 It’s a notebook like environments. Let me start at the beginning here. And what you don’t have to mess with too much.

从健康数据预测预期寿命

您在这里看不到的是处理资源和集群设置等等。集群可以预配置。它们可以向上旋转,也可以放大或缩小。通常作为一个数据科学家,你会开始选择一个可能已经配置好的集群,可能已经在运行,但如果没有,它会打开。Spark将为您提供,它将在不需要时关闭或缩小规模。所以我们可以开始工作了。在Databricks中,就像在很多环境中一样,我们经常在像这样的笔记本环境中工作,在那里你可以写代码,当然,我们一会儿就会讲到,也可以记录下你在做什么,看看代码的执行和输出。现在,数据科学生命周期确实涉及很多部分,但我喜欢把它分为三类。前期会有一些数据工程。我们必须获得原始数据,有人必须清理它,标准化它的表示,并以一种有组织的方式为其他人准备好,可能是分析师或数据科学家使用,做一些建模。 And then of course, there’s data science. There’s some exploratory data analytics, there’s some modeling of course, and model selection. And that needs some organization too. we don’t just build models. We wanna keep track of them and do that in a principled way. And last but not least, is that hop from a model to production, for some definition of production. It’s often harder than it sounds to know how that’s supposed to look. How do we get this artifact out to something that I can run at scale in a production job? And that’s a big part of what MLflow helps with too, As we’ll see here. Now, we’re gonna do this in two parts. I’m going to talk for maybe 20, 25 minutes here and get through some of the data engineering and the data science aspects. And then we’ll pause and take some questions. And then we’ll come back after a break and cover some more issues. The lead really leading up to model productionization and deployment. Now, in this example, I’m going to pick an interesting problem. It’s not the problem probably your solving, of course, but you can put your problem here in its place. Imagine as I go through this, that we’re solving your data science problem. And I tell you that I think most of what we’re seeing here maps to a lot of jobs, you’re probably trying to create and running today already. But the problem to hand here is the question of life expectancy. What determines life expectancy in say, developed countries over the last couple of decades. We know a lot about these countries, demographics and their health indicators, but we might wonder what is it that really drives this life expectancy. So it’s a predictive analytics problem, but it’s also in another sense of the question here is explaining a model to which is an interesting angle. Now, to do this, we’re going to grab some data from a couple of sources. One is the World Health Organization. The other is the World Bank. And finally another data set concerning drug overdose deaths. So, of course, every process begins with data engineering. Now these data sets are actually fairly small, but nothing about this would change If the data sets were very large or if they weren’t simply a CSV files, as in the case here. So we start here in the world of the data engineer and he might wanna work in languages like Scala, for example, and use Spark directly to express queries and transformations of the data. And that’s fine. Here we can use Scala. We can use Python, R even within one notebook as I’ll do here. But the point is that that the choice of language doesn’t necessarily determine what all these different roles have to have to use. They can make their own choices separately. And the first part here, we load the CSV files. We have to tweak a few things about the input that are a little nonstandard. And we can take, for example, and first look at the data after we’ve loaded it. And it displays in line here as nice table. This is a pretty wide data set. I believe it’s over 1600 features, for countries over the last, I think 10, 20, 30 years, at least here. Now, along the way, we might need to save off some different tables from this data. For example, a lot of the data can has codes for different health indicators in the data and a description. And we’re gonna work in terms of the codes, but we might need these descriptions later for lookup to make our output a little more interpretable. So we’re gonna save those off. And I’m going to save them as a Delta table, as a minor example of what Delta might be able to do for you. But let me come back to this lookup table in just a second, once we’ve loaded more into it.

虽然这段代码不是特别重要,但它只是做了更多的过滤转换。我认为这里的很多元素对于任何编写这样的代码来处理数据的数据工程师来说都是熟悉的。例如,在这里,我们直接使用spark Scala API来做一些过滤,但我们也可以混合使用SQL语法来表示这些过滤器。或者在某些情况下,对于用户定义的函数,将转换表示为语言原生UDF比用SQL表示更容易。这也是可能的。所以我们把所有这些不同的方法混合在一起来进一步处理这些数据。因此,我们可以将这个数据帧注册为一个表,然后切换到SQL来创建。也许我们想看看这些数据是按国家和年份排序的。正如你所看到的,这是一个相当广泛的数据集。我们也可以这样做,但可能会使用一些内置的绘图工具来快速地初步查看数据。 So this is life expectancy as a function of time and also grouped by by country here. And you can immediately see there’s a bit of an outlier here, as one country here that over time has a lower life expectancy. And one that’s even seemingly decreasing unlike other countries in the time period, 2014 to 2016 or so. Now this one happens to be the United States. And so one thing this might’ve caused us to wonder is what’s different about the US, can we figure out what’s different about this country that’s causing life expectancy to be lower?

所以继续,我们可能会再次加载另一个数据集,同样是不同的数据,但相同的想法,国家,年份和指标,在大多数发达国家几十年。

在这里,我要读出一些描述代码,它们描述中的代码并将它们保存为Delta表。注意到,对于Delta表,我可以做一些附加操作,这和其他数据源不同,比如CSV文件或parquet文件。Delta存储格式实际上只是parquet上面的一层表示,但它添加了一些有趣的新属性和属性。首先,我可以高效地进行upstart和追加操作,但我还可以在修改该表时跟踪该表的历史记录,并查看谁在何时修改了它,即使我需要回滚并查询前一个时间点的表。举个例子,当我写这些表格时,它们在这里的亚矿中变得可见。如果需要,我可以通过data选项卡访问它们。

我可以在这里的亚metastore中看到,例如,我创建的这个小查找表的Schema和它的一些值,但我也可以看到这个表的历史。这里,我写了表格,然后更新了它。我还能知道是谁,什么时候干的,等等。这对这张表来说不是很重要,但是,我马上就会说一下这个位在什么地方会变得更重要。记住处理的是主数据集。

我们会像之前一样对这些数据做一些过滤。同样的想法,只是性质不同。看一看数据。现在我们来加载第三个关于药物过量的数据集。

同样的想法。我们将对一些列名进行筛选和标准化,以便更有用一些。

最后,把国家和年份的这三个数据集连接起来,写成一个Delta表,甚至可以切换到SQL将这些注册为metastore中的表供其他人使用。

所以这个输入表将成为我们对数据科学过程的主要输入。这就是Delta可能会派上用场的地方。第一,在Delta表中,是事务性的。因此,如果有人正在运行这个数据工程作业并更新我的表,而我同时正在读取它,没有问题,我不会得到Phantom读取或不一致的状态。但是当然,当我在做数据科学的时候,我建立了一个模型,也许我需要回去弄清楚数据是什么样的。在我建立模型的时候。对于Delta,我可以这样做。我可以返回查询版本或时间戳。

但这里的表示可能对数据科学本身并不那么重要。现在我们要进入数据科学家的世界。所以,她的工作可能是这样开始的。现在,她选择用Python工作,这很好。您仍然可以读取所有这些数据,通过PySpark使用spark来查询和操作它。正如我们将在下面看到的,确实使用了许多不同的Python包,而不仅仅是Spark。所以她可能会读表格,看看这里的一些总结统计数据。我们可以看到,例如,有些特征实际上没有值或者只有很少的值是未知的。所以我们可能需要做一些缺失值的估算。作为一个简单的方法,我将简单地按国家和年份向前填充回填缺失的值。 And because the data set is small, I’m gonna do something maybe kind of too simplistic. I’m gonna pull this data down to the driver as a Panda’s data frame, use pandas to perform that manipulation and then send it right back to the cluster. Now, if the dataset was large this wouldn’t be a great idea, but for smallest datasets, it’s perfectly fine. And if this is the simplest way to get done what you need doing, you can do it. This is just one of a few ways that pandas and pandas syntax is usable and useful. If you know it, when you’re working in PySpark, for example, and I’ll get to a couple others later, but this is one simplistic example here.

接下来是一些EDA或探索性数据分析。所以,也许她想看看这里几个关键特征之间的相关性。我们可以画出这样的一对图。这使用了一个叫seaborn的包,它是建立在地图绘图库上的。这是一个常见的可视化库,它已经安装在这里的机器学习运行时以及大多数流行的数据科学软件包中。如果不是,也没有问题,之后可以很容易地安装或更新。但是对于大多数您可能想要使用的常见包,运行时本身可能已经为您安装好了。通过这个可视化图我们可以看到,这里的GDP和这里的医疗支出之间存在相关性。当然这是有道理的。当然,这些都是相关的。 And we can also see in the last row here, this is opioid deaths versus everything else. And there’s a clear set of outliers here from one country and no surprise. These dots are all United States over the years. So this is further evidence that something’s different about this one country here. And maybe we need to look into it as we build our model and go to explain the relationship between these features and life expectancy.

因此,作为数据科学过程的一部分,有时更多的工程是必要的,因此原始的,或者更确切地说,来自数据工程师的经过某种程度处理的数据,这些数据被过滤和清理,但不会为特定目的编辑或聚合。通常个别的建模工作或分析工作将需要进一步转换它以达到他们的目的,无论是聚合它还是添加更多的功能,或者只是将表示转换为对建模更有用的东西。这里我们用一种简单的热编码。这就是我们需要对这个特定数据集做的所有事情,但你可以想象这可能是更多的东西。然后我们将在这里使用一些SQL从这个特征数据集中创建一个新的Delta表。这将是另一个有趣的交接点,不仅是构建模型的基础,而且是我们以后生产工作的基础,这也需要有特征的数据来得分。所以我们最终也可以将其视为某种生产管道工作。这就是不断地将数据工程团队分配的新数据转换成模型可以理解的形式。

现在让我们进入更有趣的部分,建模。在我们开始之前。我想简单介绍另一种叫做考拉的产品。显然,如果你在Python中工作,你可能对熊猫很熟悉。这是一个非常广泛使用的数据操作工具。当然,您可以将数据作为数据帧来操作。如果你需要把它放大你需要把数据作为数据帧进行大规模操作,当然有spark和PySpark,但不是每个人都用PySpark。也许这很简单。如果能够在pandas中使用一些代码,并让它在Spark上运行,而无需更改它以使用PySpark语法,那就太好了。这就是考拉背后的想法。 It’s a reimplementation of most of the pandas API, but it’s backed by spark. So if you read data from spark via koalas, you get an object here that acts like a pandas data frame. So for example here, we can use Panda style indexing. Just select all the data up to 2016. And this is actually going to be carried out by Spark, even though it looks like we’re using pandas. Now, I don’t do much with koalas here in the simple example here, I’m gonna use it to select the update up to 2014 as the train set and data after 2014 for test. And we could have done this easily in Spark as well, but I wanted to show you that you can also do this with Panda style syntax, even at scale via something like koalas.

但实际上,我们要用熊猫数据帧来处理这些,只是因为这对于小数据集来说会更容易一些来解决这个特殊的建模问题。

好了,再讲一些模型。

现在我们可以花几天时间来讨论你可以做的所有不同的选择,解决这个建模问题的方法。这是一种回归,因为我们试图以一系列特征的函数来预测预期寿命。有很多方法,很多工具可以应用于它。但有一件事我很清楚,这个数据集并没有那么大,如果你的数据集在千兆字节的数量级上,那么在现代机器的内存中仍然可以轻松容纳。所以你不需要直接分配这么大数据集的模型序列。那太好了。这给我们留下了很多选择。例如,我们可以简单地使用XG加速。这是一个流行的梯度增强树包来执行这个回归,它会像你在其他地方使用的任何额外的增强代码一样运行。但我们可能想知道如何让spark参与进来? I mean, we’ve got a cluster, we’ve got a nice environment here that can give us access to more resources. How can we take advantage of that even if we don’t need it directly. Well, let’s see. So this code here is probably the, like the core of any modeling code you’re writing. At some level, all we’re doing is training and XG boost booster. We’re fitting to the training set and evaluating it and reporting the accuracy of that model. Of course, XG boosts like just about any package has a bunch of hyper-parameters to tune. Like what’s the max depth of the tree. What’s the learning rate, what’s the regularization parameter and so on. It’s not obvious how to fit these, of course, or select these. So commonly we’ll run cross validation and run that through a grid search or a random search over these parameters, to try and find some that seemed to work pretty well. Now that’s okay, but I wanna be a little bit smarter about that. I mean, if we’re gonna build, not just one model, but hundreds of models to try out all these possibilities, maybe we wanna be a little smarter. And that’s where a tool like Hyperapt comes in. It’s baked into the runtime, you can use it here. It’s an open source, basie and optimizer. This is gonna help us try and minimize this loss as a function of all these hyper parameters, but it’s gonna do so in a slightly smarter way. So, as it built models and learns about their losses, it’ll figure out which combinations seem to be promising and explore those combinations more in depth and intend to ignore combinations that just don’t seem to be working out very well. So that can save a lot of time, wasted time exploring combinations that just don’t work. But as a bonus, if you use extra boost here, you get really, two bonuses. If you define the search space like this and tell Hyperapt the range of values we’re interested in trying out and turn it loose here, number one, this is integrated with spark. So these modeling jobs, which are in themselves, unrelated, they can run in parallel are running parallel, or some of them are on the Spark cluster rather than serially on one machine. And obviously that has some advantages to speeding up your search. So if we run this code,

Hyperapt将使用一个星火集群来建立模型,不断学习,尝试更多的模型,随着时间的推移,最好的模型的损失显然会越来越少,这很好。但这里还有另一件我认为很有趣的事。如果你在Databricks中运行这个,你会看到通过MLflow,创建的所有模型都在这个笔记本中被追踪,或者说是这个笔记本中的实验。您可以打开运行侧栏来查看这些。但我觉得更有趣的是看到这个跳出来。

我们现在看到的实际上是MLflow跟踪服务器。你可以自己管理。你可以在你的笔记本电脑上运行它,在其他机器上使用它,登录到它。在Databricks中,它是内置的,它是为你运行的,但你也可以在Databricks之外得到这个,没问题。所有这些可能性,都是由Hyperapt自动生成的。对于每一个,我看看我是否点击它显示的超参数以及损失是什么。因此,我从MLflow获得了一些自动日志记录,因为Hyperapt和MLflow本身是集成的。当然,这是我们可以做的一件事,我们可以深入到单个的运行,但是比较一堆运行可能更有趣。如果我选择,我认为这些是,好吧,让我们比较一下这些。

如果把这些都看一遍可能会更有用。所以我们可以看到所有的运行,并将它们的数字并排比较,但通常在平行坐标图中查看更有用。所以这里我可能会,举个例子,选择那些损失最小的,最好的,然后看看它们的超参数是什么样的,也许可以了解一下什么是有效的。所以我可以,你知道,他们可能会进一步推动我的探索和实验。看起来更高的“最小儿童体重”对那些表现良好的模型更有效。

现在,

Hyperlapt现在帮助我们找到超参数的最佳组合。因此,我们可能会继续构建最后一个模型,使用MLflow,使用那些最佳设置,并在这里的所有数据上进行构建。这是如何使用MLflow的另一种方法。因此,这是MLflow的更手动或更直接的使用,这仍然不是很复杂。这是我的建模代码。要使用MLflow,我只需要用MLflow来检测它。例如,我开始运行,这是模型日志记录、工件日志记录的基本单元。在这里做我的工作,然后告诉MLflow我想记录关于这个的什么。例如,我想记录超参数当然,我想记录模型本身。我需要保存它,但你也可以做其他事情。 Like here, I’m using a package called SHAP to create an explanation of the model and why it predicted what it did in certain cases. And having it generate some, a future importance platforming, which I can also log here with the model. So it’s not just models you’re logging. It can be arbitrary artifacts. It can be metrics, parameters. It can be small data files if you want, but that’s less common. Certainly the models themselves. And MLflow has direct support for most common model types or modeling packages. So if we quickly take a look at the docs, you’ll see, there’s direct support for, for example, Keras, TensorFlow, PyTorch, Scikitlearn, of course, XG boost here, SparkMlib of course, and a couple other modeling libraries, including most R functions as well. I should mention that in this example, we’re working in Python, there’re API is Primo flow in Java, the Scala and also R. So if we, when we log this, we get another final run here.

它是这样的。我们可以看到,例如,谁在运行它,我,什么时候,链接到创建它的笔记本的确切修订版。我可以在这里加上注释。我看到了我记录的所有超参数。最重要的是,举个例子,

我在这里记录的模型和它的构件,这是助推器,序列化了。这里有一些关于必要环境的信息,包括它们的流出和额外的推动,在这种情况下,0.9。第二个总结图,我马上会回来给你们看,但是你可以把第二行的图看作是记录在一起的工件。

现在,工作流程的下一步是将这个模型投入生产。这不仅仅是字面上创建生产工作的问题,而是导致该工作的工作流,这将需要查看模型注册表。所以我要暂停一下,结束第一部分,我们暂停一分钟,休息一下。我很乐意回答你们关于我们目前所看到的一些问题。然后,我们将返回并查看如何使用模型注册表跟踪升级过程,然后使用MLflow将生产作业作为批处理作业或REST API,甚至是仪表板。我们马上回来。好的,欢迎回来。让我们回顾一下。所以,我们从一些数据工程开始处理一些关于健康指标和预期寿命的原始数据。我们已经建立了一个初步的模型,我们认为这个模型以一种有意义的方式将这些健康指标与预期寿命联系起来。 Like we could use it to predict life expectancy

未来国家的状态,这很好。所以我们需要把这个模型投入生产。在我们说什么是把东西投入生产之前,让我们先谈谈工作流程。现在我们有,正如你回忆的,我们在MLflow中运行这个我们已经记录了我们建立的模型。太好了。所以我们知道它在哪里,我们可以很容易地通过编程检索它,或者直接从分布式存储中获取它。但是,你知道,把一个模型投入生产不仅仅是把一个文件交给某人,甚至是把这个运行ID交给这里。显然围绕它有更多的工作流程,对吧?我们不会直接把东西投入生产。这就是模型注册表的用武之地。 It’s this models tab here at the left here. And if you open that and search for the, you’ll find among other models, the one I’ve created here for our demo here called life expectancy. So this is like a logical model. So this is a model that might evolve over time. Today, it might be backed by this XG boost model we recreated, maybe in a month. We’ll find a new way to build a better model with TensorFlow. And so there’s going to be versions, different versions of this logical model over time. And the model registry exists to help you track those versions and also track what state they’re in. So are they, is it the production model right now is a testing candidate or staging candidate. And who is allowed to move them through those life cycle stages. So in this instance, so here’s where we are now. So this is the data site, that’s created this model. And maybe she wants to suggest that this could be the new production model, it’s looking good. So in MLflow, you could, for example, use the client to register this run as a new version of this model called life expectancy. And then declare that that version is the new staging candidate. And so after executing that we’d be in this state right here. So we’ve got a current production model at version two. And the candidate we’re looking at now is version three.

那么接下来会发生什么呢?这要看情况。在许多情况下,可能会有一个CICD作业接管,或者运行另一个笔记本,它加载最新的代理候选并在其上运行自动测试。因此,它可能验证了该模型的准确性并不低于当前的生产模型。比如,它在一些黄金布景上表现不错。然后对它进行其他测试所以这可能就是你想做的。然后这个过程就会被批准并投入生产。但我想看看,可能是一个更人工的签字过程。这对于某些工作流组织来说可能是必要的或合适的。 In part, because I wanna to take a little time to actually talk about the problem we were solving and what one actually finds when you explore the model. So here maybe, maybe a data science manager takes over and her job is to sign off this model. So maybe she does go back and look at the details here and verifies that all the metrics look sound, and it looks like all the right things were done. But she might also unpack that run, and go pull out those plot artifacts here. Like that feature explanation I logged with SHAP. So SHAP by the way, stands for Shapley additive explanations. It’s a great set of ideas in a great Python package, which you can use to explain your models. Now it’s not built into the Databricks runtime, but as I say, as with any other package that you can load from PyPI or Maven central, you can simply attach that to a cluster. So in this case, for example, I created the SHAP library and Databricks and just attached it to my cluster so I can use it. It’s not harder than that.

那么SHAP在这里做了什么,这是轴可以创建的一个原理图,这是它的主要特征。那些被认为对模型预测影响最大的因素,也就是预期寿命,但它的影响远不止这些。这里,点是国家和年份。在这种情况下,第一行表示的是这个特征的低值,这是死亡率,例如,癌症,糖尿病。蓝色区域的低值似乎解释了较高的预期寿命,反之亦然,较高的死亡率与较低的预期寿命相伴。这并不是真正的因果断言,尽管SHAP在试图解释相关特征和混杂因素等方面做得很好。至少在方向上有趣的是它似乎很清楚地表明,在任何输入中,这个值对预期寿命的影响最大。所以你可能会认为这是最重要的特征。如果你看一下这个列表,这里有一些有趣的想法,当然也有一些有趣的想法,但没有一个是宏大的。这些都不包括与药物有关的过量,这可能是我们一开始的假设。 That’s interesting.

我将简要地向您展示使用SHAP可以做的其他一些事情。你可以生成一个特征依赖图来解释这个重要特征和对预期寿命的影响之间的关系,还可以解释它和它选择的另一个特征之间的相互作用,也就是年。所以你也可以看到这些影响是如何随着时间变化的。最后,你还可以用它来做一些事情比如看看所有对美国的预测以及所有对非美国国家的数据集的预测看看他们的SHAP值有什么不同。比如他们的解释有什么不同?他们是如何得到不同的预期寿命的呢?这里,似乎有压倒性的证据表明癌症和糖尿病的死亡率。这解释了我们在美国与世界其他地区所看到的大部分差异。

嗯,有意思。你可以想象一下把这个应用到你自己的模型上,但值得注意的是,SHAP不仅可以应用到boost模型上,还可以应用到深度学习模型上,以及任何模型上。但是不管是什么,不管你想做什么分析或者你想做什么CICD作业。这可能是决定投入生产前的一个重要且必要的步骤。但是一旦你准备好投入生产,模型注册表也就回来了。因此,在这种情况下,通过编程,您可以将模型的这个版本转移到生产环境中。没有问题。你也可以通过UI来做。所以我可以用这个版本,也许我有权要求它成为产品版本。我可以添加一个评论,准备好了。

然后可能会有其他人有权力做最后的签字,比如DevOps人员和MLOps人员通过并批准它。在这里我有权力,我是说,管理。一旦我们这样做了,我们就有了第三个版本作为新的生产模型。这本身不会引起任何事情的发生。这仍然只是记账,但这仍然是有用的,因为其他系统需要知道当前的生产模型是什么,然后只需指向模型注册表,查看当前的生产模型是什么,并将其拉出,而不是去弄清楚哪个版本的文件似乎是最新的。所以它帮助组织工作流程,

正确的。最后,我们转向生产。我认为有必要说一下,转向生产总是有点困难,为什么?首先,实验室数据科学工作流程的输出,可以说,它来自于一个非常不同的环境,而不是它将被部署的地方。通常生产可能意味着,我不知道,你可能是Hadoop MapReduce的工作

在过去的几年里,可能是某种SQL语句,它正在运行生产来评分。也可能是基于Java的环境,这与创建模型的环境有很大不同。这是进入生产的最大障碍之一。我的意思是,你经常会发现人们仍然从一些模型中提取一组系数,然后在Java代码中重新实现这个逻辑,逻辑回归模型或其他类似的东西,只是为了将其转移到生产中。很明显这很耗时,也很容易出错。因此,MLflow尝试做的关键事情之一是将您已登录的模型转换或转换为对生产更有用的形式。

现在值得一提的是,您总是可以获得注销的模型。任何时候,我都可以把原始的XG助推器,我造的助推器直接拿来用。但是,也许对我来说,生产意味着批处理工作。我需要采用这个模型,并根据过去一天到达的一堆新数据对其进行大规模评分。现在,显然我可以用Spark来做,但我必须写我自己的UTF,例如,我必须弄清楚如何正确地获得包等等。这似乎是更多的开销。MLflow能做的一件关键的事情就是返回大部分模型。您可以使用MLflow作为spark UTF(用户定义的函数)进行记录。因此,在这段代码中,我们向MLflow请求模型注册中心提供这个已注册模型的最新生产版本,然后请求它加载底层模型,这恰好是一个XG boost助压器作为Spark UTF。这变成了一个我可以用Spark应用的函数。 And so here I go, muster some new data, some new featurized data. Maybe this is more recent data that I wanna to draw predictions for. I’ll tell you that in the original data, there were not at the time actual life expectancy figures after 2016. So, maybe it’s coherent to try to infer those. And then all I have to do is apply that function to the data using Spark here. So here, I’m gonna add a column. This means life expectancy to the data frame, and I’m done. This could be my entire production job.

现在,对于需要较低延迟的用例。你可能想使用Spark流,用Spark加载流数据源,并以这种方式处理它。好消息是这部分看起来几乎是一样的。同样的Spark UTF也可以在Spark流作业中很好地工作。因此,即使您需要更低的延迟,这也可能是您的生产工作的范围。当你将它与投入生产所需的工作和努力相比较时,你会感到非常惊讶。现在,就像我说的,我们可以把这里的预测结果和之前的数据结合起来。在这里,我们只是把它们变成相同的形式,这是预测,但我们也可以再画一次,就像上面那样,根据模型看看2017年和2018年的情况。如你所见,填充得很好。大部分地方都有点平。 And I think I chalked that up to the data being mostly missing. The most of the features here are missing in 2017 and 18. So, the filling in the missing values tends to just kind of make a lot of the predictions look pretty similar. But, maybe that’s not so important. That’s specific to this particular problem. Now, one thing I don’t show here is another option for deployment. And that’s a REST API. So sometimes production means publishing a microservice, something where you can send a request like a REST formatted request and have the model make a prediction and send it back as a response. That’s also something that MLFlow can do for you. Again, for most model types, it can export a Docker container that contains within it, a server that’s serving requests for your model, and that Docker container can be taken and published and run on any infrastructure you like. It could be your own Docker cluster manager or MLFlow also has some direct support for deploying to a Microsoft Azure ML or Amazon SageMaker. Both of these services are built around serving models as Docker containers. And they can provide some additional value, like scaling the serving up and down and providing monitoring and other support like that. So in addition to be able to put out the Docker container, there’s also direct APIs for integrating with things like Azure Mellon SageMaker. I don’t show that here in the interest of time, but what you would get is pretty much what you’d expect. A REST based service, where you send JSON formatted requests with all the column values and you get back a result that has the model’s predictions like a number in this case.

最后一种形式。我想我有时会把制作当成一个仪表盘。所以有时候我们需要提供模型的结果,作为一个可以交互使用的工具。也许我们想把它变成,假设场景仪表板。MLflow也可以做到这一点,我们可以利用Databricks中内置的简单仪表板支持来帮助构建类似的东西。也许我们想要提供一个仪表板或小部件,看起来像这样。这是美国人的预期寿命。某些关键特性的价值与它的实际价值有所不同。在这种情况下,默认情况下,我们会变化。这是癌症和糖尿病的死亡率。 And so maybe you want to see how changing that value does seem to effect predicted life expectancy. Now, of course, we can pair this with MLflow. We can write code to load the model. And then in response to the selections here, go load some data and send it through the model and plot the result. But we’d probably don’t want to share literally this whole large notebook to maybe perhaps a business user who doesn’t want to deal with the code, doesn’t necessarily know how to run it. And so on. Instead, we can export this part, this visualization as a dashboard

用一些简单的小部件来测量,在这种情况下,我们可以改变这个范围。我从0到18开始选择,但也许我可以,显然我想放大这个范围。我把这个改成12,

给它一点时间重新计算。好了。现在这个,模型的预测已经在整个范围内重新计算过了,我可以再一次看到更多的细节。我可以进一步放大

等等......因此,这是一个简单的玩具仪表盘,但您可以想象,这可能会更复杂,这可能会为您提供许多其他可能性,如果生产模型并将其推向生产的目标是为仪表盘或交互式工具之类的东西提供动力。这也是可能的。并不是所有东西都是批处理作业或REST服务。

所以,感谢大家参加我的两部分演练,从数据工程到生产,一直到生产的几个不同的生产定义。为了帮助您,我向您展示了MLflow可以帮助这个过程的一些基本方法。不只是记录他们的模型和组织你的实验,还要管理注册工作流。以一种有原则的方式将模型投入生产。最终为一些生产工作提供了动力。您可能需要指导,无论是以UTF或REST服务的形式获取模型,还是为这样的仪表板提供动力。如果你对此感兴趣,如果你想了解更多关于MLflow的知识,或者有更多的互动研讨会,我建议你看看Databricks的YouTube频道,在那里我们有一个由三部分组成的研讨会;使用MLflow管理完整的机器学习生命周期。我们还会讲到ML流程,可能会用不同的工作例子讲得更深入一些。这也是你可以跟随的。 So with that, thank you again, and I hope you enjoy the rest of the summit, but before I go, I do want to stop again, take a break here with you and pause to answer some of your questions.

点击这里观看更多Spark + AI课程

免费试用Databricks
«回来
关于肖恩·欧文

Sean是Databricks的主要解决方案架构师,专注于机器学习和数据科学。他是Apache Spark的提交者和PMC成员,也是Spark高级分析的合著者。此前,他曾担任Cloudera的数据科学总监和谷歌的工程师。