lora 简单代码复现

import torch
import torch.nn as nn
import torch.nn.functional as F
import math

class Loar_LINER(nn.Module):
    def __init__(self, infeature, outfeature, merge, rank=16, loar_alpha=16, dropout=0.5):
        super(Loar_LINER, self).__init__()
        self.infeature = infeature
        self.outfeature = outfeature
        self.merge = merge
        self.rank = rank
        self.dropout = dropout
        self.loar_alpha = loar_alpha

        self.linear = nn.Linear(infeature, outfeature)
        if rank > 0:
            self.loar_b = nn.Parameter(torch.zeros(outfeature, rank))
            self.loar_a = nn.Parameter(torch.zeros(rank, infeature))
            self.scale = self.loar_alpha / self.rank
            self.linear.weight.requires_grad = False
        if self.dropout >= 0:
            self.dropout = nn.Dropout(self.dropout)
        else:
            self.dropout = nn.Identity()
        self.initial_weight()

    def initial_weight(self):
        nn.init.kaiming_uniform_(self.loar_a, a=math.sqrt(5))
        nn.init.zeros_(self.loar_b)

    def forward(self, x):
        if self.rank > 0 and self.merge:
            output = F.linear(x, self.linear.weight + self.loar_b @ self.loar_a * self.scale, self.linear.bias)
        else:
            output = self.linear(x)
        return self.dropout(output)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值