Zero shot classification - how to access the individual elements returned from classifier function?

#14
by hsuwill - opened

Question: how to access the "label" list, 'score' list, etc returned from classifier? I was writing a code to step through the list and display which label has the highest associated number. In this case, 'travel" has the highest 0.9938 score.

Background: With the zero-shot classification pipeline
The model can be loaded with the zero-shot-classification pipeline like so:

from transformers import pipeline
classifier = pipeline("zero-shot-classification",
model="facebook/bart-large-mnli")

You can then use this pipeline to classify sequences into any of the class names you specify.

sequence_to_classify = "one day I will see the world"
candidate_labels = ['travel', 'cooking', 'dancing']
classifier(sequence_to_classify, candidate_labels)
#{'labels': ['travel', 'dancing', 'cooking'],

'scores': [0.9938651323318481, 0.0032737774308770895, 0.002861034357920289],

'sequence': 'one day I will see the world'}

@hsuwill
classifier_sequence = []
classifier_labels = []
classifier_scores = []

for i in dataframe['column']):
    if i != '':
        temp = model(i, labels)
        classifier_sequence.append(temp['sequence'])
        classifier_labels.append(temp['labels'])
        classifier_scores.append(temp['scores'])

The 3 lists defined above will sequentially contain your input text, classified labels in descending order and the corresponding scores for each classified label in descending order

Sign up or log in to comment