最小可行 MLE

构建最小的可用于生产的情绪分析模型照片由 Stephen Dawson 在 Unsplash 上拍摄什么是可用于生产的模型?我们听到了很多关于生产化机器学习的消息,但拥有一个可以在实际应用中蓬勃发展的模型到底意味着什么?有很多因素会影响机器学习模型在生产中的有效性。为了本文的目的,我们将重点介绍其中的五个。可重复性监控测试自动化版本控制服务推理构建可用于生产的机器学习模型的最重要部分是能够访问它。为此,我们构建了一个提供情绪分析响应的 fastapi 客户端。我们利用 pydantic 来确保输入和输出的结构。我们使用的模型是 huggingface 的 transformers 库中的基础情绪分析管道,它允许我们使用预先训练的模型开始测试。# 文件名:main.pyfrom fastapi import FastAPIfrom pydantic import BaseModelfrom transformers import pipelineapp = FastAPI()classifier = pipeline("sentiment-analysis")class TextInput(BaseModel): text: strclass SentimentOutput(BaseModel): text: str sentiment: str score: float@app.post("/predict", response_model=SentimentOutput)async def predict_sentiment(input_data: TextInput): result = classifier(input_data.text)[0] return SentimentOutput( text=input_data.text, sentiment=result["label"], score=result["score"] )为了确保我们的工作是可重复的,我们 c

来源:走向数据科学

使用自定义模型

虽然构建和微调模型的过程并不是该项目的目的,但重要的是要了解如何将模型添加到此过程中。

确保我们可以查询我们已经培训的新模型,我们必须更新一些现有文件。例如,在main.py中,我们现在使用./ model的模型并将其加载为预验证的模型。此外,为了进行比较,我们添加了两个端点可以使用,/预测/天真和预测/训练。

main.py ./model /predict/naive 预测/训练

我们还必须更新我们的Dockerfile,以包括我们的模型文件。

#fileName:dockerfile from python:3.9-slimworkdir/appcopy unigess.txt .run pip install -r install -r要求.txtcopy main.py .copy ./model ./modelcmd [“ uvicorn” [“ uvicorn”,uvicorn“,”,“ main:app app”,“ app”,“  -  host”,“  -  host”  -  -host,“ 0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0;

重要的是,如果您使用的是git,请确保将pytorch_model.bin文件添加到git lfs中,以便可以推到github。 Git LFS允许您在非常大的文件上使用版本控件。

pytorch_model.bin git lfs