pytorch实现cross entropy函数

转onehot格式

1.首先用到scatter函数(scatter_原地改变数值)

    >>> x = torch.rand(2, 5)
    >>> x
    tensor([[ 0.3992,  0.2908,  0.9044,  0.4850,  0.6004],
            [ 0.5735,  0.9006,  0.6797,  0.4152,  0.1732]])
    ## 下面0为dim表示行index,1表示按列进行赋值
    >>> torch.zeros(3, 5).scatter_(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1
, 2]]), x)
    tensor([[ 0.3992,  0.9006,  0.6797,  0.4850,  0.6004],
            [ 0.0000,  0.2908,  0.0000,  0.4152,  0.0000],
            [ 0.5735,  0.0000,  0.9044,  0.0000,  0.1732]])

    >>> z = torch.zeros(2, 4).scatter_(1, torch.tensor([[2], [3]]), 1.23)
    >>> z
    tensor([[ 0.0000,  0.0000,  1.2300,  0.0000],
            [ 0.0000,  0.0000,  0.0000,  1.2300]])

In []: z = torch.zeros(2, 4).scatter_(1, torch.tensor([[1, 2], [1, 3]]), 1.23
    ...: )

In []: z
Out[]:
tensor([[0.0000, 1.2300, 1.2300, 0.0000],
        [0.0000, 1.2300, 0.0000, 1.2300]])

具体代码为

label = torch.tensor([0,0,1])
onehot_ = torch.FloatTensor(label.shape[0], 3)
onehot.zero_()
onehot.scatter_(1, torch.reshape(label, (3,1)), 1)

计算crosstropy

import torch
from torch import nn

lable = torch.tensor([0,0,1])

fc_out = torch.tensor(
[
    [2.5, -2, 0.8989],
    [3, 0.8, -865],
    [0.00000000000001, 2, 4.9]
])

class CrossEntropyLoss(nn.Module):
    def __init__(self):
        super(CrossEntropyLoss, self).__init__()

    def forward(self, fc_out, label):
        one_hot_lable = torch.FloatTensor(fc_out.shape[0], 3)
        one_hot_lable.zero_()
        one_hot_lable.scatter_(1, torch.reshape(lable, (fc_out.shape[0], 1)), 1)
        loss = one_hot_lable * torch.softmax(fc_out, 1)
        loss = -torch.sum(torch.log(torch.sum(loss, 1)))/fc_out.shape[0]

        return loss


loss = torch.nn.CrossEntropyLoss()
loss1 = CrossEntropyLoss()
l = loss(fc_out, lable)
l2 = loss1(fc_out, lable)

print(l)
print(l2)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值