SageMaker Studio Lab部署指南:将训练好的模型无缝集成到AWS服务

📅 2026/7/5 20:53:23
SageMaker Studio Lab部署指南:将训练好的模型无缝集成到AWS服务
SageMaker Studio Lab部署指南将训练好的模型无缝集成到AWS服务【免费下载链接】studio-lab-examplesExample notebooks for working with SageMaker Studio Lab. Sign up for an account at the link below!项目地址: https://gitcode.com/gh_mirrors/st/studio-lab-examplesSageMaker Studio Lab是一个免费的机器学习开发环境特别适合初学者和开发者快速构建、训练和部署机器学习模型。本文将详细介绍如何将在SageMaker Studio Lab中训练好的模型无缝部署到AWS服务通过Serverless Endpoint实现高效、低成本的模型服务。为什么选择SageMaker Studio Lab进行模型部署SageMaker Studio Lab提供了持续12小时的CPU会话和4小时的GPU会话非常适合机器学习学习者和开发者进行模型实验和原型开发。当需要将模型公开提供服务时AWS SageMaker Serverless Endpoint是理想的选择它可以自动扩展资源按使用付费无需管理服务器完美解决了模型部署的复杂性。图SageMaker Serverless Endpoint架构展示适合不可预测的预测流量支持自动扩展至零实例准备工作环境与工具安装在开始部署之前需要确保你的环境中安装了必要的工具和库。以下是基本的准备步骤安装AWS SDK和CLI工具首先安装boto3AWS SDK for Python和AWS CLI这两个工具将帮助你与AWS服务进行交互pip install boto3 pip install awscli配置AWS凭证配置AWS凭证是连接SageMaker Studio Lab与AWS服务的关键步骤。你需要在IAM控制台中创建用户并获取访问密钥然后通过以下方式配置mkdir ~/.aws创建credentials文件并添加你的AWS访问密钥%%writefile ~/.aws/credentials [default] aws_access_key_id 你的访问密钥 aws_secret_access_key 你的密钥创建config文件设置默认区域%%writefile ~/.aws/config [default] regionus-east-1创建SageMaker执行角色SageMaker执行角色是一个IAM角色用于在AWS硬件上代表你执行操作。你可以通过AWS SageMaker控制台创建此角色并记录其ARNAmazon资源名称。模型选择与准备以Hugging Face模型为例在本指南中我们将使用Hugging Face上的预训练模型作为示例。Hugging Face是一个开源的机器学习平台提供了大量预训练的NLP模型。选择合适的模型我们选择distilbert-base-uncased-finetuned-sst-2-english模型这是一个基于DistilBERT的英文文本分类模型专门用于情感分析任务。你可以在Hugging Face网站上直接测试该模型图Hugging Face模型页面展示包含模型详情和在线测试功能本地测试模型在部署之前建议先在本地测试模型确保其正常工作from transformers import AutoTokenizer, AutoModelForSequenceClassification from transformers import TextClassificationPipeline tokenizer AutoTokenizer.from_pretrained(distilbert-base-uncased-finetuned-sst-2-english) model AutoModelForSequenceClassification.from_pretrained(distilbert-base-uncased-finetuned-sst-2-english) pipe TextClassificationPipeline(modelmodel, tokenizertokenizer, return_all_scoresTrue) pipe(I love Amazon SageMaker Studio Lab!)部署步骤从模型到Serverless Endpoint步骤1连接AWS资源使用boto3创建SageMaker客户端连接到AWS资源import boto3 sm_client boto3.client(sagemaker) response sm_client.list_endpoints()步骤2创建SageMaker模型创建模型是部署的第一步。你需要指定使用的容器镜像对于Hugging Face模型AWS提供了预构建的深度学习容器model_data_url763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-inference:1.9-transformers4.12-cpu-py38-ubuntu20.04 container_config { Image: model_data_url, Mode: SingleModel, Environment: { HF_MODEL_ID: distilbert-base-uncased-finetuned-sst-2-english, HF_TASK : text-classification, SAGEMAKER_REGION : us-east-1 } } response sm_client.create_model( ModelNamemodel_name, PrimaryContainercontainer_config, ExecutionRoleArnrole, EnableNetworkIsolationFalse )步骤3创建Serverless Endpoint配置配置Serverless Endpoint指定内存大小和最大并发数endpoint_config_response sm_client.create_endpoint_config( EndpointConfigNameendpoint_config_name, ProductionVariants[ { ModelName: model_name, VariantName: AllTraffic, ServerlessConfig: { MemorySizeInMB: 4096, MaxConcurrency: 10 } } ] )步骤4创建Endpoint使用上一步创建的配置创建Endpointendpoint_name studio-lab-ep timestamp response sm_client.create_endpoint( EndpointNameendpoint_name, EndpointConfigNameendpoint_config_name )创建完成后你可以在AWS SageMaker控制台的Endpoints页面查看Endpoint状态。步骤5测试EndpointEndpoint创建完成后可以使用SageMaker Runtime客户端进行测试import json runtime boto3.client(sagemaker-runtime) content_type application/json data { inputs: Happy Birthday to you! } response runtime.invoke_endpoint( EndpointNameendpoint_name, ContentTypecontent_type, Bodyjson.dumps(data) ) print(response[Body].read().decode())SageMaker Serverless Endpoints工作原理SageMaker Serverless Endpoints自动管理计算资源根据流量自动扩展无需担心服务器管理。其工作流程如下图SageMaker Serverless Endpoints工作流程展示从请求发送到结果返回的完整过程发送推理请求从客户端应用或其他AWS服务发送推理请求。SageMaker Serverless Inference处理推理请求提供完全托管的无服务器端点。自动资源管理自动配置、扩展和管理计算资源。监控与日志提供托管的日志和监控功能。按使用付费仅为实际使用付费不收取闲置时间费用。返回结果将模型预测结果返回给最终用户。清理资源避免不必要的费用完成测试后及时清理创建的资源避免产生不必要的费用sm_client.delete_endpoint(EndpointNameendpoint_name) sm_client.delete_endpoint_config(EndpointConfigNameendpoint_config_name) sm_client.delete_model(ModelNamemodel_name)总结通过本文的指南你已经了解了如何将SageMaker Studio Lab中训练的模型部署到AWS SageMaker Serverless Endpoint。这种部署方式不仅简单高效还能根据实际需求自动扩展资源大大降低了运维成本。无论你是机器学习初学者还是有经验的开发者SageMaker Studio Lab和AWS Serverless Endpoints的组合都能帮助你快速将模型转化为可用的服务。如果你想深入了解更多细节可以参考项目中的详细示例笔记本Access_AWS_from_Studio_Lab_Deployment.ipynb。开始你的模型部署之旅吧【免费下载链接】studio-lab-examplesExample notebooks for working with SageMaker Studio Lab. Sign up for an account at the link below!项目地址: https://gitcode.com/gh_mirrors/st/studio-lab-examples创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考