Skip to main content

Easy Embedding-Based Zero-Shot Classfication

Project description

Zero Hero: A Simple Zero-Shot Classifier

Zero Hero is a simple zero-shot classifier that is easy to use and works well for a variety of tasks. It is especially useful for classifying text that is unlabeled, and can even be applied to pseudo-label data.

What is a zero-shot classifier?

A zero-shot text classifier is a machine learning model capable of classifying new data into categories or classes not seen during model training. Because these pre-trained models have been trained on such a large corpus of text, the model is able to infer the meaning of novel words and phrases and apply this knowledge to classify new text.

Why use Zero Hero?

There are several reasons why you’d want to use Zero Hero:

  • It’s easy to use. You only need to create a list of the categories you want to classify text for, and then pass this list to the classifier.
  • It’s accurate. Zero Hero has been shown to be accurate on a variety of tasks.
  • It’s fast. Zero Hero is very efficient and can classify text quickly.

How to use Zero Hero

To use Zero Hero, you first need to install the package using pip:

pip install zerohero

Once the package is installed, you can create a classifier using the following code:

from zerohero import make_zero_shot_classifier

# Create a classifier and pass the categories you wish to classify
categories = ['sports', 'politics', 'business', 'entertainment', 'science']
classifier = make_zero_shot_classifier(
    categories=categories,
    model_type="sentence-transformers",
    model_name="paraphrase-albert-small-v2",
)

Note that the categories passed to the classifier need to be semantically meaningful, i.e. categories of 1,2,3,4 would not be appropriate. Once you have created a classifier, you can use it to classify text using the following code:

# Classify a piece of text
text = 'The president gave a speech today about the economy.'
similarities = classifier.classify_text(text)

# Print the category
idx = np.argmax(similarities)
print(categories[idx])

This will print the following output:

business

This output indicates the classifier is most confident that the text pertains to business.

Why use Zero Hero instead of a prompt-based classifier?

There are several reasons why you might want to use Zero Hero instead of a prompt-based classifier:

  • Zero Hero is cheaper. A prompt-based classifier can be very expensive to use, especially if you have a lot of text to classify.
  • Zero Hero is more accurate. Prompt-based classifiers can be less accurate than zero-shot classifiers, especially on tasks that require a deep understanding of the text.
  • Zero Hero is easier to use. Prompt-based classifiers can be more difficult to use than zero-shot classifiers, especially if you are not familiar with machine learning.

The zero-shot classifier offers a cost-effective and efficient alternative to prompt-based classifiers for text categorization tasks. It can effectively classify text into predefined categories without the need for extensive training data by employing text embeddings and cosine similarity..

  • Text embeddings are created via the embedding function, which is used to convert text into a vector of numbers. This vector of numbers represents the meaning of the text. The zero-shot classifier uses the embedding function to compare the meaning of the text to the meaning of the categories.
  • Cosine similarity is a measure of similarity between two vectors. The zero-shot classifier uses cosine similarity to compare the meaning of the text to the meaning of the categories.

This approach significantly reduces architectural overhead compared to prompt-based classifiers that rely on generative models, resulting in a more scalable and resource-efficient solution. For instance, using an embedding/transformer model instead of a generative model can reduce the cost by a factor of ten. Additionally, the zero-shot classifier's ability to utilize pre-computed categories further enhances its performance and efficiency.

Conclusion

Zero Hero is a simple and powerful zero-shot classifier that is easy to use and works well for a variety of tasks. It is a good choice for anyone who needs to classify text that is unlabeled or that has little data.

Development Instructions

Install Dependencies: poetry install --with dev
Configure pre-commit hooks: poetry run pre-commit install
Manually run black: poetry run black zerohero/* tests/*
Manually run pylint: poetry run pylint zerohero/* tests/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zerohero-0.1.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zerohero-0.1.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file zerohero-0.1.2.tar.gz.

File metadata

  • Download URL: zerohero-0.1.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.0 Linux/6.2.0-1019-azure

File hashes

Hashes for zerohero-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7466512aae3659ec851e60fb18de45ac159117c84abb5132bbc564e271183966
MD5 fd0b4d2f1d3c1cfdd0b37690c838bbde
BLAKE2b-256 c04d94c6d0b077cbc4b8108a9a4b0071cc142c1d861e98cd3f5821f967566828

See more details on using hashes here.

File details

Details for the file zerohero-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: zerohero-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.0 Linux/6.2.0-1019-azure

File hashes

Hashes for zerohero-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fc7ebaa4b50934ce2b7d1dab89e113d3b5e14f29fd65017d196064fdd12fccb6
MD5 a4118274b62da9b735d36d8bd8c2ac7e
BLAKE2b-256 8ece1bf18c3e8a3f672ee40157ceec313aca9278d9f2eb333862d8aacde9b410

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page