
transformers 完整指南400 预训练模型一行接入与微调的 3 步实战【免费下载链接】transformers Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.项目地址: https://gitcode.com/GitHub_Trending/tra/transformers还在每个新模型出来都重写一遍模型代码transformers是 HuggingFace 的开源模型定义框架文本、视觉、音频、多模态共 400 多个预训练模型一行pipeline就能推理同一套代码直接做微调和部署。你拿到的东西transformers 四类任务能力速览它把模型定义标准化了——模型在 src/transformers/models/ 下按家族组织上层通过pipeline、Auto*类、Trainer三种入口复用。能力对应入口真实输出来自项目测试用例零样本图像分类pipeline(zero-shot-image-classification)[{score: 0.333, label: a}, {score: 0.333, label: b}, ...]目标检测pipeline(object-detection)每项含score、label、boxxmin/ymin/ymax/xmax深度估计pipeline(depth-estimation)16 位深度图可直接存为png微调训练TrainerTrainingArguments训练日志、checkpoint、评估指标上表第一行来自 tests/pipelines/test_pipelines_zero_shot_image_classification.py对一张 COCO 样张传入candidate_labels[a, b, c]断言三个分数之和近似为 1——这就是transformers推理结果的典型形态。0130 秒安装并跑通第一条推理问题很直接装完不知道从哪下第一行手。最短路径是pipeline工厂函数——它按任务名自动匹配模型、下载权重、备好输入预处理。pip install transformersfrom transformers import pipeline clf pipeline(sentiment-analysis) print(clf(这个 API 真的很好用))首跑会拉取默认模型权重之后走本地缓存。看到labelscore的输出说明整条链路已通。02用真实 COCO 样张跑通目标检测想让一张图拿到框 类别 置信度不想自己写input_ids、attention_mask把图直接喂给检测 pipeline 即可from transformers import pipeline from PIL import Image det pipeline(object-detection) img Image.open(tests/fixtures/tests_samples/COCO/000000039769.png) print(det(img))真实结果见项目测试用例 tests/pipelines/test_pipelines_object_detection.py它对同一张000000039769.png断言——每个检测项的score大于 0、label是字符串、box是含四个坐标的 dict。同一张样张还被 tests/pipelines/test_pipelines_depth_estimation.py 复用验证深度估计输出是 16 位 PIL 图像。 输入不限于 PILURL、numpy 数组、路径字符串都能直接传给pipeline预处理由模型自带的 processor 完成。03从推理到微调三个文件跑通训练闭环问题验证完 demo想把模型调到自己数据上。transformers的答案是Trainer——一个内置的训练循环日志、断点保存、评估全给你做好。from transformers import Trainer, TrainingArguments args TrainingArguments(out, learning_rate2e-5) trainer Trainer(modelmodel, argsargs, train_datasettrain_ds) trainer.train()端到端脚本在 examples/pytorch/ 下按任务分目录例如text-classification/run_glue.py、object-detection/run_object_detection.py——CI 里就是靠这些脚本回归的。想加自定义回调、优化器看 src/transformers/trainer.py 与 trainer_optimizer.py。快速上手角安装命令与常用入口一览一行安装pip install transformers入口说明适用场景pipeline(task)一行加载模型 预处理直接喂数据推理 demo 最快路径AutoModelFor*系列手动组合模型与 tokenizer控制输入张量需要自定义 forward 流程TrainerTrainingArguments内置训练循环、日志、checkpoint微调与评估transformersCLIdownload/serve/chat子命令模型下载与 API 部署CLI 实现在 src/transformers/cli/其中serve基于 vLLM适合把模型挂成 OpenAI 兼容接口。避坑清单前 5 个最容易踩的坑首跑下载慢pipeline首次调用会拉权重网络不稳先transformers download预热缓存。两种入口别混用pipeline收 PIL/URL/数组AutoModel收张量输入形态完全不同。大模型爆显存加device_mapauto做张量并行切分或dtypetorch.float16降精度。Auto 类匹配失败config 推不出对应任务时直接报错改用具体类手动加载即可。示例脚本缺依赖跑 examples/pytorch/ 脚本前先装对应目录的requirements.txt。⚠️ 精度切换fp16/bf16可能改变分数的小数位写断言或对比基准时用近似比较项目测试里普遍用pytest.approx。收尾回到开篇的问题——新模型出来不用从零写transformers把 400 多个模型的输入预处理、推理、微调都收敛到pipeline、Auto*、Trainer三个入口里。你负责数据与目标它负责模型定义这一层这正是 README.md 里模型定义框架承诺的全部。【免费下载链接】transformers Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.项目地址: https://gitcode.com/GitHub_Trending/tra/transformers创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考