我试图了解将低秩近似应用于类中的可学习参数是否有意义。目标是减少参数数量。
我有以下自定义模块:
class CustomPara(nn.Module):
def __init__(self, num_blocks, in_planes, out_planes, kernel_size):
super(CustomPara, self).__init__()
self.coefficient_shape = (num_blocks,1,1,1,1)
blocks = [torch.Tensor(out_planes, in_planes, kernel_size, kernel_size) for _ in range(num_blocks)]
for i in range(num_blocks): init.kaiming_normal_(blocks[i])
self.blocks = nn.Parameter(torch.stack(blocks)) # this is what we will freeze later
def forward(self, coefficients):
final_blocks = (self.blocks*coefficients).sum(0)
return final_blocks
是否可以在
blocks
参数上使用低秩自适应来减少可学习参数的数量?
确实,这个想法在论文LoRA:Low-Rank Adaptation of Large Language Models中介绍过。
关键思想是任何形状为 n * m 且秩为 r 的矩阵 W 都可以写成两个矩阵 A、B 的乘法,使得 A 具有形状 n * r 且 B 具有形状 r * m。
通过引入 A、B 作为可训练矩阵而不是 W 本身,您可以减少可训练参数的数量,但模型性能会略有下降。