Predictions for sequenceclassification task
I am working on SequenceClassification task and dont know how to see the predictionshere is my codefrom transformers import DistilBertTokenizer, TFDistilBertForMaskedLMimport tensorflow as tfimport torchloaded_model = DistilBertForSequenceClassification.from_pretrained(“/content/results/distillbert/model1”,return_dict=True)tokenizer = DistilBertTokenizer.from_pretrained(‘distilbert-base-cased’)inputs = tokenizer(“Hello, this news is Not good for learning”, return_tensors=“pt”)labels = torch.tensor([1]).unsqueeze(0)  # Batch size 1outputs = loaded_model(**inputs, labels=labels)loss = outputs.losslogits = outputs.logits
2020-10-09T15:56:30Z
Checkout thisdocs.First, you won’t need to passlabelsfor predictions and for prediction put the code undertorch.no_grad()to avoid calculating gradients.The returnedlogitshave shape[batch_size, num_classes], you can then applyargmaxto get the index highest scored class.classes = ["NEGATIVE", "POSITIVE"]
enc = tokenizer("positive text", return_tensors="pt")
with torch.no_grad():
  logits = model(**enc, return_dict=True).logits

pred_class_idx = torch.argmax(logits).item()
classes[pred_class_idx]you can also applysoftmaxonlogitsto get the probabilities.and better yet, just usepipelineas shown in the docs above, which does all of this for you.
2020-10-09T16:14:35Z
solved, very usefulthanks