Exploring ModelScope's Zero-Sample Classification

Introduction: The world is so big, I want to see it. What category is this sentence? A freshman with many years of algorithm work experience will explore ModelScope's zero-shot classification model with you in this article. The article mainly includes experimental environment preparation, model testing, principle analysis and so on.
Before we start exploring zero-shot classification on ModelScope, let's take a look at the prediction results:

Does it even know that I want to travel? Is this sample already trained? Let's try again, do you know how I feel?

Was this also trained? impossible.

Next, let's explore ModelScope's zero-shot classification together.

Experimental environment preparation

According to the introduction of ModelScope, the experimental environment can be divided into two situations. The author recommends the second method here, which can be used by clicking on it, saving the trouble of local installation environment, and directly experience ModelScope.

1: Local environment installation

Please refer to ModelScope environment installation.

2: Notebook

ModelScope directly integrates the online development environment, and users can directly train and call models online.

Open the zero-sample classification model, click "Open in Notebook" in the upper right corner, and select the machine model to enter the online development environment.

Experimental test

After entering the notebook or installing the development environment locally, start our journey of zero-sample classification.

load model

Model loading for zero-shot classification can be done using the following two lines of commands.

After running, ModelScope automatically downloads the specified model to the cache directory. It has to be said here that the model download speed based on Alibaba Cloud's ModelScope is very comfortable.

set label

Different from the general text classification model, the zero-shot classification model can set the corresponding label by itself, and the model will classify according to the given label. Here we first set the text label as follows.


Next, enter the text for classification. The text entered here is of course "The world is so big, I want to see it"!

In the end, we got the following results:

Judging by this return format, labels is the label we set, and scores is the probability distribution of each label. From the returned results, it can be found that the probability of the sample belonging to tourism is much greater than that of other categories.

Multi-label classification

In the above classification, we still have one parameter that is not used, namely multi_label. As can be seen from the introduction of the zero-shot classification model, this parameter is used to set whether the classification task is multi-label classification. Next, let’s also try the multi-label classification task.
First, set the tags as follows:

Next, enter text for classification and set multi_label=True.

In the end we got the following results.

The scores represent the probability of each label, and the predicted results can be considered happy and happy because their probability is greater than 0.5.

Principle analysis

After experiencing the magic of zero-sample classification, let's analyze how it works.
As mentioned in the introduction to the zero-shot classification model, this is a zero-shot classification model based on natural language inference tasks. Therefore, let's first take a look at what is a natural language inference task.
natural language reasoning

Natural language reasoning is mainly to judge the semantic relationship between two sentences (premise and hypothesis, Premise and Hypothesis). Generally, there are three categories (Entailment, Contradiction, Neutral), which can be regarded as a three-classification task. Here are 3 examples to give you a real feel for the natural language inference task.

zero-shot classification

So how to perform zero-shot classification tasks based on natural language inference?

By studying the papers given in the zero-sample classification model of the model, combined with the debugging of the ModelScope source code, we can find that the answer is actually very simple. As long as each label is regarded as a hypothesis in turn, and the input text is regarded as a premise, after the natural language inference task is performed, the prediction results of all labels are processed, and zero-sample classification can be achieved.

Set three labels: home, travel, technology

Enter text: The world is so big, I want to see it

When zero-shot classification is performed on the above input, the following three samples of natural language inference tasks are generated:

1. Premise: The world is so big, I want to see it, assumption: home
2. Premise: The world is so big, I want to see it, assumption: travel
3. Premise: The world is so big, I want to see it, Assumption: Technology

After predicting the natural language inference for the three samples, and finally integrating the prediction results, the zero-sample classification task can be realized. For example, in a multi-classification task, the hypothesis with the highest probability that the "premise sentence" implies the "hypothesis sentence" can be used as the final label.

write at the end

Today, I initially explored the ModelScope open source community, and the overall experience was very smooth. The development method of the integrated notebook is very convenient, which eliminates the trouble of installing the development environment, and is very suitable for deep learning developers to directly study and study various models. In addition, the types of models on ModelScope are very rich, and you can try to learn a lot. After all, CPU machines are now free to use~

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us