Notebooks
A
Amazon Web Services
Huggingface Text Classification

Huggingface Text Classification

data-scienceinferencearchivedamazon-sagemaker-examplesreinforcement-learningmachine-learningawsexamplesdeep-learningsagemakerjupyter-notebooktrainingmlopspytorch-sagemaker-huggingface

Hugging Face SageMaker - Text-Classification

Sentiment Analysis with DistilBERT and imdb dataset


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.

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable


Introduction

Welcome to our end-to-end binary Text-Classification example. This demo uses the Hugging Face transformers and datasets library together with a custom Amazon SageMaker SDK extension to fine-tune a pre-trained transformer for binary text classification. The pre-trained model will be fine-tuned using the imdb dataset. The following is a diagram illustrating what we will do

 Architecture

NOTE: You can run this demo in SageMaker Studio, your local machine or SageMaker Notebook Instances using PyTorch 1.13 Python 3.9

Environment and Permissions

[ ]
[ ]

Visualizing our data

We are using the datasets library to download the imdb dataset. The dataset consists of 25,000 highly polar movie reviews for training, and 25,000 for testing. Let's see how our dataset looks like

[ ]
[9]
(Dataset({
,     features: ['text', 'label'],
,     num_rows: 25000
, }),
, Dataset({
,     features: ['text', 'label'],
,     num_rows: 25000
, }))
[59]
{'text': 'It was great to see some of my favorite stars of 30 years ago including John Ritter, Ben Gazarra and Audrey Hepburn. They looked quite wonderful. But that was it. They were not given any characters or good lines to work with. I neither understood or cared what the characters were doing.<br /><br />Some of the smaller female roles were fine, Patty Henson and Colleen Camp were quite competent and confident in their small sidekick parts. They showed some talent and it is sad they didn\'t go on to star in more and better films. Sadly, I didn\'t think Dorothy Stratten got a chance to act in this her only important film role.<br /><br />The film appears to have some fans, and I was very open-minded when I started watching it. I am a big Peter Bogdanovich fan and I enjoyed his last movie, "Cat\'s Meow" and all his early ones from "Targets" to "Nickleodeon". So, it really surprised me that I was barely able to keep awake watching this one.<br /><br />It is ironic that this movie is about a detective agency where the detectives and clients get romantically involved with each other. Five years later, Bogdanovich\'s ex-girlfriend, Cybil Shepherd had a hit television series called "Moonlighting" stealing the story idea from Bogdanovich. Of course, there was a great difference in that the series relied on tons of witty dialogue, while this tries to make do with slapstick and a few screwball lines.<br /><br />Bottom line: It ain\'t no "Paper Moon" and only a very pale version of "What\'s Up, Doc".',
, 'label': 0}

Preprocessing

Before you can train a model on a dataset, it needs to be preprocessed into the expected model input format. Whether your data is text, images, or audio, they need to be converted and assembled into batches of tensors. Text, use a Tokenizer to convert text into a sequence of tokens, create a numerical representation of the tokens, and assemble them into tensors.

Tokenization

[ ]
[ ]
[ ]

Visualizing our processed dataset

Let's load our tokenized dataset and see how it looks

