编程语言
首页 > 编程语言> > 对抗训练fgm和pgd原理和源码分析

对抗训练fgm和pgd原理和源码分析

作者:互联网

当前,在各大NLP竞赛中,对抗训练已然成为上分神器,尤其是fgm和pgd使用较多,下面来说说吧。对抗训练是一种引入噪声的训练方式,可以对参数进行正则化,提升模型鲁棒性和泛化能力。

fgm

FGM的全称是Fast Gradient Method, 出现于Adversarial Training Methods for Semi-supervised Text Classification这篇论文,FGM是根据具体的梯度进行scale,得到更好的对抗样本:
在这里插入图片描述
整个对抗训练的过程如下,伪代码如下:

fgm代码实现如下:

class FGM:
    def __init__(self, model: nn.Module, eps=1.):
        self.model = (
            model.module if hasattr(model, "module") else model
        )
        self.eps = eps
        self.backup = {}

    # only attack word embedding
    def attack(self, emb_name='word_embeddings'):
        for name, param in self.model.named_parameters():
            if param.requires_grad and emb_name in name:
                self.backup[name] = param.data.clone()
                norm = torch.norm(param.grad)
                if norm and not torch.isnan(norm):
                    r_at = self.eps * param.grad / norm
                    param.data.add_(r_at)

    def restore(self, emb_name='word_embeddings'):
        for name, para in self.model.named_parameters():
            if para.requires_grad and emb_name in name:
                assert name in self.backup
                para.data = self.backup[name]

        self.backup = {}

fgm应用代码如下:

##对应第一步
loss = model(**batch_data)[0]	
loss.backward()
##对应第二步
fgm.attack()
#对应第三步
loss_adv = model(**batch_data)[0]
loss_adv.backward()
#对应第四步
fgm.restore()
#对应第五步
optimizer.step()

pgd

FGM直接通过epsilon参数一下子算出了对抗扰动,这样得到的可能不是最优的。因此PGD进行了改进,多迭代几次,慢慢找到最优的扰动。
引用:

FGM简单粗暴的“一步到位”,可能走不到约束内的最优点。PGD则是“小步走,多走几步”,如果走出了扰动半径为epsilon的空间,就映射回“球面”上,以保证扰动不要过大

在这里插入图片描述
并且
在这里插入图片描述
pgd整个对抗训练的过程如下,伪代码如下:

可以看到,在循环中r是逐渐累加的,要注意的是最后更新参数只使用最后一个x+r算出来的梯度。
pgd代码实现如下:

class PGD:
    def __init__(self, model, eps=1., alpha=0.3):
        self.model = (
            model.module if hasattr(model, "module") else model
        )
        self.eps = eps
        self.alpha = alpha
        self.emb_backup = {}
        self.grad_backup = {}

    def attack(self, emb_name='word_embeddings', is_first_attack=False):
        for name, param in self.model.named_parameters():
            if param.requires_grad and emb_name in name:
                if is_first_attack:
                    self.emb_backup[name] = param.data.clone()
                norm = torch.norm(param.grad)
                if norm != 0 and not torch.isnan(norm):
                    r_at = self.alpha * param.grad / norm
                    param.data.add_(r_at)
                    param.data = self.project(name, param.data)

    def restore(self, emb_name='word_embeddings'):
        for name, param in self.model.named_parameters():
            if param.requires_grad and emb_name in name:
                assert name in self.emb_backup
                param.data = self.emb_backup[name]
        self.emb_backup = {}

    def project(self, param_name, param_data):
        r = param_data - self.emb_backup[param_name]
        if torch.norm(r) > self.eps:
            r = self.eps * r / torch.norm(r)
        return self.emb_backup[param_name] + r

    def backup_grad(self):
        for name, param in self.model.named_parameters():
            if param.requires_grad and param.grad is not None:
                self.grad_backup[name] = param.grad.clone()

    def restore_grad(self):
        for name, param in self.model.named_parameters():
            if param.requires_grad and param.grad is not None:
                param.grad = self.grad_backup[name]

pgd应用代码如下:

loss = model(**batch_data)[0]	
loss.backward()
pgd.backup_grad()
for _t in range(pgd_k):
	pgd.attack(is_first_attack=(_t == 0))
	if _t != pgd_k - 1:
		model.zero_grad()
	else:
		pgd.restore_grad()
	loss_adv = model(**batch_data)[0]
	loss_adv.backward()
pgd.restore()
optimizer.step()

注:在torch中,每次迭代时,如果不把模型的梯度清零,会默认将模型每次迭代的梯度累加的。

标签:pgd,name,backup,self,param,fgm,源码,model,grad
来源: https://blog.csdn.net/qq_40176087/article/details/121512229