论文链接:https://arxiv.org/abs/2002.03662
import torch
import torch.nn.functional as F
class DDL(torch.nn.Module):
""" Implented of
"""
def __init__(self, pos_kl_weight=0.1, neg_kl_weight=0.02,
order_loss_weight=0.5, positive_threshold=0.0):
""" Args:
pos_kl_weight: weight for positive kl loss
neg_kl_weight: weight for negative kl loss
order_loss_weight: weight for order loss
positive_threshold: threshold fo positive pair
"""
super(DDL, self).__init__()
self.pos_kl_weight = pos_kl_weight
self.neg_kl_weight = neg_kl_weight
self.order_loss_weight = order_loss_weight
self.positive_threshold = positive_threshold
# register_buffer:在内存中定一个常量,同时,模型保存和加载的时候可以写入和读出。
# 该方法的作用是定义一组参数,该组参数的特别之处在于:模型训练时不会更新(即调用 optimizer.step() 后该组参数不会变化,只可人为地改变它们的值)
# 保存模型时,该组参数又作为模型参数不可或缺的一部分被保存。
self.register_buffer('t', torch.arange(0, 1.0, 0.001).view(-1, 1).t()) # 分配直方图间隔
def forward(self, neg_features, pos_pair_features_first, pos_pair_features_second):
# 教师(简单样本)和学生(难分样本)均有b个正样本对(2b个样本)、b个不同身份的样本
# sample b positive pairs (i.e., 2b samples) and b samples with different identities