[49]
Dataset({
,    features: ['labels', 'input_ids', 'attention_mask'],
,    num_rows: 25000
,})
[60]
{'labels': tensor(0),
, 'input_ids': tensor([  101,  2009,  2001,  2307,  2000,  2156,  2070,  1997,  2026,  5440,
,          3340,  1997,  2382,  2086,  3283,  2164,  2198, 23168,  1010,  3841,
,         14474, 11335,  1998, 14166, 22004,  1012,  2027,  2246,  3243,  6919,
,          1012,  2021,  2008,  2001,  2009,  1012,  2027,  2020,  2025,  2445,
,          2151,  3494,  2030,  2204,  3210,  2000,  2147,  2007,  1012,  1045,
,          4445,  5319,  2030,  8725,  2054,  1996,  3494,  2020,  2725,  1012,
,          1026,  7987,  1013,  1028,  1026,  7987,  1013,  1028,  2070,  1997,
,          1996,  3760,  2931,  4395,  2020,  2986,  1010, 17798, 27227,  1998,
,         28385,  3409,  2020,  3243, 17824,  1998,  9657,  1999,  2037,  2235,
,         29240,  3033,  1012,  2027,  3662,  2070,  5848,  1998,  2009,  2003,
,          6517,  2027,  2134,  1005,  1056,  2175,  2006,  2000,  2732,  1999,
,          2062,  1998,  2488,  3152,  1012, 13718,  1010,  1045,  2134,  1005,
,          1056,  2228,  9984,  2358,  8609,  6528,  2288,  1037,  3382,  2000,
,          2552,  1999,  2023,  2014,  2069,  2590,  2143,  2535,  1012,  1026,
,          7987,  1013,  1028,  1026,  7987,  1013,  1028,  1996,  2143,  3544,
,          2000,  2031,  2070,  4599,  1010,  1998,  1045,  2001,  2200,  2330,
,          1011, 13128,  2043,  1045,  2318,  3666,  2009,  1012,  1045,  2572,
,          1037,  2502,  2848, 22132,  7847, 12303,  5470,  1998,  1045,  5632,
,          2010,  2197,  3185,  1010,  1000,  4937,  1005,  1055,  2033,  5004,
,          1000,  1998,  2035,  2010,  2220,  3924,  2013,  1000,  7889,  1000,
,          2000,  1000,  4172,  2571, 10244,  2239,  1000,  1012,  2061,  1010,
,          2009,  2428,  4527,  2033,  2008,  1045,  2001,  4510,  2583,  2000,
,          2562,  8300,  3666,  2023,  2028,  1012,  1026,  7987,  1013,  1028,
,          1026,  7987,  1013,  1028,  2009,  2003, 19313,  2008,  2023,  3185,
,          2003,  2055,  1037,  6317,  4034,  2073,  1996, 18145,  1998,  7846,
,          2131,  6298,  3973,  2920,  2007,  2169,  2060,  1012,  2274,  2086,
,          2101,  1010, 22132,  7847, 12303,  1005,  1055,  4654,  1011,  6513,
,          1010, 22330, 14454, 11133,  2018,  1037,  2718,  2547,  2186,  2170,
,          1000, 11986,  2075,  1000, 11065,  1996,  2466,  2801,  2013, 22132,
,          7847, 12303,  1012,  1997,  2607,  1010,  2045,  2001,  1037,  2307,
,          4489,  1999,  2008,  1996,  2186, 13538,  2006,  6197,  1997, 25591,
,          7982,  1010,  2096,  2023,  5363,  2000,  2191,  2079,  2007, 14308,
,         21354,  1998,  1037,  2261, 11224,  7384,  3210,  1012,  1026,  7987,
,          1013,  1028,  1026,  7987,  1013,  1028,  3953,  2240,  1024,  2009,
,          7110,  1005,  1056,  2053,  1000,  3259,  4231,  1000,  1998,  2069,
,          1037,  2200,  5122,  2544,  1997,  1000,  2054,  1005,  1055,  2039,
,          1010,  9986,  1000,  1012,   102,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
,             0,     0]),
, 'attention_mask': tensor([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
,         1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
,         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
,         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
,         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
,         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
,         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
,         0, 0, 0, 0, 0, 0, 0, 0])}

Creating an Estimator and start a training job

[21]
[22]
[ ]

Deploying the endpoint

To deploy our endpoint, we call deploy() on our HuggingFace estimator object, passing in our desired number of instances and instance type.

[ ]

Then, we use the returned predictor object to call the endpoint.

[27]
[{'label': 'LABEL_1', 'score': 0.984623908996582}]
[28]
[{'label': 'LABEL_0', 'score': 0.9781230092048645}]

Finally, we delete the endpoint again.

[ ]

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.

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

 This badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable