大模型系列之模型参数冻结

第一、冻结的参数设置成False

比如说仅训练embedding层参数

python 复制代码
for name, param in model.named_parameters():
    if "model.embed_tokens" not in name:
        param.requires_grad = False

第二、优化器过滤False的参数

python 复制代码
optimizer = AdamW(filter(lambda p: p.requires_grad, model.parameters()))

第三、案例(以mistral为例,仅优化最后几层)

1. 加载模型

python 复制代码
model = MistralForSequenceClassification.from_pretrained(args.model_dir, num_labels=num_labels).half()

2. 默认是优化所有参数,如下所示:

python 复制代码
for name, p in model.named_parameters():
	print(name, p.requires_grad)

输出:

model.embed_tokens.weight True

model.layers.0.self_attn.q_proj.weight True

model.layers.0.self_attn.k_proj.weight True

model.layers.0.self_attn.v_proj.weight True

model.layers.0.self_attn.o_proj.weight True

model.layers.0.mlp.gate_proj.weight True

model.layers.0.mlp.up_proj.weight True

model.layers.0.mlp.down_proj.weight True

model.layers.0.input_layernorm.weight True

model.layers.0.post_attention_layernorm.weight True

model.layers.1.self_attn.q_proj.weight True

model.layers.1.self_attn.k_proj.weight True

model.layers.1.self_attn.v_proj.weight True

model.layers.1.self_attn.o_proj.weight True

model.layers.1.mlp.gate_proj.weight True

model.layers.1.mlp.up_proj.weight True

model.layers.1.mlp.down_proj.weight True

model.layers.1.input_layernorm.weight True

model.layers.1.post_attention_layernorm.weight True

model.layers.2.self_attn.q_proj.weight True

model.layers.2.self_attn.k_proj.weight True

model.layers.2.self_attn.v_proj.weight True

model.layers.2.self_attn.o_proj.weight True

model.layers.2.mlp.gate_proj.weight True

model.layers.2.mlp.up_proj.weight True

model.layers.2.mlp.down_proj.weight True

model.layers.2.input_layernorm.weight True

model.layers.2.post_attention_layernorm.weight True

model.layers.3.self_attn.q_proj.weight True

model.layers.3.self_attn.k_proj.weight True

model.layers.3.self_attn.v_proj.weight True

model.layers.3.self_attn.o_proj.weight True

model.layers.3.mlp.gate_proj.weight True

model.layers.3.mlp.up_proj.weight True

model.layers.3.mlp.down_proj.weight True

model.layers.3.input_layernorm.weight True

model.layers.3.post_attention_layernorm.weight True

model.layers.4.self_attn.q_proj.weight True

model.layers.4.self_attn.k_proj.weight True

model.layers.4.self_attn.v_proj.weight True

model.layers.4.self_attn.o_proj.weight True

model.layers.4.mlp.gate_proj.weight True

model.layers.4.mlp.up_proj.weight True

model.layers.4.mlp.down_proj.weight True

model.layers.4.input_layernorm.weight True

model.layers.4.post_attention_layernorm.weight True

model.layers.5.self_attn.q_proj.weight True

model.layers.5.self_attn.k_proj.weight True

model.layers.5.self_attn.v_proj.weight True

model.layers.5.self_attn.o_proj.weight True

model.layers.5.mlp.gate_proj.weight True

model.layers.5.mlp.up_proj.weight True

model.layers.5.mlp.down_proj.weight True

model.layers.5.input_layernorm.weight True

model.layers.5.post_attention_layernorm.weight True

model.layers.6.self_attn.q_proj.weight True

model.layers.6.self_attn.k_proj.weight True

model.layers.6.self_attn.v_proj.weight True

model.layers.6.self_attn.o_proj.weight True

model.layers.6.mlp.gate_proj.weight True

model.layers.6.mlp.up_proj.weight True

model.layers.6.mlp.down_proj.weight True

model.layers.6.input_layernorm.weight True

model.layers.6.post_attention_layernorm.weight True

model.layers.7.self_attn.q_proj.weight True

model.layers.7.self_attn.k_proj.weight True

model.layers.7.self_attn.v_proj.weight True

model.layers.7.self_attn.o_proj.weight True

model.layers.7.mlp.gate_proj.weight True

model.layers.7.mlp.up_proj.weight True

model.layers.7.mlp.down_proj.weight True

model.layers.7.input_layernorm.weight True

model.layers.7.post_attention_layernorm.weight True

model.layers.8.self_attn.q_proj.weight True

model.layers.8.self_attn.k_proj.weight True

model.layers.8.self_attn.v_proj.weight True

model.layers.8.self_attn.o_proj.weight True

