LoRA 原理

LoRA 在原始权重旁添加低秩矩阵 AABB,只训练这些小矩阵:

W=W+αABW' = W + \alpha \cdot AB

其中 ARd×rA \in \mathbb{R}^{d \times r}, BRr×kB \in \mathbb{R}^{r \times k},秩 rr 通常取 8~64。

实践代码

from peft import LoraConfig, get_peft_model

config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "v_proj"],
    lora_dropout=0.1,
)
model = get_peft_model(base_model, config)

LoRA 大幅降低了微调门槛,配合 RLHF 原理与实践 中介绍的训练方法效果更佳。