You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I'm not sure you need it. You can use the model.config.id2label[<index_of_the_class_you_want>] to map the label/class index with its actual name.
For instance, you do the inference:
with torch.no_grad():
logits_output = predicted_class_id = model(
# take the input, which contains 'input_ids', 'attention_mask', and 'token_type_ids'
input_ids = inputs['input_ids'].to(device),
token_type_ids = inputs['token_type_ids'].to(device)
).logits #we're only interested in the logits, so keep only those
And then you either just extract the predicted class with:
predicted_class_id = logits_output.argmax().item()
model.config.id2label[predicted_class_id] # map the id of the prediction to the corresponding label word
Or you can also output probability of the sentence to be classified in each class:
probabilities = torch.nn.functional.softmax(logits_output, dim=1) # Computing probabilities for each class
proba_per_class = list(zip(
probabilities.flatten().numpy(), # is a 2D tensor that we flatten to a 1D tensor, then convert to numpy
model.config.id2label.values(), # e.g. 'GHG_emissions' # model.config.id2label is a dict with value (class name, 'GHG_emissions')
model.config.id2label.keys() # and key (class number, e.g. 1)
))
print(proba_per_class)
Could you let me know where to get index_to_name.json
The text was updated successfully, but these errors were encountered: