您现在的位置是:亿华云 > 应用开发
创建一个容器化的机器学习模型
亿华云2025-10-08 21:05:02【应用开发】8人已围观
简介数据科学家在创建机器学习模型后,必须将其部署到生产中。要在不同的基础架构上运行它,使用容器并通过 REST API 公开模型是部署机器学习模型的常用方法。本文演示了如何在Podman容器中使用Conn
数据科学家在创建机器学习模型后,创建必须将其部署到生产中。个容要在不同的器化器学基础架构上运行它,使用容器并通过 REST API 公开模型是习模型部署机器学习模型的常用方法。本文演示了如何在 Podman 容器中使用 Connexion 推出使用 REST API 的创建 TensorFlow 机器学习模型。
准备
首先,个容使用以下命令安装 Podman:
sudo dnf -y install podman接下来,器化器学为容器创建一个新文件夹并切换到该目录。习模型
mkdir deployment_container && cd deployment_containerTensorFlow 模型的创建 REST API
下一步是为机器学习模型创建 REST API。这个 github 仓库包含一个预训练模型,云南idc服务商个容以及能让 REST API 工作的器化器学设置。
使用以下命令在 deployment_container 目录中克隆它:
git clone https://github.com/svenboesiger/titanic_tf_ml_model.git prediction.py 和 ml_model/prediction.py 能进行 Tensorflow 预测,习模型而 20x20x20 神经网络的创建权重位于文件夹 ml_model/ 中。
swagger.yamlswagger.yaml 使用 Swagger规范 定义 Connexion 库的个容 API。此文件包含让你的器化器学服务器提供输入参数验证、输出响应数据验证、URL 端点定义所需的所有信息。
额外地,Connexion 还将给你提供一个简单但有用的单页 Web 应用,源码库它演示了如何使用 Javascript 调用 API 和更新 DOM。
swagger: "2.0"info: description: This is the swagger file that goes with our server code version: "1.0.0" title: Tensorflow Podman Articleconsumes: - "application/json"produces: - "application/json"basePath: "/"paths: /survival_probability: post: operationId: "prediction.post" tags: - "Prediction" summary: "The prediction data structure provided by the server application" description: "Retrieve the chance of surviving the titanic disaster" parameters: - in: body name: passenger required: true schema: $ref: #/definitions/PredictionPost responses: 201: description: Survival probability of an individual Titanic passengerdefinitions: PredictionPost: type: object server.py 和 requirements.txtserver.py 定义了启动 Connexion 服务器的入口点。
import connexionapp = connexion.App(__name__, specification_dir=./)app.add_api(swagger.yaml)if __name__ == __main__: app.run(debug=True)requirements.txt 定义了运行程序所需的 python 包。
connexiontensorflowpandas容器化!
为了让 Podman 构建映像,请在上面的准备步骤中创建的 deployment_container 目录中创建一个名为 Dockerfile 的新文件:
FROM fedora:28# File Author / MaintainerMAINTAINER Sven Boesiger <donotspam@ujelang.com># Update the sourcesRUN dnf -y update --refresh# Install additional dependenciesRUN dnf -y install libstdc++RUN dnf -y autoremove# Copy the application folder inside the containerADD /titanic_tf_ml_model /titanic_tf_ml_model# Get pip to download and install requirements:RUN pip3 install -r /titanic_tf_ml_model/requirements.txt# Expose portsEXPOSE 5000# Set the default directory where CMD will executeWORKDIR /titanic_tf_ml_model# Set the default command to execute# when creating a new containerCMD python3 server.py接下来,使用以下命令构建容器镜像:
podman build -t ml_deployment .运行容器
随着容器镜像的构建和准备就绪,你可以使用以下命令在本地运行它:
podman run -p 5000:5000 ml_deployment在 Web 浏览器中输入 http://0.0.0.0:5000/ui 访问 Swagger/Connexion UI 并测试模型:
当然,你现在也可以在应用中通过 REST API 访问模型。亿华云计算
很赞哦!(5167)
相关文章
- 第三,.cc域名域名也有很多优势资源域名,从整体注册基数也可以由此推断;
- Javascript 正则深入以及十个非常有意思的正则实战
- List.sort 和 Sorted 用哪个比较好?
- 海外域名注册在哪里注册?怎么样选择海外域名注册商?
- 2、定期提交和投标域名注册。例如,益华网络点击“立即预订”后,平台会抢先为客户注册域名。当然,一个域名可能会被多个客户预订,所以出价最高的人中标。
- 单字母.company域名高价成交!你认识.company域名吗?
- 你了解世界上功能最强大的开源数据库吗?
- List.sort 和 Sorted 用哪个比较好?
- 打开https://www.aizhan.com/输入自己想要查询的域名然后按回车键,如果做过网站都会有数据显示出来
- 聚名网告诉你与终端进行域名交易的一些细节