当前位置:网站首页>pytorch implementation of Poly1CrossEntropyLoss
pytorch implementation of Poly1CrossEntropyLoss
2022-08-09 04:19:00 【Aldersw】
代码改自github
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class Poly1CrossEntropyLoss(nn.Module):
def __init__(self,
num_classes: int,
epsilon: float = 1.0,
reduction: str = "none"):
""" Create instance of Poly1CrossEntropyLoss :param num_classes: :param epsilon: :param reduction: one of none|sum|mean, apply reduction to final loss tensor """
super(Poly1CrossEntropyLoss, self).__init__()
self.num_classes = num_classes
self.epsilon = epsilon
self.reduction = reduction
return
def forward(self, output, labels):
""" Forward pass :param output: tensor of shape [N, num_classes] :param labels: tensor of shape [N] or [N, num_classes] :return: poly cross-entropy loss """
#timm如果有使用cutmix或者mixup,then you will getlabels是[batchsize,classnums]维度的矩阵,因为获取的label不是hard label,但也不算是smooth label,corresponding to each datalabel类似于这种[0,0,0.65,0,0,0.35,0,...]
if labels.ndim == 1:
labels_onehot = F.one_hot(labels, num_classes=self.num_classes).to(device=output.device,dtype=output.dtype)
else:
labels=labels.to(device=output.device)
pt = torch.sum(labels * F.softmax(output, dim=-1), dim=-1)#64,1
CE = F.cross_entropy(input=output, target=labels, reduction='none')
# print(CE.shape)#64
poly1 = CE + self.epsilon * (1 - pt)
if self.reduction == "mean":
poly1 = poly1.mean()
elif self.reduction == "sum":
poly1 = poly1.sum()
#This averaging operation is added to obtain the average of each dataloss,If not added, the length is batchsize的一维矩阵,Each data is stored separatelyloss
#poly1 = poly1.mean()
return poly1
边栏推荐
猜你喜欢
随机推荐
全栈代码测试覆盖率及用例发现系统的建设和实践
助力To B业务,这类企业端数据值得风控童鞋关注
2022 High Voltage Electrician Exam Questions and Answers
MySql.Data.MySqlClient.DBNull
阿里云天池大赛赛题(机器学习)——工业蒸汽量预测(完整代码)
了解CV和RoboMaster视觉组(五)目标跟踪:基于深度学习的方法
Alibaba Cloud Tianchi Contest Question (Machine Learning) - Repeat Purchase Prediction of Tmall Users (Complete Code)
简单的数学公式计算
了解CV和RoboMaster视觉组(五)滤波器、观测器和预测方法:维纳滤波器Wiener Filter,LMS
整数倍数数列
两种K线形态预示今日伦敦银走向
Gopacket source code analysis
串扰与防护
【周赛复盘】力扣第 305 场单周赛
机器人工程师入门知识框架(思维导图)
为什么有的时间函数在同一事务内返回的都是同一值?
"IP" command to configure network interface
单根k线图知识别以为自己都懂了
了解CV和RoboMaster视觉组(五)滤波器、观测器和预测方法:自适应滤波器
技术分享 | 如何模拟真实使用场景?mock 技术来帮你