Logloss详解

定义:

− ( y log ⁡ ( p ) + ( 1 − y ) log ⁡ ( 1 − p ) ) -{(y\log(p) + (1 - y)\log(1 - p))} (ylog(p)+(1y)log(1p))

y y y表示样本的真实标签(1或0), p p p表示模型预测为正样本的概率。

可视化:

下图展示了lable=1时对数损失值的范围。当预测概率接近1时,对数损失缓慢下降。但随着预测概率的降低,对数损失迅速增加。对数损失对两种类型的错误都会进行处罚,尤其是那些置信度很高的错误预测!
在这里插入图片描述

Code:

def logloss(true_label, predicted_prob):
  if true_label == 1:
    return -log(predicted_prob)
  else:
    return -log(1 - predicted_prob)

一个样本集里正样本出现的概率为p,如果我们把每个样本的预测值都置为p,那么logloss是多少呢?

很显然
若p=0.1,logloss=0.325

若p=0.2,logloss=0.500

若p=0.3,logloss=0.611

若p=0.4,logloss=0.673

若p=0.5,logloss=0.693

若p=0.6,logloss=0.673

若p=0.7,logloss=0.611

若p=0.8,logloss=0.500

若p=0.9,logloss=0.325

所以最差的情况就是,正好是一半正样本一半负样本,此时你乱猜出的logloss是0.693。

所以只要loglss是在0.693以上,就说明模型是失败的。

Reference:

http://wiki.fast.ai/index.php/Log_Loss

http://sofasofa.io/forum_main_post.php?postid=1000508

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
yolov5 loss.py 代码详解 yolov5 loss.py 是 YOLOv5 模型中的一个关键文件,主要负责计算模型的损失函数。下面是该文件的代码详解: 1. 导入必要的库 ```python import torch import torch.nn.functional as F from torch import nn ``` 2. 定义损失函数类 ```python class YOLOv5Loss(nn.Module): def __init__(self, anchors, strides, iou_threshold, num_classes, img_size): super(YOLOv5Loss, self).__init__() self.anchors = anchors self.strides = strides self.iou_threshold = iou_threshold self.num_classes = num_classes self.img_size = img_size ``` 该类继承自 nn.Module,包含了一些必要的参数,如 anchors,strides,iou_threshold,num_classes 和 img_size。 3. 定义计算损失函数的方法 ```python def forward(self, x, targets=None): bs, _, ny, nx = x.shape # batch size, channels, grid size na = self.anchors.shape[] # number of anchors stride = self.img_size / max(ny, nx) # compute stride yolo_out, grid = [], [] for i in range(3): yolo_out.append(x[i].view(bs, na, self.num_classes + 5, ny, nx).permute(, 1, 3, 4, 2).contiguous()) grid.append(torch.meshgrid(torch.arange(ny), torch.arange(nx))) ny, nx = ny // 2, nx // 2 loss, nGT, nCorrect, mask = , , , torch.zeros(bs, na, ny, nx) for i in range(3): y, g = yolo_out[i], grid[i] y[..., :2] = (y[..., :2].sigmoid() + g) * stride # xy y[..., 2:4] = y[..., 2:4].exp() * self.anchors[i].to(x.device) # wh y[..., :4] *= mask.unsqueeze(-1).to(x.device) y[..., 4:] = y[..., 4:].sigmoid() if targets is not None: na_t, _, _, _, _ = targets.shape t = targets[..., 2:6] * stride gxy = g.unsqueeze().unsqueeze(-1).to(x.device) gi, gj = gxy[..., ], gxy[..., 1] b = t[..., :4] iou = box_iou(b, y[..., :4]) # iou iou_max, _ = iou.max(2) # Match targets to anchors a = torch.arange(na_t).view(-1, 1).repeat(1, na) t = targets[a, iou_max >= self.iou_threshold] # select targets # Compute losses if len(t): # xy loss xy = y[..., :2] - gxy xy_loss = (torch.abs(xy) - .5).pow(2) * mask.unsqueeze(-1).to(x.device) # wh loss wh = torch.log(y[..., 2:4] / self.anchors[i].to(x.device) + 1e-16) wh_loss = F.huber_loss(wh, t[..., 2:4], reduction='none') * mask.unsqueeze(-1).to(x.device) # class loss tcls = t[..., ].long() tcls_onehot = torch.zeros_like(y[..., 5:]) tcls_onehot[torch.arange(len(t)), tcls] = 1 cls_loss = F.binary_cross_entropy(y[..., 5:], tcls_onehot, reduction='none') * mask.unsqueeze(-1).to(x.device) # objectness loss obj_loss = F.binary_cross_entropy(y[..., 4:5], iou_max.unsqueeze(-1), reduction='none') * mask.to(x.device) # total loss loss += (xy_loss + wh_loss + cls_loss + obj_loss).sum() nGT += len(t) nCorrect += (iou_max >= self.iou_threshold).sum().item() mask = torch.zeros(bs, na, ny, nx) if targets is not None: t = targets[..., 2:6] * stride gi, gj = g[..., ], g[..., 1] a = targets[..., 1].long() mask[torch.arange(bs), a, gj, gi] = 1 return loss, nGT, nCorrect ``` 该方法接受输入 x 和 targets,其中 x 是模型的输出,targets 是真实标签。该方法首先根据输入 x 的形状计算出 batch size,channels,grid size 和 number of anchors 等参数,然后根据这些参数计算出 stride 和 grid。接着,该方法将输入 x 分成三个部分,每个部分都包含了 na 个 anchors 和 self.num_classes + 5 个通道。然后,该方法将每个部分的输出转换成合适的形状,并计算出每个 anchor 的中心点坐标和宽高。接着,该方法根据 targets 计算出损失函数,包括 xy loss,wh loss,class loss 和 objectness loss。最后,该方法返回损失函数的值,以及 nGT 和 nCorrect。 4. 定义计算 box iou 的方法 ```python def box_iou(box1, box2): """ Returns the IoU of two bounding boxes """ b1_x1, b1_y1, b1_x2, b1_y2 = box1[..., ], box1[..., 1], box1[..., 2], box1[..., 3] b2_x1, b2_y1, b2_x2, b2_y2 = box2[..., ], box2[..., 1], box2[..., 2], box2[..., 3] inter_rect_x1 = torch.max(b1_x1, b2_x1) inter_rect_y1 = torch.max(b1_y1, b2_y1) inter_rect_x2 = torch.min(b1_x2, b2_x2) inter_rect_y2 = torch.min(b1_y2, b2_y2) inter_area = torch.clamp(inter_rect_x2 - inter_rect_x1 + 1, min=) * torch.clamp(inter_rect_y2 - inter_rect_y1 + 1, min=) b1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1) b2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1) iou = inter_area / (b1_area + b2_area - inter_area + 1e-16) return iou ``` 该方法接受两个参数 box1 和 box2,分别表示两个 bounding box 的坐标。该方法首先计算出两个 bounding box 的交集和并集,然后计算出它们的 IoU。 以上就是 yolov5 loss.py 代码的详解
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值