探究binary cross entropy的计算细节

import torch
import random
import numpy as np

#设置随机种子
def setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True
setup_seed(12345)

#实验数据
import torch.nn.functional as F
import math

input = torch.randn((3, 2), requires_grad=True)
target = torch.tensor([[1., 0.],
        [1., 0.],
        [0., 1.]])
assert input.shape == target.shape
print("input ",input)
print("target ",target)

实验数据为

input  tensor([[ 1.4271, -1.8701],
        [-1.1962, -2.0440],
        [-0.4560, -1.4295]], requires_grad=True)
target  tensor([[1., 0.],
        [1., 0.],
        [0., 1.]])
# binary cross entropy的计算细节
def _cross_entropy(t,h):
    return -t*math.log(h)-(1-t)*math.log(1-h)

def sigmoid(x):
    return 1/(1+math.exp(-x))

def BCE(input,target):
    bce = 0
    m = 2*input.size(0)
    for i in range(input.size(0)):
        for j in range(input.size(1)):
            t = target[i][j]
            h = sigmoid(input[i][j])
            #print(h)
            bce += _cross_entropy(t,h)
    bce /= m
    return bce

bce = BCE(input,target)
print("bce ",bce)

采用自定义binary cross entropy函数的计算结果为

bce tensor(0.6793)

#调用pytorch库计算binary cross entropy
bce = F.binary_cross_entropy(F.sigmoid(input),target)
print("bce ",bce)

先调用sigmoid函数,再调用pytorch库的binary_cross_entropy函数的计算结果为

bce  tensor(0.6793, grad_fn=<BinaryCrossEntropyBackward>)

#调用pytorch库的binary_cross_entropy_with_logits函数的计算结果
bce = F.binary_cross_entropy_with_logits(input,target)
print("bce ",bce)

调用pytorch库的binary_cross_entropy_with_logits函数的计算结果为

bce  tensor(0.6793, grad_fn=<MeanBackward1>)

猜你喜欢

转载自blog.csdn.net/u012751110/article/details/104065259
今日推荐