model.layers.8.mlp.gate_proj.weight True

model.layers.8.mlp.up_proj.weight True

model.layers.8.mlp.down_proj.weight True

model.layers.8.input_layernorm.weight True

model.layers.8.post_attention_layernorm.weight True

model.layers.9.self_attn.q_proj.weight True

model.layers.9.self_attn.k_proj.weight True

model.layers.9.self_attn.v_proj.weight True

model.layers.9.self_attn.o_proj.weight True

model.layers.9.mlp.gate_proj.weight True

model.layers.9.mlp.up_proj.weight True

model.layers.9.mlp.down_proj.weight True

model.layers.9.input_layernorm.weight True

model.layers.9.post_attention_layernorm.weight True

model.layers.10.self_attn.q_proj.weight True

model.layers.10.self_attn.k_proj.weight True

model.layers.10.self_attn.v_proj.weight True

model.layers.10.self_attn.o_proj.weight True

model.layers.10.mlp.gate_proj.weight True

model.layers.10.mlp.up_proj.weight True

model.layers.10.mlp.down_proj.weight True

model.layers.10.input_layernorm.weight True

model.layers.10.post_attention_layernorm.weight True

model.layers.11.self_attn.q_proj.weight True

model.layers.11.self_attn.k_proj.weight True

model.layers.11.self_attn.v_proj.weight True

model.layers.11.self_attn.o_proj.weight True

model.layers.11.mlp.gate_proj.weight True

model.layers.11.mlp.up_proj.weight True

model.layers.11.mlp.down_proj.weight True

model.layers.11.input_layernorm.weight True

model.layers.11.post_attention_layernorm.weight True

model.layers.12.self_attn.q_proj.weight True

model.layers.12.self_attn.k_proj.weight True

model.layers.12.self_attn.v_proj.weight True

model.layers.12.self_attn.o_proj.weight True

model.layers.12.mlp.gate_proj.weight True

model.layers.12.mlp.up_proj.weight True

model.layers.12.mlp.down_proj.weight True

model.layers.12.input_layernorm.weight True

model.layers.12.post_attention_layernorm.weight True

model.layers.13.self_attn.q_proj.weight True

model.layers.13.self_attn.k_proj.weight True

model.layers.13.self_attn.v_proj.weight True

model.layers.13.self_attn.o_proj.weight True

model.layers.13.mlp.gate_proj.weight True

model.layers.13.mlp.up_proj.weight True

model.layers.13.mlp.down_proj.weight True

model.layers.13.input_layernorm.weight True

model.layers.13.post_attention_layernorm.weight True

model.layers.14.self_attn.q_proj.weight True

model.layers.14.self_attn.k_proj.weight True

model.layers.14.self_attn.v_proj.weight True

model.layers.14.self_attn.o_proj.weight True

model.layers.14.mlp.gate_proj.weight True

model.layers.14.mlp.up_proj.weight True

model.layers.14.mlp.down_proj.weight True

model.layers.14.input_layernorm.weight True

model.layers.14.post_attention_layernorm.weight True

model.layers.15.self_attn.q_proj.weight True

model.layers.15.self_attn.k_proj.weight True

model.layers.15.self_attn.v_proj.weight True

model.layers.15.self_attn.o_proj.weight True

model.layers.15.mlp.gate_proj.weight True

model.layers.15.mlp.up_proj.weight True

model.layers.15.mlp.down_proj.weight True

model.layers.15.input_layernorm.weight True

model.layers.15.post_attention_layernorm.weight True

model.layers.16.self_attn.q_proj.weight True

model.layers.16.self_attn.k_proj.weight True

model.layers.16.self_attn.v_proj.weight True

model.layers.16.self_attn.o_proj.weight True

model.layers.16.mlp.gate_proj.weight True

model.layers.16.mlp.up_proj.weight True

model.layers.16.mlp.down_proj.weight True

model.layers.16.input_layernorm.weight True

model.layers.16.post_attention_layernorm.weight True

model.layers.17.self_attn.q_proj.weight True

model.layers.17.self_attn.k_proj.weight True

model.layers.17.self_attn.v_proj.weight True

model.layers.17.self_attn.o_proj.weight True

model.layers.17.mlp.gate_proj.weight True

model.layers.17.mlp.up_proj.weight True

model.layers.17.mlp.down_proj.weight True

model.layers.17.input_layernorm.weight True

model.layers.17.post_attention_layernorm.weight True

model.layers.18.self_attn.q_proj.weight True

model.layers.18.self_attn.k_proj.weight True

model.layers.18.self_attn.v_proj.weight True

model.layers.18.self_attn.o_proj.weight True

model.layers.18.mlp.gate_proj.weight True

