实时推理管道
leap的Python项目详细描述
把数据给我!
欢迎!leapy是一个用于实时、亚毫秒级推理的库; 它为快速 模特服务。 这些管道的目标是使用DASK的可伸缩机器学习, 它遵循scikit学习api。但是,您可以使用此框架 直接使用scikit也可以学习。
pipe=Pipeline([('fp',FeaturePipeline([('ohe',OneHotEncoder(sparse=False),[0,1])])),('clf',LogisticRegression())])pipe.fit(X,y)pipeline_runtime=pipe.to_runtime()# ⚡⚡⚡ init('./model_repo',pipeline_runtime,df.head())# Ready to deploy
为这条超高速管道服务:
$ leap serve --repo ./model_repo
$ curl localhost:8080:/health
{
"status": "healthy"
}
(请参见下面的基准测试和更详细的使用示例。)
好处
dask是python中的一个python分布式计算环境, 新兴的机器学习组件,与Scikit Learn的API兼容。 使用Leapy的框架,我们可以实时为这些管道服务!
这意味着:
- 没有jvm:不依赖jvm使用spark。
- python:所有python开发和自定义转换器——没有scala&java 需要!
- 规模:scikit学习逻辑和管道规模到集群。
- 快速:你可以控制你的变形金刚的速度。
- 简单:使用Docker轻松构建和部署模型。
- 可靠:鼓励测试驱动的方法。
示例
基准
我们要做的一个简单的例子——计算一个热编码,
一个数据点(dask数组x_da
和numpy数组)的大约200k个标签
x = x_da.compute()
):
其中ohe_dml
(来自dask_ml
)和ohe
(来自leapy
)本质上是
相同;ohe_sk
来自scikit-learn
,而ohe_runtime
来自
ohe.to_runtime()
。并且,在bumps之上的dask转换上运行compute()
时间长达1秒左右。
随着使用ohe_runtime
节省的时间,我们可以压缩更多
变换和估计量在1毫秒以下。
示例用法
从dask数组中的数据集开始,X
、y
和dataframeddf
:
管道:
importnumpyasnpimportpandasaspdimportdask.arrayasdaimportdask.dataframeasddX_np=np.array([[1,'a'],[2,'b']],dtype=np.object)df_pd=pd.DataFrame(X_np,columns=['test_int','test_str'])y_np=np.array([0,1])X=da.from_array(X_np,chunks=X_np.shape)y=da.from_array(y_np,chunks=y_np.shape)ddf=dd.from_pandas(df_pd,npartitions=1)
创建我们的管道:
fromsklearn.pipelineimportPipelinefromdask_ml.linear_modelimportLogisticRegressionfromleapy.dask.transformersimportOneHotEncoderfromleapy.dask.pipelineimportFeaturePipelinefromleapy.serveimportinitpipe=Pipeline([('fp',FeaturePipeline([# One-Hot-Encode 'test_str' feature, drop 'test_int'('ohe',OneHotEncoder(sparse=False),[1])])),('clf',LogisticRegression())])pipe.fit(X,y)
然后,我们导出到运行时管道并准备好模型服务:
pipe_runtime=pipe.to_runtime()init('./model_repo',pipe_runtime,ddf.head())
最后我们提供模型:
$ leap serve --repo ./model_repo
$ curl localhost:8080/predict \
-X POST \
-H "Content-Type: application/json" \
--data '{"test_int": 1, "test_str": "b"}'
{
"prediction": 1.0
}
有关模型服务的更多信息,请参见leapy/serve/README.md。
致谢
Leapy的灵感来自MLeap。