train-sentence-transformers
它能做什么
这个技能训练能把文本转换为可搜索向量的模型,或为两段文本的匹配程度打分的模型。适合开发搜索、匹配或聚类功能的开发者。
适用场景
- ✓当你想为搜索或相似度微调嵌入模型时。
- ✓当你想训练一个为查询和段落配对打分的重排序模型的时候。
- ✓当你需要训练稀疏或多向量检索模型的时候。
技能链接
https://github.com/huggingface/skills/blob/main/skills/train-sentence-transformers/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请用我的支持问答配对数据微调一个 sentence-transformers 模型,以改善搜索效果。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会选择合适的模型类型,例如双编码器、交叉编码器或稀疏编码器。它会从经过测试的训练模板开始,而不是从头编写脚本,并根据你的数据进行调整。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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_<type>_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) |
| [MultiVectorEncoder] | MultiVectorEncoder (ColBERT) |
One embedding per token, scored with MaxSim | Late-interaction retrieval, recall gains over bi-encoders at higher storage cost, multimodal (ColPali / ColQwen2) |
Tiebreakers when the request is ambiguous: "embedding model" / "vector search" / "similarity" → [SentenceTransformer]. "rerank" / "ranker" / "two-stage" → [CrossEncoder]. "SPLADE" / "sparse" / "inverted index" → [SparseEncoder]. "ColBERT" / "late interaction" / "multi-vector" / "MaxSim" / "ColPali" / "ColQwen" → [MultiVectorEncoder]. 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_DUPLICATESrequirement for MNRL-family,Cached*↔gradient_checkpointingincompatibility.references/evaluators_sentence_transformer.md: evaluator-to-task mapping,metric_for_best_modelkey construction (named vs unnamed), per-evaluatorprimary_metricvalues.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_weightderivation,activation_fn=Identity()mandatory for non-BCE losses (silent eval-rank collapse otherwise).references/evaluators_cross_encoder.md:CrossEncoderRerankingEvaluatorrecipe, named-evaluator key formateval_{name}_{primary_metric}.scripts/train_cross_encoder_example.py: production template. Copy this as your starting point.
[SparseEncoder]
references/losses_sparse_encoder.md:SpladeLosswrapper requirement, FLOPS regularizer weights, smoke-test active-dim ramp behavior.references/evaluators_sparse_encoder.md:SparseNanoBEIREvaluator(English-only) and the in-domain alternative,eval_{name}_{primary_metric}key format.scripts/train_sparse_encoder_example.py: production template. Copy this as your starting point.
[MultiVectorEncoder]
references/losses_multi_vector_encoder.md: MaxSim scoring, scale choice per scoring mode (scale=1.0for MaxSim, roughly the average query length for MeanMaxSim), MNRL / CachedMNRL / MarginMSE / DistillKLDiv, XTR-vs-ColBERT scoring, CachedMNRL ↔gradient_checkpointingincompatibility.references/evaluators_multi_vector_encoder.md:MultiVectorNanoBEIREvaluator(English-only) and the in-domain alternative,eval_NanoBEIR_mean_maxsim_ndcg@10key format, distillation-eval spearman variant.scripts/train_multi_vector_encoder_example.py: production template. Copy this as your starting point.
Cross-cutting: always required (regardless of task)
references/training_args.md:TrainingArgumentsknobs, pre
常见问题
它涵盖哪些模型类型?
它涵盖用于嵌入的双编码器、用于重排序的交叉编码器、SPLADE 等稀疏编码器,以及多向量模型。
为什么要从模板开始?
模板包含一些容易遗漏的设置细节,从模板开始可以避免常见错误。
它能帮助选择模型类型吗?
能。技能中的一张表格可以帮助把模型类型与任务相匹配。
详情
作者的原始描述
Train or fine-tune sentence-transformers models across `SentenceTransformer` (bi-encoder, dense or static embedding model for retrieval, similarity, clustering, classification, paraphrase mining, dedup, multimodal), `CrossEncoder` (reranker, pair scoring for two-stage retrieval / pair classification), `SparseEncoder` (SPLADE, sparse embedding model for learned-sparse retrieval), and `MultiVectorEncoder` (ColBERT / late-interaction, per-token embeddings scored with MaxSim). Covers loss selection, hard-negative mining, evaluators, distillation, LoRA, Matryoshka, and Hugging Face Hub publishing.
repo: huggingface/skills
热度
- GitHub 星标
- 11k
- 排名
- #11781
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/30
评论
- …
同一仓库的更多技能
hf-cli
11k帮你的智能体使用 Hugging Face 命令行工具,下载、上传和管理模型、数据集、Spaces、仓库以及本地缓存。
hf-cloud-aws-context-discovery
11k帮你的智能体在进行任何云端工作之前,找到你的 AWS 配置文件、区域和账户信息,避免猜测该使用哪个账户。
hf-cloud-python-env-setup
11k帮你的智能体设置一个干净的 Python 环境,使用合适的 Python 版本和最新的库,然后再运行 SageMaker 或 AWS 自动化脚本。
hf-cloud-sagemaker-deployment-planner
11k帮你的智能体规划如何把模型部署到 Amazon SageMaker,在实时、无服务器、异步或批量托管之间进行选择,并使用合理的默认设置。