train-sentence-transformers
🤖 AI Summary
This skill trains a sentence-transformers model for generating semantically meaningful sentence embeddings. It provides production-ready training templates and references for configuring losses, evaluators, and training arguments, rather than generating scripts from scratch.
How to Install
Claude Code:
git clone --depth 1 https://github.com/huggingface/skills.git && cp skills/skills/train-sentence-transformers ~/.claude/skills/train-sentence-transformers -r# Train a sentence-transformers Model
**This SKILL.md is a router, not a manual.** It tells you which references and example scripts to load for your task. The actual content — recommended losses, evaluators, training-script structure, model selection, training-arg knobs, troubleshooting — lives in `references/` and `scripts/`.
**Do not synthesize a training script from this file alone.** Open the per-type production template (`scripts/train__example.py`) and copy it as your starting point. The templates contain load-bearing scaffolding (autocast helper, model-card class, logger silencing list, `force=True`, `seed`, TF32, version-compatible imports, named-evaluator metric handling) that prior agent runs have repeatedly missed when rolling their own from a synthesized snippet.
## 1. Identify the model type
| Tag | Class | What it does | When to pick |
|---|---|---|---|
| **[SentenceTransformer]** | `SentenceTransformer` (bi-encoder) | Maps each input to a fixed-dim dense vector | Retrieval, similarity, clustering, classification, paraphrase mining, dedup |
| **[CrossEncoder]** | `CrossEncoder` (reranker) | Scores `(query, passage)` pairs jointly | Two-stage retrieval (rerank top-100 from bi-encoder), pair classification |
| **[SparseEncoder]** | `SparseEncoder` (SPLADE) | Sparse vectors over the vocabulary | Learned-sparse retrieval, inverted-index backends (Elasticsearch / OpenSearch / Lucene) |
Tiebreakers when the request is ambiguous: "embedding model" / "vector search" / "similarity" → **[SentenceTransformer]**. "rerank" / "ranker" / "two-stage" → **[CrossEncoder]**. "SPLADE" / "sparse" / "inverted index" → **[SparseEncoder]**. If still unclear, ask.
## 2. Required reading
**Read these in full before writing any code. Do not triage by perceived relevance.**
### Per-type — always required
**[SentenceTransformer]**
- `references/losses_sentence_transformer.md` — loss-to-data-shape mapping; `BatchSamplers.NO_DUPLICATES` requirement for MNRL-family; `Cached*` ↔ `gradient_checkpointing` incompatibility.
- `references/evaluators_sentence_transformer.md` — evaluator-to-task mapping; `metric_for_best_model` key construction (named vs unnamed); per-evaluator `primary_metric` values.
- `references/model_architectures.md` — encoder vs decoder vs static vs Router pipelines; pooling rules (mean / cls / lasttoken); auto-mean-pooling behavior for fresh-start MLM bases.
- `scripts/train_sentence_transformer_example.py` — production template; copy this as your starting point.
**[CrossEncoder]**
- `references/losses_cross_encoder.md` — pointwise / pairwise / listwise / distillation; `pos_weight` derivation; `activation_fn=Identity()` mandatory for non-BCE losses (silent eval-rank collapse otherwise).
- `references/evaluators_cross_encoder.md` — `CrossEncoderRerankingEvaluator` recipe; named-evaluator key format `eval_{name}_{primary_metric}`.
- `scripts/train_cross_encoder_example.py` — production template; copy this as your starting point.
**
Details
| Category | AI/ML → ml |
| Source | huggingface/skills |
| SKILL.md | View on GitHub → |
| Repo Stars | ★ 10.7K |
| Est. per Skill | 357 (shared across 30 skills from this repo) |
| Difficulty | Intermediate |
| Risk Level | N/A |
Related Skills
ai-product
AI Product Development Every product will be AI-powered. The question is whether you'll build it rig
agentfolio
AgentFolio Role: Autonomous Agent Discovery Guide Use this skill when you want to discover, compare,
onboarding-psychologist
You are a Behavioral Psychologist specializing in habit formation and user retention. Your task is t
marketing-psychology
Marketing Psychology & Mental Models (Applied · Ethical · Prioritized) You are a marketing psych
Works Well With
Skills from the same repository — often designed to work together
hf-mem
hfmem estimates the required memory for inference, including model weights and an optional KV cache,
hf-cli
Install: curl -LsSf https://hf.co/cli/install.sh | bash -s. The Hugging Face Hub CLI tool hf is avai
trl-training
TRL Training Skill You are an expert at using the TRL (Transformers Reinforcement Learning) library