Unexpected inference output
I noticed a discrepancy between the probabilities reported in the model card example and the probabilities produced when running the exact same code.
Using the example provided in the model card i otaind a very different and weird classification
{'414 - Economic Orthodoxy': 3.64, '601 - National Way of Life: Positive': 3.22, '702 - Labour Groups: Negative': 3.07, ....}
414 - Economic Orthodoxy
Here is the code I used
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained("manifesto-project/manifestoberta-xlm-roberta-56policy-topics-sentence-2024-1-1")
tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-large")
sentence = "We will restore funding to the Global Environment Facility and the Intergovernmental Panel on Climate Change, to support critical climate science research around the world"
inputs = tokenizer(sentence,
return_tensors="pt",
max_length=200, #we limited the input to 200 tokens during finetuning
padding="max_length",
truncation=True
)
logits = model(**inputs).logits
probabilities = torch.softmax(logits, dim=1).tolist()[0]
probabilities = {model.config.id2label[index]: round(probability * 100, 2) for index, probability in enumerate(probabilities)}
probabilities = dict(sorted(probabilities.items(), key=lambda item: item[1], reverse=True))
print(probabilities)
{'501 - Environmental Protection: Positive': 67.56, '411 - Technology and Infrastructure': 14.03, '107 - Internationalism: Positive': 13.58, '416 - Anti-Growth Economy: Positive': 2.24...
predicted_class = model.config.id2label[logits.argmax().item()]
print(predicted_class)
Yes, there is something overlooked in the example. When you specify like this, it reproduces:
AutoModelForSequenceClassification.from_pretrained("manifesto-project/manifestoberta-xlm-roberta-56policy-topics-sentence-2024-1-1", trust_remote_code=True)
Thank you for pointing that out! We have corrected the code in the model card.
Thank you for the quick response!