model.layers.18.mlp.up_proj.weight True

model.layers.18.mlp.down_proj.weight True

model.layers.18.input_layernorm.weight True

model.layers.18.post_attention_layernorm.weight True

model.layers.19.self_attn.q_proj.weight True

model.layers.19.self_attn.k_proj.weight True

model.layers.19.self_attn.v_proj.weight True

model.layers.19.self_attn.o_proj.weight True

model.layers.19.mlp.gate_proj.weight True

model.layers.19.mlp.up_proj.weight True

model.layers.19.mlp.down_proj.weight True

model.layers.19.input_layernorm.weight True

model.layers.19.post_attention_layernorm.weight True

model.layers.20.self_attn.q_proj.weight True

model.layers.20.self_attn.k_proj.weight True

model.layers.20.self_attn.v_proj.weight True

model.layers.20.self_attn.o_proj.weight True

model.layers.20.mlp.gate_proj.weight True

model.layers.20.mlp.up_proj.weight True

model.layers.20.mlp.down_proj.weight True

model.layers.20.input_layernorm.weight True

model.layers.20.post_attention_layernorm.weight True

model.layers.21.self_attn.q_proj.weight True

model.layers.21.self_attn.k_proj.weight True

model.layers.21.self_attn.v_proj.weight True

model.layers.21.self_attn.o_proj.weight True

model.layers.21.mlp.gate_proj.weight True

model.layers.21.mlp.up_proj.weight True

model.layers.21.mlp.down_proj.weight True

model.layers.21.input_layernorm.weight True

model.layers.21.post_attention_layernorm.weight True

model.layers.22.self_attn.q_proj.weight True

model.layers.22.self_attn.k_proj.weight True

model.layers.22.self_attn.v_proj.weight True

model.layers.22.self_attn.o_proj.weight True

model.layers.22.mlp.gate_proj.weight True

model.layers.22.mlp.up_proj.weight True

model.layers.22.mlp.down_proj.weight True

model.layers.22.input_layernorm.weight True

model.layers.22.post_attention_layernorm.weight True

model.layers.23.self_attn.q_proj.weight True

model.layers.23.self_attn.k_proj.weight True

model.layers.23.self_attn.v_proj.weight True

model.layers.23.self_attn.o_proj.weight True

model.layers.23.mlp.gate_proj.weight True

model.layers.23.mlp.up_proj.weight True

model.layers.23.mlp.down_proj.weight True

model.layers.23.input_layernorm.weight True

model.layers.23.post_attention_layernorm.weight True

model.layers.24.self_attn.q_proj.weight True

model.layers.24.self_attn.k_proj.weight True

model.layers.24.self_attn.v_proj.weight True

model.layers.24.self_attn.o_proj.weight True

model.layers.24.mlp.gate_proj.weight True

model.layers.24.mlp.up_proj.weight True

model.layers.24.mlp.down_proj.weight True

model.layers.24.input_layernorm.weight True

model.layers.24.post_attention_layernorm.weight True

model.layers.25.self_attn.q_proj.weight True

model.layers.25.self_attn.k_proj.weight True

model.layers.25.self_attn.v_proj.weight True

model.layers.25.self_attn.o_proj.weight True

model.layers.25.mlp.gate_proj.weight True

model.layers.25.mlp.up_proj.weight True

model.layers.25.mlp.down_proj.weight True

model.layers.25.input_layernorm.weight True

model.layers.25.post_attention_layernorm.weight True

model.layers.26.self_attn.q_proj.weight True

model.layers.26.self_attn.k_proj.weight True

model.layers.26.self_attn.v_proj.weight True

model.layers.26.self_attn.o_proj.weight True

model.layers.26.mlp.gate_proj.weight True

model.layers.26.mlp.up_proj.weight True

model.layers.26.mlp.down_proj.weight True

model.layers.26.input_layernorm.weight True

model.layers.26.post_attention_layernorm.weight True

model.layers.27.self_attn.q_proj.weight True

model.layers.27.self_attn.k_proj.weight True

model.layers.27.self_attn.v_proj.weight True

model.layers.27.self_attn.o_proj.weight True

model.layers.27.mlp.gate_proj.weight True

model.layers.27.mlp.up_proj.weight True

model.layers.27.mlp.down_proj.weight True

model.layers.27.input_layernorm.weight True

model.layers.27.post_attention_layernorm.weight True

model.layers.28.self_attn.q_proj.weight True

model.layers.28.self_attn.k_proj.weight True

model.layers.28.self_attn.v_proj.weight True

model.layers.28.self_attn.o_proj.weight True

model.layers.28.mlp.gate_proj.weight True

model.layers.28.mlp.up_proj.weight True

