Sentence Similarity
sentence-transformers
Safetensors
English
feature-extraction
Generated from Trainer
dataset_size:3012496
loss:MultipleNegativesRankingLoss
Eval Results (legacy)
Instructions to use sentence-transformers-testing/stsb-bert-tiny-lora with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use sentence-transformers-testing/stsb-bert-tiny-lora with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-lora") sentences = [ "how to sign legal documents as power of attorney?", "After the principal's name, write “by” and then sign your own name. Under or after the signature line, indicate your status as POA by including any of the following identifiers: as POA, as Agent, as Attorney in Fact or as Power of Attorney.", "['From the Home screen, swipe left to Apps.', 'Tap Transfer my Data.', 'Tap Menu (...).', 'Tap Export to SD card.']", "Ginger Dank Nugs (Grape) - 350mg. Feast your eyes on these unique and striking gourmet chocolates; Coco Nugs created by Ginger Dank. Crafted to resemble perfect nugs of cannabis, each of the 10 buds contains 35mg of THC. ... This is a perfect product for both cannabis and chocolate lovers, who appreciate a little twist." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [4, 4] - Notebooks
- Google Colab
- Kaggle
Create train_script.py
Browse files- train_script.py +95 -0
train_script.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from datasets import load_dataset, Dataset
|
| 3 |
+
from sentence_transformers import (
|
| 4 |
+
SentenceTransformer,
|
| 5 |
+
SentenceTransformerTrainer,
|
| 6 |
+
SentenceTransformerTrainingArguments,
|
| 7 |
+
SentenceTransformerModelCardData,
|
| 8 |
+
)
|
| 9 |
+
from sentence_transformers.losses import MultipleNegativesRankingLoss
|
| 10 |
+
from sentence_transformers.training_args import BatchSamplers
|
| 11 |
+
from sentence_transformers.evaluation import NanoBEIREvaluator
|
| 12 |
+
from peft import LoraConfig, TaskType
|
| 13 |
+
|
| 14 |
+
logging.basicConfig(
|
| 15 |
+
format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# 1. Load a model to finetune with 2. (Optional) model card data
|
| 19 |
+
model = SentenceTransformer(
|
| 20 |
+
"sentence-transformers-testing/stsb-bert-tiny-safetensors",
|
| 21 |
+
model_card_data=SentenceTransformerModelCardData(
|
| 22 |
+
language="en",
|
| 23 |
+
license="apache-2.0",
|
| 24 |
+
model_name="stsb-bert-tiny adapter finetuned on GooAQ pairs",
|
| 25 |
+
),
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Apply PEFT with PromptTuningConfig
|
| 29 |
+
peft_config = LoraConfig(
|
| 30 |
+
task_type=TaskType.FEATURE_EXTRACTION,
|
| 31 |
+
inference_mode=False,
|
| 32 |
+
r=8,
|
| 33 |
+
lora_alpha=32,
|
| 34 |
+
lora_dropout=0.1,
|
| 35 |
+
)
|
| 36 |
+
model.add_adapter(peft_config, "dense")
|
| 37 |
+
|
| 38 |
+
# 3. Load a dataset to finetune on
|
| 39 |
+
dataset = load_dataset("sentence-transformers/gooaq", split="train")
|
| 40 |
+
dataset_dict = dataset.train_test_split(test_size=10_000, seed=12)
|
| 41 |
+
train_dataset: Dataset = dataset_dict["train"].select(range(1_000_000))
|
| 42 |
+
eval_dataset: Dataset = dataset_dict["test"]
|
| 43 |
+
|
| 44 |
+
# 4. Define a loss function
|
| 45 |
+
loss = MultipleNegativesRankingLoss(model)
|
| 46 |
+
|
| 47 |
+
# 5. (Optional) Specify training arguments
|
| 48 |
+
run_name = "stsb-bert-tiny-base-gooaq-peft"
|
| 49 |
+
args = SentenceTransformerTrainingArguments(
|
| 50 |
+
# Required parameter:
|
| 51 |
+
output_dir=f"models/{run_name}",
|
| 52 |
+
# Optional training parameters:
|
| 53 |
+
num_train_epochs=1,
|
| 54 |
+
per_device_train_batch_size=1024,
|
| 55 |
+
per_device_eval_batch_size=1024,
|
| 56 |
+
learning_rate=2e-5,
|
| 57 |
+
warmup_ratio=0.1,
|
| 58 |
+
fp16=False, # Set to False if you get an error that your GPU can't run on FP16
|
| 59 |
+
bf16=True, # Set to True if you have a GPU that supports BF16
|
| 60 |
+
batch_sampler=BatchSamplers.NO_DUPLICATES, # MultipleNegativesRankingLoss benefits from no duplicate samples in a batch
|
| 61 |
+
# Optional tracking/debugging parameters:
|
| 62 |
+
eval_strategy="steps",
|
| 63 |
+
eval_steps=100,
|
| 64 |
+
save_strategy="steps",
|
| 65 |
+
save_steps=100,
|
| 66 |
+
save_total_limit=2,
|
| 67 |
+
logging_steps=25,
|
| 68 |
+
logging_first_step=True,
|
| 69 |
+
run_name=run_name, # Will be used in W&B if `wandb` is installed
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# 6. (Optional) Create an evaluator & evaluate the base model
|
| 73 |
+
# The full corpus, but only the evaluation queries
|
| 74 |
+
dev_evaluator = NanoBEIREvaluator(batch_size=1024)
|
| 75 |
+
dev_evaluator(model)
|
| 76 |
+
|
| 77 |
+
# 7. Create a trainer & train
|
| 78 |
+
trainer = SentenceTransformerTrainer(
|
| 79 |
+
model=model,
|
| 80 |
+
args=args,
|
| 81 |
+
train_dataset=train_dataset,
|
| 82 |
+
eval_dataset=eval_dataset,
|
| 83 |
+
loss=loss,
|
| 84 |
+
evaluator=dev_evaluator,
|
| 85 |
+
)
|
| 86 |
+
trainer.train()
|
| 87 |
+
|
| 88 |
+
# (Optional) Evaluate the trained model on the evaluator after training
|
| 89 |
+
dev_evaluator(model)
|
| 90 |
+
|
| 91 |
+
# 8. Save the trained model
|
| 92 |
+
model.save_pretrained(f"models/{run_name}/final")
|
| 93 |
+
|
| 94 |
+
# 9. (Optional) Push it to the Hugging Face Hub
|
| 95 |
+
model.push_to_hub("sentence-transformers-testing/stsb-bert-tiny-lora", private=True)
|