🚂 Training and Finetuning

Train powerful sentence embedding models with AnglE using either CLI or Python API.


🗂️ Data Preparation

AnglE supports three dataset formats. Choose based on your task:

Format A: Pair with Label

A pair format with three columns: text1, text2, and label. The label should be a similarity score (e.g., 0-1).

Example:

{"text1": "A plane is taking off.", "text2": "An air plane is taking off.", "label": 0.95}
Format B: Query-Positive

A pair format with two columns: query and positive. Both fields can be str or List[str] (random sampling for lists).

Example:

{"query": "A person on a horse jumps over a broken down airplane.", "positive": "A person is outdoors, on a horse."}
Format C: Query-Positive-Negative

A triple format with three columns: query, positive, and negative. All fields can be str or List[str] (random sampling for lists).

Example:

{"query": "Two blond women are hugging one another.", "positive": "There are women showing affection.", "negative": "Men are fighting."}

Note

All formats use HuggingFace datasets.Dataset.


🎯 Training Methods

🐍 Method 2: Python API Training

Train models programmatically using the angle_emb library.

Open In Colab

Example:

from datasets import load_dataset
from angle_emb import AnglE

# Step 1: Load pretrained model
angle = AnglE.from_pretrained(
    'SeanLee97/angle-bert-base-uncased-nli-en-v1',
    max_length=128,
    pooling_strategy='cls'
).cuda()

# Step 2: Prepare dataset (Format A example)
ds = load_dataset('mteb/stsbenchmark-sts')
ds = ds.map(lambda obj: {
    "text1": str(obj["sentence1"]),
    "text2": str(obj['sentence2']),
    "label": obj['score']
})
ds = ds.select_columns(["text1", "text2", "label"])

# Step 3: Train the model
angle.fit(
    train_ds=ds['train'].shuffle(),
    valid_ds=ds['validation'],
    output_dir='ckpts/sts-b',
    batch_size=32,
    epochs=5,
    learning_rate=2e-5,
    save_steps=100,
    eval_steps=1000,
    warmup_steps=0,
    gradient_accumulation_steps=1,
    loss_kwargs={
        'cosine_w': 1.0,
        'ibn_w': 1.0,
        'angle_w': 0.02,
        'cosine_tau': 20,
        'ibn_tau': 20,
        'angle_tau': 20
    },
    fp16=True,
    logging_steps=100
)

# Step 4: Evaluate
corrcoef = angle.evaluate(ds['test'])
print('Spearman\'s corrcoef:', corrcoef)

⚙️ Configuration & Hyperparameters

💡 Loss Weight Parameters

Parameter

Default Value

Description

angle_w

0.02

Weight for angle loss

ibn_w

1.0

Weight for in-batch negative loss

cln_w

1.0

Weight for contrastive learning loss

cosine_w

0.0

Weight for cosine loss

💡 Temperature Parameters

Parameter

Default Value

Description

angle_tau

20.0

Temperature for angle loss

ibn_tau

20.0

Temperature for ibn and cln losses

cosine_tau

20.0

Temperature for cosine loss

💡 Fine-tuning Tips

Format-specific Recommendations:

Prevent Catastrophic Forgetting:

To alleviate information forgetting during fine-tuning:

  • Set teacher_name_or_path for knowledge distillation

  • Use same model path for self-distillation

  • Important: Teacher and student must use the same tokenizer

⚙️ Advanced Features

Training Special Models:

Applying Prompts:

Format

Flag

Applies To

Format A

--text_prompt

Both text1 and text2

Format B/C

--query_prompt

query field

Format B/C

--doc_prompt

positive and negative

Model Conversion:

Convert trained models to sentence-transformers format:

python scripts/convert_to_sentence_transformers.py --help

🔄 Integration with sentence-transformers

Training:

SentenceTransformers provides an AnglE loss implementation.

Warning

The SentenceTransformers implementation is partial. For best results, use the official angle_emb library.

Inference:

Models trained with angle_emb can be converted to sentence-transformers format using the conversion script at examples/convert_to_sentence_transformers.py.

📚 Additional Resources