Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/sagemaker/14_train_and_push_to_hub/sagemaker-notebook.ipynb
Views: 2542
HuggingFace Hub meets Amazon SageMaker
Fine-tune a Multi-Class Classification with Trainer
and emotion
dataset and push it to the Hugging Face Hub
Introduction
Welcome to our end-to-end multi-class Text-Classification example. In this demo, we will use the Hugging Faces transformers
and datasets
library together with a custom Amazon sagemaker-sdk extension to fine-tune a pre-trained transformer for multi-class text classification. In particular, the pre-trained model will be fine-tuned using the emotion
dataset. To get started, we need to set up the environment with a few prerequisite steps, for permissions, configurations, and so on.
NOTE: You can run this demo in Sagemaker Studio, your local machine or Sagemaker Notebook Instances
Development Environment and Permissions
Installation
Note: we only install the required libraries from Hugging Face and AWS. You also need PyTorch or Tensorflow, if you haven´t it installed
Permissions
If you are going to use Sagemaker in a local environment. You need access to an IAM Role with the required permissions for Sagemaker. You can find here more about it.
Preprocessing
We are using the datasets
library to download and preprocess the emotion
dataset. After preprocessing, the dataset will be uploaded to our sagemaker_session_bucket
to be used within our training job. The emotion dataset consists of 16000 training examples, 2000 validation examples, and 2000 testing examples.
Tokenization
Uploading data to sagemaker_session_bucket
After we processed the datasets
we are going to use the new FileSystem
integration to upload our dataset to S3.
Creating an Estimator and start a training job
List of supported models: https://huggingface.co/models?library=pytorch,transformers&sort=downloads
setting up push_to_hub
for our model.
The train.py
scripts implements the push_to_hub
using the Trainer
and TrainingArguments
. To push our model to the Hub we need to define the push_to_hub
. hyperparameter and set it to True
and provide out Hugging Face Token. Additionally, we can configure the repository name and saving strategy using the hub_model_id
, hub_strategy
.
You can find documentation to those parameters here.
We are going to provide our HF Token securely with out exposing it to the public using notebook_login
from the huggingface_hub
SDK. But be careful your token will still be visible insight the logs of the training job. If you run huggingface_estimator.fit(...,wait=True)
you will see the token in the logs. A better way of providing your HF_TOKEN
to your training jobs would be using AWS Secret Manager
You can also directly find your token at https://hf.co/settings/token.
Accessing the model on hf.co/models
we can access the model on hf.co/models using the hub_model_id
and our username.
Deploying the model from Hugging Face to a SageMaker Endpoint
To deploy our model to Amazon SageMaker we can create a HuggingFaceModel
and provide the Hub configuration (HF_MODEL_ID
& HF_TASK
) to deploy it. Alternatively, we can use the the hugginface_estimator
to deploy our model from S3 with huggingface_estimator.deploy()
.
Then, we use the returned predictor object to call the endpoint.
Finally, we delete the inference endpoint.