<jupyter_start><jupyter_text>Zero Shot Learning Using Natural Language InferenceIn this notebook, we will demonstrate **zero-shot** topic classification.  **Zero-Shot Learning (ZSL)** is being able to solve a task despite not having received any training examples of that task.  The `ZeroShotClassifier` class in *ktrain* can be used to perform topic classification with no training examples.  The technique is based on **Natural Language Inference (or NLI)** as described in [this interesting blog post](https://joeddav.github.io/blog/2020/05/29/ZSL.html) by <NAME>. STEP 1: Setup the Zero Shot Classifier and Describe TopicsWe first instantiate the zero-shot-classifier and then describe the topic labels for our classifier with strings.<jupyter_code>from ktrain import text 
zsl = text.ZeroShotClassifier()
topic_strings=['politics', 'elections', 'sports', 'films', 'television']<jupyter_output><empty_output><jupyter_text>STEP 2: PredictThere is no training involved here, as we are using **zero-shot-learning**.  We will simply supply the document that is being classified and the `topic_strings` defined earlier. The `predict` method uses Natural Language Inference (NLI) to infer the topic probabilities.<jupyter_code>doc = 'I am extremely dissatisfied with the President and will definitely vote in 2020.'
zsl.predict(doc, topic_strings=topic_strings, include_labels=True)<jupyter_output><empty_output><jupyter_text>As you can see, our model correctly assigned the highest probabilities to `politics` and `elections`, as the text supplied pertains to both these topics.Let's try some other examples. document about `television`<jupyter_code>doc = 'What is your favorite sitcom of all time?'
zsl.predict(doc, topic_strings=topic_strings, include_labels=True)<jupyter_output><empty_output><jupyter_text>document about both `politics` and `television`<jupyter_code>doc = """
President <NAME>'s senior adviser and son-in-law, <NAME>, praised 
the administration's response to the coronavirus pandemic as a \"great success story\" on Wednesday -- 
less than a day after the number of confirmed coronavirus cases in the United States topped 1 million. 
Kushner painted a rosy picture for \"Fox and Friends\" Wednesday morning, 
saying that \"the federal government rose to the challenge and 
this is a great success story and I think that that's really what needs to be told.\"
"""
zsl.predict(doc, topic_strings=topic_strings, include_labels=True)<jupyter_output><empty_output><jupyter_text>document about `sports`, `television`, and `film`<jupyter_code>doc = "The Last Dance is a 2020 American basketball documentary miniseries co-produced by ESPN Films and Netflix."
zsl.predict(doc, topic_strings=topic_strings, include_labels=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08200867052023121, 'fraction_numerical': 0.006864161849710983, 'mean_word_length': 6.36436170212766, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '358070', 'license': 'Apache-2.0', 'n_tokens_mistral': 795, 'n_tokens_neox': 745, 'n_words': 312, 'path': 'examples/text/zero_shot_learning_with_nli.ipynb', 'repo_name': 'edcog/ktrain', 'size': '2786'}