opensi·si

Finetuning Sparse Encoder models often heavily improves the performance of the model on your use case, because each task requires a different notion of similarity. For example,…

Training Overview

Why Finetune?

Finetuning Sparse Encoder models often heavily improves the performance of the model on your use case, because each task requires a different notion of similarity. For example, given news articles:

  • "Apple launches the new iPad"
  • "NVIDIA is gearing up for the next GPU generation"

Then the following use cases, we may have different notions of similarity:

  • a model for classification of news articles as Economy, Sports, Technology, Politics, etc., should produce similar embeddings for these texts.
  • a model for semantic textual similarity should produce dissimilar embeddings for these texts, as they have different meanings.
  • a model for semantic search would not need a notion for similarity between two documents, as it should only compare queries and documents.

Also see Training Examples for numerous training scripts for common real-world applications that you can adopt.

Training Components

Training Sparse Encoder models involves between 4 to 6 components:

    Model
    Learn how to initialize the model for training.

    Dataset
    Learn how to prepare the data for training.

    Loss Function
    Learn how to prepare and choose a loss function.

    Training Arguments
    Learn which training arguments are useful.

    Evaluator
    Learn how to evaluate during and after training.

    Trainer
    Learn how to start the training process.

Model

Dataset

Dataset Format

Loss Function

Loss functions quantify how well a model performs for a given batch of data, allowing an optimizer to update the model weights to produce more favourable (i.e., lower) loss values. This is the core of the training process.

Sadly, there is no single loss function that works best for all use-cases. Instead, which loss function to use greatly depends on your available data and on your target task. See Dataset Format to learn what datasets are valid for which loss functions. Additionally, the Loss Overview will be your best friend to learn about the options.

Training Arguments

Key Training Arguments for improving training performance

    learning_rate
    lr_scheduler_type
    warmup_steps
    num_train_epochs
    max_steps
    per_device_train_batch_size
    per_device_eval_batch_size
    auto_find_batch_size
    fp16
    bf16
    load_best_model_at_end
    metric_for_best_model
    gradient_accumulation_steps
    activation_checkpointing
    eval_accumulation_steps
    optim
    batch_sampler
    multi_dataset_batch_sampler
    router_mapping
    learning_rate_mapping

Key Training Arguments for observing training performance

    eval_strategy
    eval_steps
    save_strategy
    save_steps
    save_total_limit
    report_to
    run_name
    log_level
    logging_steps
    push_to_hub
    hub_model_id
    hub_strategy
    hub_private_repo
args = SparseEncoderTrainingArguments(
    # Required parameter:
    output_dir="models/splade-distilbert-base-uncased-nq",
    # Optional training parameters:
    num_train_epochs=1,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=16,
    learning_rate=2e-5,
    warmup_steps=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,  # losses that use "in-batch negatives" benefit from no duplicates
    # Optional tracking/debugging parameters:
    eval_strategy="steps",
    eval_steps=0.1,
    save_strategy="steps",
    save_steps=0.1,
    save_total_limit=2,
    logging_steps=0.01,
    run_name="splade-distilbert-base-uncased-nq",  # Will be used in W&B if `wandb` is installed
)

Evaluator