Class Probabilities as Output
#1
by
Arcticweasel
- opened
Hello everyone,
I wondered if it is possible to receive the probabilities for the different classes as an output when using your package instead of the predicted class with the highest probability?
Thanks in advance for any responses!
Take the original code from https://github.com/oliverguhr/german-sentiment-lib/blob/master/germansentiment/sentimentmodel.py and change the last two lines of the predict_sentiment function to
probs = torch.nn.functional.softmax(logits[0], dim=-1).tolist()
label_ids = torch.argmax(logits[0], axis=1)
return ([self.model.config.id2label[label_id.item()] for label_id in label_ids], probs)
You will get the probabilities for positive, negative and neutral when calling the predict_sentiment function like
preds, probs = model.predict_sentiment(texts)
Thanks @Arcticweasel and @tanjafranziska ! I added an API feature for the class probabilities:
from germansentiment import SentimentModel
model = SentimentModel()
classes, probabilities = model.predict_sentiment(["das ist super"], output_probabilities = True)
print(classes, probabilities)
['positive'] [[['positive', 0.9761366844177246], ['negative', 0.023540444672107697], ['neutral', 0.00032294404809363186]]]
oliverguhr
changed discussion status to
closed