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

第一、冻结的参数设置成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

相关推荐
巷955几秒前
OpenCV图像形态学:原理、操作与应用详解
人工智能·opencv·计算机视觉
深蓝易网30 分钟前
为什么制造企业需要用MES管理系统升级改造车间
大数据·运维·人工智能·制造·devops
xiangzhihong838 分钟前
Amodal3R ,南洋理工推出的 3D 生成模型
人工智能·深度学习·计算机视觉
狂奔solar1 小时前
diffusion-vas 提升遮挡区域的分割精度
人工智能·深度学习
资源大全免费分享1 小时前
MacOS 的 AI Agent 新星,本地沙盒驱动,解锁 macOS 操作新体验!
人工智能·macos·策略模式
跳跳糖炒酸奶1 小时前
第四章、Isaacsim在GUI中构建机器人(2):组装一个简单的机器人
人工智能·python·算法·ubuntu·机器人
AI.NET 极客圈1 小时前
AI与.NET技术实操系列(四):使用 Semantic Kernel 和 DeepSeek 构建AI应用
人工智能·.net
Debroon2 小时前
应华为 AI 医疗军团之战,各方动态和反应
人工智能·华为
俊哥V2 小时前
阿里通义千问发布全模态开源大模型Qwen2.5-Omni-7B
人工智能·ai
果冻人工智能2 小时前
每一条广告都只为你而生: 用 人工智能 颠覆广告行业的下一步
人工智能