Amazon JumpStart Zero Shot Text Classification
Introduction to JumpStart - Zero Shot Text classification
This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.
Welcome to Amazon SageMaker JumpStart! You can use JumpStart to solve many Machine Learning tasks through one-click in SageMaker Studio, or through SageMaker JumpStart API. In this demo notebook, we demonstrate how to use the JumpStart API to do zero shot text classification.
In supervised classification, natural language processing (NLP) models can only classify text that belong to classes in the training data. Zero-shot classification is a paradigm where a model can classify new, unseen examples that belong to classes that were not present in the training data. For example, a text classification model that is trained to classify new year resolutions tweets on 2 classes ‘career’ and ‘health’, can be used to classify resolutions to a category ‘finance’ that the model has not been trained on .
In this notebook, you will learn how to deploy the pre-trained model for Zero Shot Text classification, run inference and clean up resources. Furthermore, we benchmark the a zero shot text classification model, BART LARGE MNLI model on New Year's Resolutions dataset.
Note: This notebook was tested on ml.t3.medium instance in Amazon SageMaker Studio with Python 3 (Data Science) kernel and in Amazon SageMaker Notebook instance with conda_python3 kernel.
Note: After you’re done running the notebook, make sure to delete all resources so that all the resources that you created in the process are deleted and your billing is stopped. Code in Clean up the endpoint deletes model and endpoints that are created.
1. Set Up
Before executing the notebook, there are some initial steps required for set up.
Permissions and environment variables
To host on Amazon SageMaker, we need to set up and authenticate the use of AWS services. Here, we use the execution role associated with the current notebook as the AWS account role with SageMaker access.
2. Select a model
You can continue with the default model, or can choose a different model from the dropdown generated upon running the next cell. A complete list of SageMaker pre-trained models can also be accessed at Sagemaker pre-trained Models. Default model is the base BART Large model further trained on the MNLI dataset.
[Optional] Here, we filter-out all the zero shot text classification models and select a model for inference.
Chose a model for Inference
3. Retrieve Artifacts & Deploy an Endpoint
Using SageMaker, we can perform inference on the pre-trained model, even without fine-tuning it first on a new dataset. We start by retrieving the deploy_image_uri, deploy_source_uri, and model_uri for the pre-trained model. To host the pre-trained model, we create an instance of sagemaker.model.Model and deploy it. This may take a few minutes.
4. Query endpoint and parse response
Input to the endpoint is a sequence and a set of candidate labels to chose from. It is in a json format and encoded in utf-8 format. Output of the endpoint is a json with predicted labels and the scores.
Next we write some helper function for querying the endpoint and parsing the endpoint response.
Zero Shot text classification models also support multi-class classification i.e. predict multiple labels for a single input. To predict more than one label, set multi_class to True. By default, this parameter is False.
5. Benchmarking
In this section, we will benchmark the BART LARGE MNLI model on the New Year's Resolutions dataset. We classify each resolution as one of the following categories:
- Health
- Humor
- Personal Growth
- Philanthropy
- Leisure
- Career
- Finance
- Education
- Time Management
5.1. Data download and inspection
Remap provided categories to the newly defined categories.
Drop the family, friends, and relationship categories because the original Family/Friends/Relationship cannot be mapped to a single category
5.2. Dataset Clean up
Before running inference the text is cleaned by removing links, emojis, and media.
5.3. Run inference
Select the number of samples you want to use for inference. The estimated time depends on the instance type you choose. On ml.p3.2xlarge, running inference on 1000 samples take roughly 5 minutes.
5.4. Compute metrics
Next, we compare the predicted label with the ground truth labels.
6. Clean up the endpoint
Notebook CI Test Results
This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.