I'm using bert to do sentiment analysis. I previous used cardiffnlp's twitter-roberta-base-sentiment, https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment.

It gives the the usage on its page.

from transformers import AutoModelForSequenceClassification
from transformers import TFAutoModelForSequenceClassification
from transformers import AutoTokenizer
import numpy as np
from scipy.special import softmax
import csv
import urllib.request

# Preprocess text (username and link placeholders)
def preprocess(text):
    new_text = []
    for t in text.split(" "):
        t = '@user' if t.startswith('@') and len(t) > 1 else t
        t = 'http' if t.startswith('http') else t
    return " ".join(new_text)

# Tasks:
# emoji, emotion, hate, irony, offensive, sentiment
# stance/abortion, stance/atheism, stance/climate, stance/feminist, stance/hillary

MODEL = f"cardiffnlp/twitter-roberta-base-{task}"

tokenizer = AutoTokenizer.from_pretrained(MODEL)

# download label mapping
mapping_link = f"https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/{task}/mapping.txt"
with urllib.request.urlopen(mapping_link) as f:
    html = f.read().decode('utf-8').split("\n")
    csvreader = csv.reader(html, delimiter='\t')
labels = [row[1] for row in csvreader if len(row) > 1]

# PT
model = AutoModelForSequenceClassification.from_pretrained(MODEL)

text = "Good night 😊"
text = preprocess(text)
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
scores = output[0][0].detach().numpy()
scores = softmax(scores)

# # TF
# model = TFAutoModelForSequenceClassification.from_pretrained(MODEL)
# model.save_pretrained(MODEL)

# text = "Good night 😊"
# encoded_input = tokenizer(text, return_tensors='tf')
# output = model(encoded_input)
# scores = output[0][0].numpy()
# scores = softmax(scores)

It shows sentiments of all three labels, positive, neutral and negative.

However, I'm now trying to use Finbert from ProsusAI to do sentiment analysis https://huggingface.co/ProsusAI/finbert. It doesn't give me its usage on its page. So I'm following this tutorial https://towardsdatascience.com/effortless-nlp-using-pre-trained-hugging-face-pipelines-with-just-3-lines-of-code-a4788d95754f.

My code is

from transformers import pipeline
classifier = pipeline('sentiment-analysis', model='ProsusAI/finbert')
classifier('Stocks rallied and the British pound gained.')

However, the result is [{'label': 'positive', 'score': 0.8983612656593323}]. It only shows the sentiment of the most likely label's (positive). But I need all three labels' sentiment (positive, neutral and negative). How should I use it?


1 Answer 1


You can get the scores for all labels as follows:

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import scipy

tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
model = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert")

inputs = tokenizer("Stocks rallied and the British pound gained.", return_tensors="pt")
with torch.no_grad():
    logits = model(**inputs).logits
scores = {k: v for k, v in zip(model.config.id2label.values(), scipy.special.softmax(logits.numpy().squeeze()))}
# {'negative': 0.034473564, 'neutral': 0.067165166, 'positive': 0.8983614}
  • $\begingroup$ Thank you very much, I ran your code but got OSError: Can't load tokenizer for 'ProsusAI/finbert'. Make sure that: - 'ProsusAI/finbert' is a correct model identifier listed on 'https://huggingface.co/models' (make sure 'ProsusAI/finbert' is not a path to a local directory with something else, in that case) - or 'ProsusAI/finbert' is the correct path to a directory containing relevant tokenizer files. I looked into this post github.com/ProsusAI/finBERT/issues/42, but I didn't use a proxy and have enough disk space. Is it my own problem here? $\endgroup$
    – user900476
    Jul 6, 2022 at 12:07
  • $\begingroup$ I don't get that issue when running the code on google colab, I'm using version 4.20.1 of the transformers library. You might try changing the cache directory in the from_pretrained method using the cache_dir argument. $\endgroup$
    – Oxbowerce
    Jul 6, 2022 at 12:17
  • $\begingroup$ Thank you very much, problem solved on colab. I don't quite understand model(**inputs).logits now, might need to spend some time to study transformer codes. $\endgroup$
    – user900476
    Jul 6, 2022 at 13:38
  • $\begingroup$ It's simply taken the tokenized input and passing it through the model, and then your taking the logits from the model's output. $\endgroup$
    – Oxbowerce
    Jul 6, 2022 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.