2021SC@SDUSC
#训练损失函数,用在训练时
train_criterion = SemiLoss()
#交叉熵损失, 用在验证集和测试集, 是模型训完完成后的,使用交叉熵进行计算损失
criterion = nn.CrossEntropyLoss()
根据前几篇博客对损失函数的介绍,在半监督文本分类该项目的代码中,其主要公式:
使用SemiLoss计算损失
class SemiLoss(object)
def __call__(self, outputs_x, targets_x, outputs_u, targets_u, outputs_u_2, epoch, mixed=1)
半监督损失函数
param outputs_x 模型输出的x
param targets_x 真实的x
param outputs_u 模型输出的无标签的x
param targets_u 真实的无标签的x
param outputs_u_2 模型输出的无标签x_2
param epoch 迭代次数
param mixed 是否是混合过的输出
return if args.mix_method == 0 or args.mix_method =