Tips Deploy Machine Learning Models with Amazon SageMaker

duckhiem999

New member
[TIẾNG VIỆT]:
** Cách triển khai các mô hình học máy với Amazon Sagemaker **

Các mô hình học máy (ML) có thể được sử dụng để giải quyết nhiều vấn đề khác nhau, từ dự đoán khách hàng đến phát hiện gian lận.Tuy nhiên, một khi bạn đã đào tạo một mô hình, bạn cần triển khai nó để nó có thể được sử dụng trong sản xuất.Amazon Sagemaker giúp dễ dàng triển khai các mô hình ML đến nhiều môi trường sản xuất khác nhau, bao gồm AWS Lambda, Amazon Container Container (ECS) và Amazon đàn hồi Kubernetes Service (EKS).

Trong bài viết này, chúng tôi sẽ hướng dẫn bạn qua các bước triển khai mô hình học máy với Amazon Sagemaker.Chúng tôi sẽ sử dụng một ví dụ đơn giản về một mô hình dự đoán giá của một ngôi nhà.

** Điều kiện tiên quyết **

Để làm theo với hướng dẫn này, bạn sẽ cần những điều sau:

* Tài khoản AWS
* AWS CLI đã cài đặt và cấu hình
* SDK AWS cho Python (BOTO3) đã được cài đặt

** Bước 1: Huấn luyện mô hình học máy **

Bước đầu tiên là đào tạo một mô hình học máy.Chúng tôi sẽ sử dụng [Thuật toán người học tuyến tính của Amazon Sagemaker] (Linear Learner Algorithm - Amazon SageMaker) để đào tạo một mô hình để dự đoán giá của một ngôi nhà.

Để đào tạo mô hình, chúng tôi sẽ sử dụng [bộ dữ liệu nhà ở California] (https://www.kaggle.com/camnugent/california-lousing-prices).Bộ dữ liệu này chứa thông tin về giá nhà đất ở California, bao gồm giá nhà ở trung bình, số lượng phòng ngủ, số lượng phòng tắm và khu vực của ngôi nhà.

Chúng tôi có thể đào tạo mô hình bằng mã sau:

`` `Python
Nhập boto3
Từ Sagemaker nhập GET_EXECATE_ROLE

vai trò = get_execut_role ()

# Tạo phiên Sagemaker
session = boto3.session (v trên vùng_name = 'us-East-1', vai trò = vai trò)

# Tạo mô hình người học tuyến tính
model = sagemaker.linearlearner (
tính năng_dim = 8,
Dự đoán_type = 'Thủy biến',
vai trò = vai trò,
sagemaker_session = phiên
)

# Đào tạo mô hình
model.fit (
data = sagemaker.inputs.traininginput (
s3_uri = 's3: //sagemaker-sample-data-us-east-1/california-housing/train.csv'
)
)
`` `

Khi mô hình được đào tạo, chúng ta có thể lưu nó vào một kho lưu trữ mô hình.

`` `Python
model.save_model (
model_name = 'my-model',
model_dir = 's3: // my-bucket/my-model'
)
`` `

** Bước 2: Triển khai mô hình cho AWS Lambda **

Chúng tôi có thể triển khai mô hình cho AWS Lambda để nó có thể được sử dụng để đưa ra dự đoán trong thời gian thực.

Để triển khai mô hình, chúng tôi sẽ sử dụng mã sau:

`` `Python
Nhập boto3
từ sagemaker.lambda_transformer nhập khẩu Lambdatransformer

# Tạo một máy biến áp Lambda
Transformer = LambDatransformer (
model_name = 'my-model',
vai trò = vai trò,
sagemaker_session = phiên
)

# Triển khai máy biến áp đến AWS Lambda
Transformer.deploy (
entry_point = 'dự đoán.py',
Handler = 'dự đoán',
memory_size = 128,
Thời gian chờ = 300
)
`` `

Khi mô hình được triển khai, chúng ta có thể đưa ra dự đoán bằng mã sau:

`` `Python
Nhập boto3

# Nhận ARN của chức năng Lambda
Arn = 'Arn: AWS: Lambda: US-East-1: 123456789012: Hàm: My-Model'

# Tạo máy khách cho dịch vụ Lambda
client = boto3.client ('lambda')

# Làm cho một dự đoán
phản hồi = client.invoke (
FunctionName = arn,
POADLOAD = '{"Tính năng": [1000, 3, 2]}'
)

# In dự đoán
in

[ENGLISH]:
**How to Deploy Machine Learning Models with Amazon SageMaker**

Machine learning (ML) models can be used to solve a variety of problems, from predicting customer churn to detecting fraud. However, once you've trained a model, you need to deploy it so that it can be used in production. Amazon SageMaker makes it easy to deploy ML models to a variety of production environments, including AWS Lambda, Amazon Elastic Container Service (ECS), and Amazon Elastic Kubernetes Service (EKS).

In this article, we'll walk you through the steps of deploying a machine learning model with Amazon SageMaker. We'll use a simple example of a model that predicts the price of a house.

**Prerequisites**

To follow along with this tutorial, you'll need the following:

* An AWS account
* The AWS CLI installed and configured
* The AWS SDK for Python (Boto3) installed

**Step 1: Train a Machine Learning Model**

The first step is to train a machine learning model. We'll use the [Amazon SageMaker Linear Learner algorithm](https://docs.aws.amazon.com/sagemaker/latest/dg/linear-learner.html) to train a model to predict the price of a house.

To train the model, we'll use the [California Housing dataset](https://www.kaggle.com/camnugent/california-housing-prices). This dataset contains information on housing prices in California, including the median housing price, the number of bedrooms, the number of bathrooms, and the area of the house.

We can train the model using the following code:

```python
import boto3
from sagemaker import get_execution_role

role = get_execution_role()

# Create a SageMaker session
session = boto3.Session(region_name='us-east-1', role=role)

# Create a linear learner model
model = sagemaker.LinearLearner(
feature_dim=8,
predictor_type='regressor',
role=role,
sagemaker_session=session
)

# Train the model
model.fit(
data=sagemaker.inputs.TrainingInput(
s3_uri='s3://sagemaker-sample-data-us-east-1/california-housing/train.csv'
)
)
```

Once the model is trained, we can save it to a model repository.

```python
model.save_model(
model_name='my-model',
model_dir='s3://my-bucket/my-model'
)
```

**Step 2: Deploy the Model to AWS Lambda**

We can deploy the model to AWS Lambda so that it can be used to make predictions in real time.

To deploy the model, we'll use the following code:

```python
import boto3
from sagemaker.lambda_transformer import LambdaTransformer

# Create a Lambda transformer
transformer = LambdaTransformer(
model_name='my-model',
role=role,
sagemaker_session=session
)

# Deploy the transformer to AWS Lambda
transformer.deploy(
entry_point='predict.py',
handler='predict',
memory_size=128,
timeout=300
)
```

Once the model is deployed, we can make predictions using the following code:

```python
import boto3

# Get the Lambda function's ARN
arn = 'arn:aws:lambda:us-east-1:123456789012:function:my-model'

# Create a client for the Lambda service
client = boto3.client('lambda')

# Make a prediction
response = client.invoke(
FunctionName=arn,
Payload='{"features": [1000, 3, 2]}'
)

# Print the prediction
print
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top