Get startedGet started for free

Zero-shot classification and QNLI

1. Zero-shot classification and QNLI

Previously, we used pipelines for sentiment analysis, a specific type of text classification. But text classification is much broader! Let's explore zero-shot classification and question natural language inference, two powerful techniques that highlight its versatility.

2. Zero-shot classification

Imagine we want to classify text into categories like "sports", "technology", or "health", but we don't have a model that does that by default. That's where zero-shot classification comes in. Zero-shot classification allows a model to assign text to labels it hasn't seen during its learning phase. It uses a form of natural language prediction. Essentially, it asks: "Does this sentence imply this label?" This approach is especially useful in real-world scenarios like content tagging, customer support, or filtering news articles, any time we want to classify text into flexible, user-defined categories without building a new model.

3. Zero-shot classification pipeline

To apply this technique, we import pipeline from transformers and define a pipeline with the task set to "zero-shot-classification", using a suitable model using the same process we applied for sentiment analysis. We then specify the text to classify and a list of candidate_labels representing the possible categories defined by the user. Passing these to the zero_shot_classifier returns a dictionary containing the input sequence, the candidate labels, and their corresponding confidence scores. As we see, the model is 99.48% confident that the provided text is related to sports.

4. Question natural language inference (QNLI)

Now, let's shift to another powerful use case: Question Natural Language Inference or QNLI. Suppose we have a passage of text and a question. QNLI determines whether the passage contains information that answers the question, typically outputting a score that reflects the strength of that inference. This is especially useful in applications like document search, chatbots, or information retrieval, where quickly identifying relevant information from large volumes of text is essential.

5. QNLI pipeline

To apply this in code, we use a 'text-classification' pipeline with a suitable text classification model that performs QNLI. To use the pipeline, we define a passage, such as a text talking about penguins, and a question asking where penguins live. We pass them to the pipeline in the form of a dictionary where text is the question and text_pair is the passage. However, these keys may vary depending on the model, which is consulting the documentation is a must. The output includes a label and a score indicating how confidently the model believes the passage answers the question; 99% confidence in this case.

6. QNLI pipeline

If we change the question to ask about the capital of Paris and rerun the code, we see that the label stays the same but the score drops, indicating that the passage does not have an answer for the provided question.

7. Let's practice!

Time to test these out in code!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.