model.layers.28.mlp.down_proj.weight True

model.layers.28.input_layernorm.weight True

model.layers.28.post_attention_layernorm.weight True

model.layers.29.self_attn.q_proj.weight True

model.layers.29.self_attn.k_proj.weight True

model.layers.29.self_attn.v_proj.weight True

model.layers.29.self_attn.o_proj.weight True

model.layers.29.mlp.gate_proj.weight True

model.layers.29.mlp.up_proj.weight True

model.layers.29.mlp.down_proj.weight True

model.layers.29.input_layernorm.weight True

model.layers.29.post_attention_layernorm.weight True

model.layers.30.self_attn.q_proj.weight True

model.layers.30.self_attn.k_proj.weight True

model.layers.30.self_attn.v_proj.weight True

model.layers.30.self_attn.o_proj.weight True

model.layers.30.mlp.gate_proj.weight True

model.layers.30.mlp.up_proj.weight True

model.layers.30.mlp.down_proj.weight True

model.layers.30.input_layernorm.weight True

model.layers.30.post_attention_layernorm.weight True

model.layers.31.self_attn.q_proj.weight True

model.layers.31.self_attn.k_proj.weight True

model.layers.31.self_attn.v_proj.weight True

model.layers.31.self_attn.o_proj.weight True

model.layers.31.mlp.gate_proj.weight True

model.layers.31.mlp.up_proj.weight True

model.layers.31.mlp.down_proj.weight True

model.layers.31.input_layernorm.weight True

model.layers.31.post_attention_layernorm.weight True

model.norm.weight True

score.weight True

3. 优化最后几层:

python 复制代码
for name, param in model.named_parameters():
    param.requires_grad = False
    for k in [31, 30, 29, 'score', 'model.norm.weight']:
        if str(k) in name:
            param.requires_grad = True
            print(name)

optimizer = AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=args.learning_rate)

输出:

model.layers.29.self_attn.q_proj.weight

model.layers.29.self_attn.k_proj.weight

model.layers.29.self_attn.v_proj.weight

model.layers.29.self_attn.o_proj.weight

model.layers.29.mlp.gate_proj.weight

model.layers.29.mlp.up_proj.weight

model.layers.29.mlp.down_proj.weight

model.layers.29.input_layernorm.weight

model.layers.29.post_attention_layernorm.weight

model.layers.30.self_attn.q_proj.weight

model.layers.30.self_attn.k_proj.weight

model.layers.30.self_attn.v_proj.weight

model.layers.30.self_attn.o_proj.weight

model.layers.30.mlp.gate_proj.weight

model.layers.30.mlp.up_proj.weight

model.layers.30.mlp.down_proj.weight

model.layers.30.input_layernorm.weight

model.layers.30.post_attention_layernorm.weight

model.layers.31.self_attn.q_proj.weight

model.layers.31.self_attn.k_proj.weight

model.layers.31.self_attn.v_proj.weight

model.layers.31.self_attn.o_proj.weight

model.layers.31.mlp.gate_proj.weight

model.layers.31.mlp.up_proj.weight

model.layers.31.mlp.down_proj.weight

model.layers.31.input_layernorm.weight

model.layers.31.post_attention_layernorm.weight

model.norm.weight

score.weight

相关推荐
小于小于大橙子3 小时前
视觉SLAM数学基础
人工智能·数码相机·自动化·自动驾驶·几何学
封步宇AIGC5 小时前
量化交易系统开发-实时行情自动化交易-3.4.2.Okex行情交易数据
人工智能·python·机器学习·数据挖掘
封步宇AIGC5 小时前
量化交易系统开发-实时行情自动化交易-2.技术栈
人工智能·python·机器学习·数据挖掘
景鹤5 小时前
【算法】递归+回溯+剪枝:78.子集
算法·机器学习·剪枝
陌上阳光5 小时前
动手学深度学习68 Transformer
人工智能·深度学习·transformer
OpenI启智社区5 小时前
共筑开源技术新篇章 | 2024 CCF中国开源大会盛大开幕
人工智能·开源·ccf中国开源大会·大湾区
AI服务老曹5 小时前
建立更及时、更有效的安全生产优化提升策略的智慧油站开源了
大数据·人工智能·物联网·开源·音视频
YRr YRr5 小时前
PyTorch:torchvision中的dataset的使用
人工智能
love_and_hope6 小时前
Pytorch学习--神经网络--完整的模型训练套路
人工智能·pytorch·python·深度学习·神经网络·学习
思通数据6 小时前
AI与OCR:数字档案馆图像扫描与文字识别技术实现与项目案例
大数据·人工智能·目标检测·计算机视觉·自然语言处理·数据挖掘·ocr