Transform Logits to probabilities doesn't work

Hello,

I finetuned a BertforSequenceClassification model in order to perform a multiclass classification. However, when my model is finetuned I predict my test dataset with:

preds_output = trainer.predict(data_encoded['test'])

So I am able to get my predicted output with:

preds_output.predictions

This should return 2 arrays. One including the logits and another including the predicted classes. Now I want to get the probabilty the classes are predicted with instead of the logits. When I try to do that with

from torch import nn

probabilities = nn.functional.softmax(preds_output.predictions, dim=-1)
print(probabilities)

I get the error that ‘numpy.ndarray’ object has no attribute ‘softmax’. Any idea what can help? Thank you guys

The predictions are a NumPy array, so you need to convert them to a torch Tensor if you want to use PyTorch functions on them.

1 Like

Thanks for the quick reply. I have built a kind of work around using:

from scipy.special import softmax

probabilities = softmax(preds_output[0], axis=1)

Or am I missing smth here?

The code above can be used and returns the same probabilities like transforming the array to a torch tensor and applying the code from my original post. Just for everyone who ll stumble upon this post…

Yes, that works as well!

1 Like