SageMaker is a machine learning service managed by Amazon. It’s basically a service that combines EC2, ECR and S3 all together, allowing you to train complex machine learning models quickly and easily, and then deploy the model into a production-ready hosted environment. It provides many best-in-class built-in algorithms, such as Factorization Machines, XGBoost etc. It also allows you to train models using various machine learning frameworks, such as Apache MXNet, TensorFlow, and Scikit-learn.
A straightforward way to interact with SageMaker is using the notebook Instance. This process is described in detail by Amazon (link). We use SageMaker in a slightly different way. we only want to use SageMaker for the model training part, so that we can train a complex model on a large dataset without worrying about the messy infrastructural details. But the rest of the process (e.g., data preparation, making predictions) run locally. So, in our use case, we want to:
1. Interact with SageMaker jobs from local machine, without using SageMaker notebook Instance. Why? Well, there are a few advantages:
- 启动一个笔记本实例需要几分钟的时间,这很慢。
- 除非您手动停止该实例,否则您将始终为正在运行的实例收费,无论您是否在积极使用它。另一方面,如果你从本地机器提交训练作业,你将只对模型训练部分收费。
- 如果代码放在本地,你可以使用你的IDE进行调试,并使用github进行版本控制。
2.在本地访问训练好的模型,这样我们就可以
- 观察模型的细节,而不是把模型当作一个黑盒子来使用
- 在本地进行预测,并以我们自己的方式使用模型
这篇文章的其余部分将介绍我们如何通过5个步骤实现这一目标。
- 设置你的本地机器,这样你就可以在本地与SageMaker作业互动。
- 准备好你的数据
- 提交培训工作
- 下载经过训练的模型
- 在当地进行预测
在最后,我们还将简要地告诉你如何使用SageMaker的超参数调整器,它可以帮助你调整机器学习模型。
Set up your local machine
为了与SageMaker作业进行编程和本地交互,你需要安装sagemaker Python API,以及AWS SDK for python。你可以通过运行以下程序来安装它们 pip install sagemaker boto3
The easiest way to test if your local environment is ready, is by running through a sample notebook, for example, An Introduction to Factorization Machines with MNIST. Run this sample notebook, and check if you need to install additional packages, or if any AWS credential information is missing.
现在你确定你的本地机器已经正确设置了与SageMaker的交互,那么你就可以带着你自己的数据,使用SageMaker训练一个因数机分类模型,下载模型并进行预测。首先,让我们看看如何准备你的数据进行训练。