| 作者: CSDN 博主 Leona na | |
| --- | --- |

# 基于 Amazon SageMaker 构建细粒度情感分析应用【附部署视频】
> [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 是亚马逊云计算(Amazon Web Service)的一项完全托管的[机器学习](https://aws.amazon.com/cn/machine-learning/?trk=cndc-detail)平台服务,算法工程师和数据科学家可以基于此平台快速构建、训练和部署[机器学习](https://aws.amazon.com/cn/machine-learning/?trk=cndc-detail) (ML) 模型,而无需关注底层资源的管理和运维工作。它作为一个工具集,提供了用于[机器学习](https://aws.amazon.com/cn/machine-learning/?trk=cndc-detail)的端到端的所有组件,包括数据标记、数据处理、算法设计、模型训练、训练调试、超参调优、模型部署、模型监控等,使得[机器学习](https://aws.amazon.com/cn/machine-learning/?trk=cndc-detail)变得更
## 基于 Amazon SageMaker 构建细粒度情感分析应用
基于 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 构建细粒度情感分析应用
## [](https://devpress.csdn.net/aws-c/644646adb4541e244e4f9681.html)一、创建 Sagemaker Notebook 实例
输入名称、选择实例类型、配置磁盘大小,具体如下图

创建角色

创建实例

打开 jupyter

打开 terminal 终端

## 二、下载 GAS-SageMaker 数据
```
cd ~/SageMaker
git clone https://github.com/HaoranLv/GAS-SageMaker.git
```
在 Jupyter Notebook 中打开 gabsa.ipynb 逐行运行。

注意事项参考这位博主: [th0ma](https://blog.csdn.net/weixin\\_41466565?type=blog\\&login=from_csdn?trk=cndc-detail)\
博主原文链接在这里:[亚马逊云科技【云上探索实验室】使用 Amazon SageMaker 构建机器学习应用、构建细粒度情感分析应用、基于 Stable Diffusion 模型,快速搭建你的第一个 AIGC 应用](https://blog.csdn.net/weixin\\_41466565/article/details/129782175?spm=1001.2014.3001.5502\\&login=from_csdn?trk=cndc-detail)\
注意:这里使用我的代码可以正确部署。\
引入依赖并进行权限配置
```
import sagemaker as sage
from time import gmtime, strftime
from sagemaker import get_execution_role
from sagemaker.pytorch import PyTorch
```
执行代码图片补充:

将处理好的数据上传到 S3
```
WORK_DIRECTORY = "./data"
# S3 prefix
prefix = "demo"
sess = sage.Session()
role = sage.get_execution_role()
data_location = sess.upload_data(WORK_DIRECTORY, key_prefix=prefix)
```
执行代码图片补充:

定义超参数,实验使用 Huggingface hub 公开的 T5-base 预训练参数进行初始化\
**这里把 “num_train_epochs”:“30” ,修改为 2 ,加快训练时间防止额外扣费**
```
hyperparameters = {
"task" : "tasd",
"dataset" : "rest15",
"model_name_or_path" : "t5-base",
"paradigm": "extraction",
"eval_batch_size" :"16",
"train_batch_size" :"2",
"learning_rate" :"3e-4",
"num_train_epochs":"2",
"n_gpu": "1"
}
```
执行代码图片补充:

实例化 estimator,由于代码使用 Pytorch 框架,故这里直接使用 SageMaker 预置的 Pytorch 容器
```
entry_point = 'finetune.py'
source_dir = './'
git_config = None
framework_version = '1.7.1'
py_version='py36'
instance_type='ml.p3.2xlarge'
instance_count=1
estimator = PyTorch(
entry_point = entry_point,
source_dir = source_dir,
git_config = git_config,
role = role,
debugger_hook_config=False,
hyperparameters = hyperparameters,
framework_version = framework_version,
py_version = py_version,
instance_type = instance_type,
instance_count = instance_count
)
```
执行代码图片补充:

启动模型训练
```
inputs = {'tasd': data_location+'/tasd/'}
response = estimator.fit(inputs)
```
执行代码图片补充:

可以在 S3 存储桶找到训练的模型,点击复制 S3 URL

**完成训练后,把下面代码中的 s3://sagemaker-ap-southeast-1-116572824542/pytorch-training-2022-05-28-10-05-39-029/output/model.tar.gz 替换为刚刚复制的 S3 中的存储桶地址**
```
import sagemaker
instance_type = 'ml.m5.4xlarge'
role = sagemaker.get_execution_role()
from sagemaker.pytorch.model import PyTorchModel
pytorch_model = PyTorchModel(model_data='s3://sagemaker-ap-southeast-1-116572824542/pytorch-training-2022-05-28-10-05-39-029/output/model.tar.gz',
role=role,
entry_point='inference.py',
source_dir='./',
framework_version='1.7.1',
py_version='py36'
) # TODO set model_server_workers=1 to avoid torchhub bug
```
开始训练
```
predictor = pytorch_model.deploy(instance_type=instance_type, initial_instance_count=1
```
执行代码图片补充:

这时可以查看终端节点-可以看到端点

点击名称,可以看到监控状态
