🚀 Flax Sentence Embeddings Model
This project is dedicated to training sentence embedding models on large-scale sentence-level datasets using a self-supervised contrastive learning approach. It offers a powerful tool for sentence encoding, enabling various NLP tasks such as information retrieval, clustering, and sentence similarity analysis.
🚀 Quick Start
Our model is designed to serve as a sentence encoder. When provided with an input sentence, it outputs a vector that encapsulates the semantic information of the sentence. This vector can be utilized for information retrieval, clustering, or sentence similarity tasks.
Here's how to use this model to extract features from a given text using the SentenceTransformers library:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('flax-sentence-embeddings/all_datasets_v3_roberta-large')
text = "Replace me by any text you'd like."
text_embbedding = model.encode(text)
✨ Features
- Powerful Sentence Encoding: Transforms input sentences into vectors that capture semantic information.
- Versatile Applications: Suitable for information retrieval, clustering, and sentence similarity tasks.
- Large-scale Training: Trained on over 1 billion sentence pairs for enhanced performance.
📦 Installation
The installation process mainly involves setting up the SentenceTransformers
library. You can install it using the following command:
pip install sentence-transformers
💻 Usage Examples
Basic Usage
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('flax-sentence-embeddings/all_datasets_v3_roberta-large')
text = "This is a sample sentence."
text_embedding = model.encode(text)
print(text_embedding)
Advanced Usage
from sentence_transformers import SentenceTransformer
import numpy as np
model = SentenceTransformer('flax-sentence-embeddings/all_datasets_v3_roberta-large')
sentences = ["This is the first sentence.", "This is the second sentence."]
embeddings = model.encode(sentences)
cosine_similarity = np.dot(embeddings[0], embeddings[1]) / (np.linalg.norm(embeddings[0]) * np.linalg.norm(embeddings[1]))
print(f"Cosine similarity: {cosine_similarity}")
📚 Documentation
Model description
The project aims to train sentence embedding models on very large sentence level datasets using a self-supervised
contrastive learning objective. We used the pretrained roberta-large
model and fine-tuned it on a
1B sentence pairs dataset. We use a contrastive learning objective: given a sentence from the pair, the model should predict which out of a set of randomly sampled other sentences, was actually paired with it in our dataset.
We developed this model during the
Community week using JAX/Flax for NLP & CV,
organized by Hugging Face. We developed this model as part of the project:
Train the Best Sentence Embedding Model Ever with 1B Training Pairs. We benefited from efficient hardware infrastructure to run the project: 7 TPUs v3-8, as well
as intervention from Google’s Flax, JAX, and Cloud team member about efficient deep learning frameworks.
Intended uses
Our model is intended to be used as a sentence encoder. Given an input sentence, it outputs a vector which captures
the sentence semantic information. The sentence vector may be used for information retrieval, clustering or sentence
similarity tasks.
🔧 Technical Details
Training procedure
Pre-training
We use the pretrained roberta-large
. Please refer to the model
card for more detailed information about the pre-training procedure.
Fine-tuning
We fine-tune the model using a contrastive objective. Formally, we compute the cosine similarity from each possible sentence pairs from the batch.
We then apply the cross entropy loss by comparing with true pairs.
Hyper parameters
We trained our model on a TPU v3-8. We train the model during 540k steps using a batch size of 1024 (128 per TPU core).
We use a learning rate warm up of 500. The sequence length was limited to 128 tokens. We used the AdamW optimizer with
a 2e-5 learning rate. The full training script is accessible in this current repository.
Training data
We use the concatenation from multiple datasets to fine-tune our model. The total number of sentence pairs is above 1 billion sentences.
We sampled each dataset given a weighted probability which configuration is detailed in the data_config.json
file.
Property |
Details |
Model Type |
Sentence embedding model fine-tuned from roberta-large |
Training Data |
Concatenation of multiple datasets with over 1 billion sentence pairs |
Dataset |
Paper |
Number of training tuples |
GOOAQ: Open Question Answering with Diverse Answer Types |
paper |
3,012,496 |
Stack Exchange |
- |
364,001 |
Flickr 30k |
paper |
317,695 |
[COCO 2020](COCO 2020) |
paper |
828,395 |
Code Search |
- |
1,151,414 |
TriviaqQA |
- |
73,346 |
SQuAD2.0 |
paper |
87,599 |
Natural Questions (NQ) |
paper |
100,231 |
Simple Wikipedia |
paper |
102,225 |
Quora Question Pairs |
- |
103,663 |
Altlex |
paper |
112,696 |
Wikihow |
paper |
128,542 |
Sentence Compression |
paper |
180,000 |
AllNLI (SNLI and MultiNLI |
paper SNLI, paper MultiNLI |
277,230 |
Eli5 |
paper |
325,475 |
SPECTER |
paper |
684,100 |
S2ORC Title/Abstract |
paper |
41,769,185 |
S2ORC Citation/Citation |
paper |
52,603,982 |
S2ORC Citation/Abstract |
paper |
116,288,806 |
PAQ |
paper |
64,371,441 |
WikiAnswers |
paper |
77,427,422 |
SearchQA |
- |
582,261 |
Yahoo Answers Title/Answer |
paper |
1,198,260 |
Yahoo Answers Title/Question |
paper |
659,896 |
Yahoo Answers Question/Answer |
paper |
681,164 |
MS MARCO |
paper |
9,144,553 |
Reddit conversationnal |
paper |
726,484,430 |
total |
|
1,097,953,922 |