import torch.nn as nn
nn.BCELoss((weight=None, size_average=None, reduce=None, reduction=‘mean’))
一、torch.nn.BCELoss()
介绍
BCELoss()
是计算目标值和预测值之间的二进制交叉熵损失函数。其公式如下:
l n = − w n ⋅ [ y n ⋅ l o g x n + ( 1 − y n ) ⋅ l o g ( 1 − x n ) ] l_n=-w_n·[{y_n·logx_n}+{(1-y_n)·log(1-x_n)}] ln=−wn⋅[yn⋅logxn+(1−yn)⋅log(1−xn)]
其中, w n w_n wn表示权重矩阵, x n x_n xn表示预测值矩阵(输入矩阵被激活函数处理后的结果), y n y_n yn表示目标值矩阵。(注意, l o g log log以 e e e为底,即数学中的 l n ln ln)
二、torch.nn.BCELoss()
应用
代码:
import torch
import torch.nn as nn
weights=torch.tensor([[1, 1, 0],
[1, 1, 1],
[1, 1, 1]])
m = nn.Sigmoid()
loss = nn.BCELoss(weight=weights,reduction='none')
input = torch.tensor([[-0.1514, 0.0744, -1.5716],
[-0.3198, -1.2424, -1.4921],
[ 0.5548, 0.8131, 1.0369]], requires_grad=True)
target = torch.tensor([[0., 1., 0.],
[0., 1., 1.],
[0., 0., 0.]])
output = loss(m(input), target)
print(m(input)) #被激活函数处理的输入矩阵
print(target) #目标值矩阵
print(weights) #权重矩阵
print(output) #损失值矩阵
运行结果:
tensor([[0.4622, 0.5186, 0.1720],
[0.4207, 0.2240, 0.1836],
[0.6352, 0.6928, 0.7383]], grad_fn=<SigmoidBackward>)
tensor([[0., 1., 0.],
[0., 1., 1.],
[0., 0., 0.]])
tensor([[1, 1, 0],
[1, 1, 1],
[1, 1, 1]])
tensor([[0.6203, 0.6566, 0.0000],
[0.5460, 1.4960, 1.6950],
[1.0085, 1.1802, 1.3404]], grad_fn=<BinaryCrossEntropyBackward>)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章