- Transformers 模型設計上是可定制的。
- 每個模型的代碼都包含在 Transformers 倉庫的
model
子文件夾中(transformers/src/transformers/models at main · huggingface/transformers),每個模型文件夾通常包含:modeling.py
:定義模型結構與前向傳播configuration.py
:定義模型的超參數配置
1?配置(Configuration)
1.1 自定義配置
- 自定義配置類的要點:
- 必須繼承自
PretrainedConfig
,以繼承from_pretrained()
、save_pretrained()
等功能; - 構造函數
__init__()
必須接收任意**kwargs
并傳給父類; - 添加
model_type
屬性,以支持 AutoClass; - 可以加入參數校驗邏輯。
- 必須繼承自
1.2 保存配置
resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True)
resnet50d_config.save_pretrained("custom-resnet")
2?模型結構
- 模型類需要繼承自
PreTrainedModel
,并接受配置對象作為輸入 - Transformers 約定模型的所有超參數由配置對象提供
- 可以構建兩種模型:
2.1?裸模型(輸出隱藏狀態)
2.2?帶分類頭的模型(支持 Trainer
,輸出 logits 和 loss)
2.3?加載預訓練權重
import timmresnet50d = ResnetModel(resnet50d_config)
#此時 resnet50d.model 就是一個結構為 ResNet-50d 的模型,但權重是 隨機初始化的,沒有訓練。pretrained_model = timm.create_model("resnet50d", pretrained=True)
#從 timm 加載已經訓練好的 resnet50d 模型resnet50d.model.load_state_dict(pretrained_model.state_dict())
3 啟用 AutoClass 支持
AutoClass API 能自動根據配置加載模型,簡化用戶調用
需要:
-
在配置類中加入
model_type
; -
在模型類中加入
config_class
; -
使用
AutoConfig.register()
和AutoModel.register()
注冊。
from transformers import AutoConfig, AutoModel, AutoModelForImageClassificationAutoConfig.register("resnet", ResnetConfig)
#注冊自定義配置類 ResnetConfig。
#"resnet" 是 ResnetConfig.model_type,它必須和配置類中的 model_type = "resnet" 一致。
#注冊后,用戶可以通過 AutoConfig.from_pretrained() 自動加載這個配置類。AutoModel.register(ResnetConfig, ResnetModel)
#把裸模型類 ResnetModel 綁定到 AutoModel。
'''
這樣用戶就可以用如下方式加載模型:
model = AutoModel.from_pretrained("your-username/custom-resnet50d", trust_remote_code=True)
'''AutoModelForImageClassification.register(ResnetConfig, ResnetModelForImageClassification)
#注冊了你帶分類頭的模型 ResnetModelForImageClassification 到 AutoModelForImageClassification。
'''
用戶可以像這樣加載:
model = AutoModelForImageClassification.from_pretrained("your-username/custom-resnet50d", trust_remote_code=True
)
'''
4 本地保存& 加載特定模型
?假設已經定義和注冊配置和模型,并加載了預訓練權重
resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True)
#加載自定義configresnet50d = ResnetModelForImageClassification(resnet50d_config)
#加載自定義model# 加載預訓練權重
import timm
pretrained = timm.create_model("resnet50d", pretrained=True)
resnet50d.model.load_state_dict(pretrained.state_dict())
注冊 AutoClass 支持,保存 AutoClass 映射信息
resnet50d_config.register_for_auto_class()
resnet50d.register_for_auto_class("AutoModelForImageClassification")
保存模型和配置到本地
resnet50d.save_pretrained("custom-resnet50d/")
resnet50d_config.save_pretrained("custom-resnet50d/")
4.1 本地重新加載
from transformers import AutoModelForImageClassification# 加載模型
model = AutoModelForImageClassification.from_pretrained("custom-resnet50d/", trust_remote_code=True
)
由于使用的是自定義模型類,加載時一定要加上trust_remote_code=True
4.2 保存后的本地目錄
4.3 為什么要保存config?
config
是必須保存的,因為AutoModel
是依賴config.json
來決定加載哪個模型類。- AutoModel.from_pretrained("path_or_repo")背后的機制是
- 先加載配置文件
config.json
config = AutoConfig.from_pretrained("path_or_repo")
- 根據
config.model_type
決定使用哪個模型類"model_type": "resnet"
→ 查找注冊的ResnetModel
-
再加載權重文件(.bin 或 .safetensors)到模型中
- 先加載配置文件