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

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

比如说仅训练embedding层参数

for name, param in model.named_parameters():
    if "model.embed_tokens" not in name:
        param.requires_grad = False

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

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

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

1. 加载模型

model = MistralForSequenceClassification.from_pretrained(args.model_dir, num_labels=num_labels).half()

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

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. 优化最后几层:

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

你可能感兴趣的:(深度学习,机器学习,人工智能)