Feature Extraction
sentence-transformers
Safetensors
English
bert
sparse-encoder
sparse
splade
Generated from Trainer
dataset_size:99000
loss:SpladeLoss
Eval Results (legacy)
text-embeddings-inference
Instructions to use sparse-encoder-testing/splade-bert-tiny-nq with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use sparse-encoder-testing/splade-bert-tiny-nq with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("sparse-encoder-testing/splade-bert-tiny-nq") sentences = [ "who are the dancers in the limp bizkit rollin video", "Voting age Before the Second World War, the voting age in almost all countries was 21 years or higher. Czechoslovakia was the first to reduce the voting age to 20 years in 1946, and by 1968 a total of 17 countries had lowered their voting age.[1] Many countries, particularly in Western Europe, reduced their voting ages to 18 years during the 1970s, starting with the United Kingdom (1969),[2] with the United States (26th Amendment) (1971), Canada, West Germany (1972), Australia (1974), France (1974), and others following soon afterwards. By the end of the 20th century, 18 had become by far the most common voting age. However, a few countries maintain a voting age of 20 years or higher. It was argued that young men could be drafted to go to war at 18, and many people felt they should be able to vote at the age of 18.[3]", "Rollin' (Limp Bizkit song) The music video was filmed atop the South Tower of the former World Trade Center in New York City. The introduction features Ben Stiller and Stephen Dorff mistaking Fred Durst for the valet and giving him the keys to their Bentley Azure. Also making a cameo is break dancer Mr. Wiggles. The rest of the video has several cuts to Durst and his bandmates hanging out of the Bentley as they drive about Manhattan. The song Ben Stiller is playing at the beginning is \"My Generation\" from the same album. The video also features scenes of Fred Durst with five girls dancing in a room. The video was filmed around the same time as the film Zoolander, which explains Stiller and Dorff's appearance. Fred Durst has a small cameo in that film.", "Eobard Thawne When Thawne reappears, he murders the revived Johnny Quick,[9] before proceeding to trap Barry and the revived Max Mercury inside the negative Speed Force. Thawne then attempts to kill Wally West's children through their connection to the Speed Force in front of Linda Park-West, only to be stopped by Jay Garrick and Bart Allen. Thawne defeats Jay and prepares to kill Bart, but Barry, Max, Wally, Jesse Quick, and Impulse arrive to prevent the villain from doing so.[8][10] In the ensuing fight, Thawne reveals that he is responsible for every tragedy that has occurred in Barry's life, including the death of his mother. Thawne then decides to destroy everything the Flash holds dear by killing Barry's wife, Iris, before they even met.[10]" ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [4, 4] - Notebooks
- Google Colab
- Kaggle
File size: 3,009 Bytes
7bd9ac6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | from datasets import load_dataset
from sentence_transformers import (
SparseEncoder,
SparseEncoderTrainer,
SparseEncoderTrainingArguments,
SparseEncoderModelCardData,
)
from sentence_transformers.sparse_encoder.losses import SpladeLoss, SparseMultipleNegativesRankingLoss
from sentence_transformers.training_args import BatchSamplers
from sentence_transformers.sparse_encoder.evaluation import SparseNanoBEIREvaluator
from sentence_transformers.sparse_encoder.models import SpladePooling, MLMTransformer
# 1. Load a model to finetune with 2. (Optional) model card data
mlm_transformer = MLMTransformer("prajjwal1/bert-tiny")
splade_pooling = SpladePooling(pooling_strategy="max", word_embedding_dimension=mlm_transformer.get_sentence_embedding_dimension())
model = SparseEncoder(
modules=[mlm_transformer, splade_pooling],
model_card_data=SparseEncoderModelCardData(
language="en",
license="apache-2.0",
model_name="SPLADE BERT-tiny trained on Natural-Questions tuples",
)
)
# 3. Load a dataset to finetune on
full_dataset = load_dataset("sentence-transformers/natural-questions", split="train").select(range(100_000))
dataset_dict = full_dataset.train_test_split(test_size=1_000, seed=12)
train_dataset = dataset_dict["train"]
eval_dataset = dataset_dict["test"]
# 4. Define a loss function
loss = SpladeLoss(
model=model,
loss=SparseMultipleNegativesRankingLoss(model=model),
lambda_query=5e-5,
lambda_corpus=3e-5,
)
# 5. (Optional) Specify training arguments
args = SparseEncoderTrainingArguments(
# Required parameter:
output_dir="models/splade-bert-tiny-nq",
# Optional training parameters:
num_train_epochs=1,
per_device_train_batch_size=64,
per_device_eval_batch_size=64,
learning_rate=2e-5,
warmup_ratio=0.1,
fp16=True, # Set to False if you get an error that your GPU can't run on FP16
bf16=False, # Set to True if you have a GPU that supports BF16
batch_sampler=BatchSamplers.NO_DUPLICATES, # MultipleNegativesRankingLoss benefits from no duplicate samples in a batch
# Optional tracking/debugging parameters:
eval_strategy="steps",
eval_steps=200,
save_strategy="steps",
save_steps=200,
save_total_limit=2,
logging_steps=20,
run_name="splade-bert-tiny-nq", # Will be used in W&B if `wandb` is installed
)
# 6. (Optional) Create an evaluator & evaluate the base model
dev_evaluator = SparseNanoBEIREvaluator(dataset_names=["msmarco", "nfcorpus", "nq"], batch_size=16)
# 7. Create a trainer & train
trainer = SparseEncoderTrainer(
model=model,
args=args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
loss=loss,
evaluator=dev_evaluator,
)
trainer.train()
# 8. Evaluate the model performance again after training
dev_evaluator(model)
# 9. Save the trained model
model.save_pretrained("models/splade-bert-tiny-nq/final")
# 10. (Optional) Push it to the Hugging Face Hub
model.push_to_hub("splade-bert-tiny-nq") |