Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Fast State-of-the-Art Static Embeddings

License

NotificationsYou must be signed in to change notification settings

MinishLab/model2vec

Repository files navigation

Model2Vec logo

Package versionSupported Python versionsDownloadsCodecovJoin DiscordLicense - MIT

Model2Vec is a technique to turn any sentence transformer into a really small static model, reducing model size by a factor up to 50 and making the models up to 500 times faster, with a small drop in performance. Ourbest model is the most performant static embedding model in the world. See our resultshere, or dive in to see how it works.

Quickstart

Install the lightweight base package with:

pip install model2vec

You can start using Model2Vec by loading one of ourflagship models from the HuggingFace hub. These models are pre-trained and ready to use. The following code snippet shows how to load a model and make embeddings, which you can use for any task, such as text classification, retrieval, clustering, or building a RAG system:

frommodel2vecimportStaticModel# Load a model from the HuggingFace hub (in this case the potion-base-8M model)model=StaticModel.from_pretrained("minishlab/potion-base-8M")# Make embeddingsembeddings=model.encode(["It's dangerous to go alone!","It's a secret to everybody."])# Make sequences of token embeddingstoken_embeddings=model.encode_as_sequence(["It's dangerous to go alone!","It's a secret to everybody."])

Instead of using one of our models, you can also distill your own Model2Vec model from a Sentence Transformer model. First, install thedistillation extras with:

pip install model2vec[distill]

Then, you can distill a model in ~30 seconds on a CPU with the following code snippet:

frommodel2vec.distillimportdistill# Distill a Sentence Transformer model, in this case the BAAI/bge-base-en-v1.5 modelm2v_model=distill(model_name="BAAI/bge-base-en-v1.5",pca_dims=256)# Save the modelm2v_model.save_pretrained("m2v_model")

After distillation, you can also fine-tune your own classification models on top of the distilled model, or on a pre-trained model. First, make sure you install thetraining extras with:

pip install model2vec[training]

Then, you can fine-tune a model as follows:

importnumpyasnpfromdatasetsimportload_datasetfrommodel2vec.trainimportStaticModelForClassification# Initialize a classifier from a pre-trained modelclassifier=StaticModelForClassification.from_pretrained(model_name="minishlab/potion-base-32M")# Load a dataset. Note: both single and multi-label classification datasets are supportedds=load_dataset("setfit/subj")# Train the classifier on text (X) and labels (y)classifier.fit(ds["train"]["text"],ds["train"]["label"])# Evaluate the classifierclassification_report=classifier.evaluate(ds["test"]["text"],ds["test"]["label"])

For advanced usage, please refer to ourusage documentation.

Updates & Announcements

Main Features

  • State-of-the-Art Performance: Model2Vec models outperform any other static embeddings (such as GLoVe and BPEmb) by a large margin, as can be seen in ourresults.
  • Small: Model2Vec reduces the size of a Sentence Transformer model by a factor of up to 50. Ourbest model is just ~30 MB on disk, and our smallest model just ~8 MB (making it the smallest model onMTEB!).
  • Lightweight Dependencies: the base package's only major dependency isnumpy.
  • Lightning-fast Inference: up to 500 times faster on CPU than the original model.
  • Fast, Dataset-free Distillation: distill your own model in 30 seconds on a CPU, without a dataset.
  • Fine-tuning: fine-tune your own classification models on top of Model2Vec models.
  • Integrated in many popular libraries: Model2Vec is integrated direclty into popular libraries such asSentence Transformers andLangChain. For more information, see ourintegrations documentation.
  • Tightly integrated with HuggingFace hub: easily share and load models from the HuggingFace hub, using the familiarfrom_pretrained andpush_to_hub. Our own models can be foundhere.

What is Model2Vec?

Model2vec creates a small, fast, and powerful model that outperforms other static embedding models by a large margin on all tasks we could find, while being much faster to create than traditional static embedding models such as GloVe. Like BPEmb, it can create subword embeddings, but with much better performance. Distillation doesn't needany data, just a vocabulary and a model.

The core idea is to forward pass a vocabulary through a sentence transformer model, creating static embeddings for the indiviudal tokens. After this, there are a number of post-processing steps we do that results in our best models. For a more extensive deepdive, please refer to the following resources:

  • Our initialModel2Vec blog post. Note that, while this post gives a good overview of the core idea, we've made a number of substantial improvements since then.
  • OurTokenlearn blog post. This post describes the Tokenlearn method we used to train ourpotion models.
  • Our officialdocumentation. This document provides a high-level overview of how Model2Vec works.

Documentation

Our official documentation can be foundhere. This includes:

Model List

We provide a number of models that can be used out of the box. These models are available on theHuggingFace hub and can be loaded using thefrom_pretrained method. The models are listed below.

ModelLanguageSentence TransformerParamsTask
potion-base-32MEnglishbge-base-en-v1.532.3MGeneral
potion-base-8MEnglishbge-base-en-v1.57.5MGeneral
potion-base-4MEnglishbge-base-en-v1.53.7MGeneral
potion-base-2MEnglishbge-base-en-v1.51.8MGeneral
potion-retrieval-32MEnglishbge-base-en-v1.532.3MRetrieval
M2V_multilingual_outputMultilingualLaBSE471MGeneral

Results

We have performed extensive experiments to evaluate the performance of Model2Vec models. The results are documented in theresults folder. The results are presented in the following sections:

License

MIT

Citing

If you use Model2Vec in your research, please cite the following:

@software{minishlab2024model2vec,authors ={Stephan Tulkens and Thomas van Dongen},title ={Model2Vec: Fast State-of-the-Art Static Embeddings},year ={2024},url ={https://github.com/MinishLab/model2vec}}

[8]ページ先頭

©2009-2025 Movatter.jp