paraphrase Tiny B E R T L6 v2
sentence-transformersIntroduction
The paraphrase-TinyBERT-L6-v2 is a model from the Sentence Transformers library. It is designed to map sentences and paragraphs to a 768-dimensional dense vector space, enabling tasks such as clustering and semantic search.
Architecture
The model architecture employs a Transformer with a maximum sequence length of 128. It includes a Pooling layer configured to use mean pooling for sentence embeddings, leveraging a BERT model for token embedding.
Training
This model follows the Sentence-BERT framework, which uses a Siamese BERT-Network to derive sentence embeddings. It is optimized for sentence similarity tasks.
Guide: Running Locally
Basic Steps
-
Install Sentence Transformers
Ensure that thesentence-transformers
library is installed:pip install -U sentence-transformers
-
Use the Model
Load and use the model for generating sentence embeddings:from sentence_transformers import SentenceTransformer model = SentenceTransformer('sentence-transformers/paraphrase-TinyBERT-L6-v2') sentences = ["This is an example sentence", "Each sentence is converted"] embeddings = model.encode(sentences) print(embeddings)
-
Alternative Using Hugging Face Transformers
You can also use the model without the Sentence Transformers library by manually handling the token embeddings and pooling:from transformers import AutoTokenizer, AutoModel import torch tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/paraphrase-TinyBERT-L6-v2') model = AutoModel.from_pretrained('sentence-transformers/paraphrase-TinyBERT-L6-v2') sentences = ['This is an example sentence', 'Each sentence is converted'] encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt') with torch.no_grad(): model_output = model(**encoded_input) def mean_pooling(model_output, attention_mask): token_embeddings = model_output[0] input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float() return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9) sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask']) print("Sentence embeddings:") print(sentence_embeddings)
Cloud GPUs
For faster processing, consider using cloud GPU services such as AWS, Google Cloud, or Azure.
License
The model is licensed under the Apache-2.0 License.