Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

where to get index_to_name.json #1

Open
ppengkkang opened this issue Jan 8, 2021 · 1 comment
Open

where to get index_to_name.json #1

ppengkkang opened this issue Jan 8, 2021 · 1 comment

Comments

@ppengkkang
Copy link

Could you let me know where to get index_to_name.json

@guidel96
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants