ProsusAI / finBERT

Financial Sentiment Analysis with BERT
Apache License 2.0
1.45k stars 417 forks source link

predict() in Predict.py function issue "only one element tensors can be converted to Python scalars" #20

Closed hlian188 closed 3 years ago

hlian188 commented 4 years ago

When I call predict, I get the error "only one element tensors can be converted to Python scalars" on line 618 of finbert.py. When I modify the line from: logits = softmax(np.array(logits)) to logits = softmax(np.array(logits[0]))

I get no error, but the predictions and sentiment scores do not seem right when I tested it on examples.csv. The logit looks like [small number, .99..., small number], so the labels are all negative and the scores are all around -.99...

For reference, I copied predict.py, finbert.py, and util.py into a jupyter notebook and used the following as my model model = BertForSequenceClassification.from_pretrained("ipuneetrathore/bert-base-cased-finetuned finBERT",num_labels=3,cache_dir=None)

doguaraci commented 4 years ago

I'm not sure what might be causing the problem. Could you please post the code that is causing the error and print logits right before you get the error? And also the complete error message.

sharebear1993 commented 4 years ago

I also get the same error. Please see the code I used and the logits before the error

image image image

sharebear1993 commented 4 years ago

When I modify the code like hlian1888 (logits = softmax(np.array(logits[0]))), I get these results which are different from the notebook on github

My results image

doguaraci commented 3 years ago

@sharebear1993 sorry for the very late response. This problem should be fixed with the latest commit.