Pytorch复习笔记--pytorch常见交叉熵函数的实现

1. nn.CrossEntropyLoss()

计算公式如下:
L o s s ( x , c l a s s ) = − l n ( e x [ c l a s s ] ∑ i e x [ i ] ) = − x [ c l a s s ] + l n ( ∑ i e x [ i ] ) Loss(x, class) = -ln(\frac{e^{x[class]}}{\sum_{i}e^{x[i]}}) = -x[class] + ln(\sum_{i}e^{x[i]}) Loss(x,class)=−ln(∑iex[i]ex[class])=−x[class]+ln(i∑ex[i])

代码实现如下:

python 复制代码
import torch
import torch.nn as nn
import math
import numpy as np

def cross_entorpy(logits, labels):
    loss = 0
    batch = len(labels)
    for b_idx in range(batch):
        hou = 0
        for j in logits[b_idx]: # 计算累加部分
            hou += np.exp(j)
        loss += -logits[b_idx][labels[b_idx]] + np.log(hou) # -logits[b_idx][labels[b_idx]]表示计算-x[class]
    return np.around(loss / batch, 4) # 保留四位小数

if __name__ == "__main__":
    entroy = nn.CrossEntropyLoss()
    logits = torch.Tensor([[0.1234, 0.5555,0.3211], [0.1234, 0.5555,0.3211], [0.1234, 0.5555,0.3211]])
    labels = torch.tensor([0, 1, 2])
    loss1 = entroy(logits, labels) # 调用pytorch接口
    print("loss1: ", loss1) # tensor(1.1142)
    
    logits = np.array(logits)
    labels = np.array(labels)
    loss2 = cross_entorpy(logits, labels) # 调用自定义函数
    print("loss2: ", loss2) # 1.1142
    
    print("All Done!")

2. nn.BCELoss()

计算公式如下:
L o s s ( x , y ) = − 1 n ∑ i n ( y i ∗ l n ( x i ) + ( 1 − y i ) ∗ l n ( 1 − x i ) ) Loss(x, y) = -\frac{1}{n}\sum_{i}^{n}(y_{i}*ln(x_{i}) + (1-y_{i})*ln(1 - x_{i})) Loss(x,y)=−n1i∑n(yi∗ln(xi)+(1−yi)∗ln(1−xi))

代码实现如下:

python 复制代码
import torch
import torch.nn as nn
import math
import numpy as np

def BCE_loss(logits, labels):
    func = nn.Sigmoid()
    logits = func(logits)
    batch = logits.shape[0]
    Num_class = logits.shape[1]
    total_loss = 0
    for b_idx in range(batch):
        single_sample_loss = 0
        for j in range(Num_class):
            single_sample_loss += labels[b_idx][j].item() * math.log(logits[b_idx][j].item()) + (1 - labels[b_idx][j].item()) * math.log(1 - logits[b_idx][j].item())
        total_loss += single_sample_loss / Num_class
        
    loss = -1 * (total_loss / batch)
    return np.around(loss, 4)        

if __name__ == "__main__":
    BCEloss = nn.BCELoss()
    func = nn.Sigmoid()
    logits = torch.Tensor([[1.1234, 1.5555, 1.3211], [1.1234, 1.5555, 1.3211], [1.1234, 1.5555, 1.3211]])
    labels = torch.Tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) # 转换成one-hot的形式
    loss1 = BCEloss(func(logits), labels) # 调用nn.BCELoss()时,logits的数值必须在区间(0, 1)之间
    print("loss1: ", loss1) # tensor(1.1254)
    
    loss2 = BCE_loss(logits, labels)
    print("loss2: ", loss2) # 1.1254
    
    print("All Done!")

3. nn.BCEWithLogitsLoss()

计算公式如下:
L o s s ( x , y ) = − 1 n ∑ i n ( y i ∗ l n ( x i ) + ( 1 − y i ) ∗ l n ( 1 − x i ) ) Loss(x, y) = -\frac{1}{n}\sum_{i}^{n}(y_{i}*ln(x_{i}) + (1-y_{i})*ln(1 - x_{i})) Loss(x,y)=−n1i∑n(yi∗ln(xi)+(1−yi)∗ln(1−xi))

nn.BCEWithLogitsLoss() 和 nn.BCELoss()的区别在于nn.BCEWithLogitsLoss()自带Sigmoid()函数来处理输入。

代码实现如下:

python 复制代码
import torch
import torch.nn as nn
import math
import numpy as np

def BCE_loss(logits, labels):
    func = nn.Sigmoid()
    logits = func(logits)
    batch = logits.shape[0]
    Num_class = logits.shape[1]
    total_loss = 0
    for b_idx in range(batch):
        single_sample_loss = 0
        for j in range(Num_class):
            single_sample_loss += labels[b_idx][j].item() * math.log(logits[b_idx][j].item()) + (1 - labels[b_idx][j].item()) * math.log(1 - logits[b_idx][j].item())
        total_loss += single_sample_loss / Num_class
        
    loss = -1 * (total_loss / batch)
    return np.around(loss, 4)        

if __name__ == "__main__":
    BCEWithLogitsLoss = nn.BCEWithLogitsLoss() # 自带Sigmoid()函数
    logits = torch.Tensor([[1.1234, 1.5555, 1.3211], [1.1234, 1.5555, 1.3211], [1.1234, 1.5555, 1.3211]])
    labels = torch.Tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) # 转换成one-hot的形式
    loss1 = BCEWithLogitsLoss(logits, labels) # 调用nn.BCELoss()时,logits的数值必须在区间(0, 1)之间
    print("loss1: ", loss1) # tensor(1.1254)
    
    loss2 = BCE_loss(logits, labels)
    print("loss2: ", loss2) # 1.1254
    
    print("All Done!")
相关推荐
love_and_hope9 小时前
Pytorch学习--神经网络--搭建小实战(手撕CIFAR 10 model structure)和 Sequential 的使用
人工智能·pytorch·python·深度学习·学习
好喜欢吃红柚子16 小时前
万字长文解读空间、通道注意力机制机制和超详细代码逐行分析(SE,CBAM,SGE,CA,ECA,TA)
人工智能·pytorch·python·计算机视觉·cnn
羊小猪~~17 小时前
神经网络基础--什么是正向传播??什么是方向传播??
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
写代码的小阿帆20 小时前
pytorch实现深度神经网络DNN与卷积神经网络CNN
pytorch·cnn·dnn
丕羽1 天前
【Pytorch】基本语法
人工智能·pytorch·python
Shy9604182 天前
Pytorch实现transformer语言模型
人工智能·pytorch
周末不下雨2 天前
跟着小土堆学习pytorch(六)——神经网络的基本骨架(nn.model)
pytorch·神经网络·学习
蜡笔小新星2 天前
针对初学者的PyTorch项目推荐
开发语言·人工智能·pytorch·经验分享·python·深度学习·学习
矩阵猫咪2 天前
【深度学习】时间序列预测、分类、异常检测、概率预测项目实战案例
人工智能·pytorch·深度学习·神经网络·机器学习·transformer·时间序列预测
zs1996_2 天前
深度学习注意力机制类型总结&pytorch实现代码
人工智能·pytorch·深度学习