Skip to main content
Sentiment analysis is an NLP task that determines the emotional polarity of text, usually as:
  • Positive
  • Negative
  • Sometimes Neutral
For example:
In HuggingFace, we can use a pre-trained and fine-tuned Transformer model to predict sentiment without training a model from scratch.

1. NLP Text Preprocessing

Before a machine learning model can process text, the text needs to be converted into numerical representations. Typical preprocessing includes:
  1. Convert text into tokens
  2. Convert tokens into token IDs
  3. Add special tokens
  4. Create attention masks
  5. Pad or truncate sequences
  6. Pass the processed input to the Transformer model
Example:
can be tokenized approximately as:
The tokenizer then converts these tokens into numerical IDs:
The actual IDs depend on the tokenizer being used.

2. HuggingFace Transformers

HuggingFace Transformers provides pre-trained NLP models such as:
  • BERT
  • DistilBERT
  • RoBERTa
  • ALBERT
  • DeBERTa
For sentiment analysis, we can use a model that has already been fine-tuned on a sentiment classification dataset. A common beginner-friendly model is:
It is a DistilBERT model fine-tuned for binary sentiment classification. The model predicts:

3. Text Preprocessing with AutoTokenizer

HuggingFace provides AutoTokenizer to automatically load the correct tokenizer for a model.
Now tokenize a sentence:
You will get information similar to:

input_ids

input_ids are numerical representations of the tokens.

attention_mask

attention_mask tells the model which tokens are actual input and which tokens are padding.

4. Tokenization with Padding and Truncation

For multiple sentences, the sentences can have different lengths. We can use:
Meaning:
Example:

5. Predicting Sentiment

We can load the fine-tuned model using AutoModelForSequenceClassification.
Then pass the tokenized input to the model:
The model produces logits.
Logits are raw scores produced by the classification layer.

6. Convert Logits into Probabilities

We can use Softmax to convert logits into probabilities.
Example:
This means:

7. Get the Predicted Class

We can find the class with the highest probability.
For example:
The mapping is:

8. Complete Sentiment Analysis Code

This is a simple version suitable for a beginner coding exercise.
Example output:

9. Using the HuggingFace Pipeline

HuggingFace also provides a much simpler pipeline() API. Instead of manually performing tokenization, model inference, Softmax, and prediction, we can use:
Example:
For multiple sentences:
Output:

10. Code by using Pipeline

11. How the Prediction Works

The complete flow is:
For example:

12. Installing Required Libraries

Install the required packages with:
Then run:

13. Important Concepts

14. pipeline() vs Manual Approach

Pipeline

Best when:
  • You want simple code
  • You are learning NLP
  • You only need predictions
  • You want to quickly test a model

Manual approach

Best when:
  • You want to understand the internal process
  • You need custom preprocessing
  • You want more control over the model
  • You are building a larger NLP application

15. Key Takeaway

The main idea is: