train-sentence-transformers
它能做什么
这个功能可以用来训练或微调 AI 模型,让它理解文本之间的相似度。它会为词语或句子生成数字表示(嵌入,embeddings),适用于信息检索、文本聚类或分类等任务。
适用场景
- ✓当你希望 AI 找到与某段示例文本相似的文本时。
- ✓当你需要自动将主题相近的文本归为一组时。
- ✓当你希望 AI 把文本分到不同类别中时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/train-sentence-transformers/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“训练一个模型,用来查找与科技相关的相似新闻文章。”
每周 5 个新技能,1 分钟读完。
工作原理
系统利用示例数据,教模型把含义相近的文本映射到数值上彼此接近的位置。然后根据具体示例调整模型,以提升它在相似度比较或分类任务中的表现。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Train a sentence-transformers Model
When to Use
Use this skill when you need 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...
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) |
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_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.
Cross-cutting — always required (regardless of task)
references/training_args.md—TrainingArgumentsknobs, precision rules (load fp32 + autocast bf16/fp16; nevertorch_dtype=bfloat16),warmup_steps(float) vs deprecatedwarmup_ratio,save_stepsmust be a multiple ofeval_stepsforload_best_model_at_end, schedulers, HPO, tracker, resume, hub-push variants.references/dataset_formats.md— column-matching rules (label name auto-detection; column-order-not-name); reshaping recipes; hard-negative mining options.
…
常见问题
SentenceTransformer、CrossEncoder 和 SparseEncoder 有什么区别?
SentenceTransformer 为每段文本生成一个向量(适合通用搜索)。CrossEncoder 将两段文本放在一起比较,给出一个分数(适合对结果进行精细排序)。SparseEncoder 使用另一种方法生成稀疏向量,适合配合索引进行精确搜索。
使用这个功能需要写代码吗?
需要。这个功能面向已经熟悉编程的人。它提供了模型和说明,但你需要根据自己的具体需求修改代码示例。
什么是“嵌入”(embeddings)?
嵌入是文本的数值表示(向量)。含义相近的文本,其嵌入在数值上也会很接近,这样 AI 就能理解它们之间的关系。
详情
作者的原始描述
Train or fine-tune SentenceTransformer, CrossEncoder, and SparseEncoder models for retrieval, similarity, clustering, classification, reranking, and related embedding tasks.
热度
- GitHub 星标
- 48k
- 排名
- #6014
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。