B C E L o s s ( 二 分 类 交 叉 熵 损 失 ) BCELoss(二分类交叉熵损失) BCELoss(二分类交叉熵损失)
def clip_by_tensor(t,t_min,t_max):
t=t.float()
result = (t >= t_min).float() * t + (t < t_min).float() * t_min
result = (result <= t_max).float() * result + (result > t_max).float() * t_max
return result
def BCELoss(pred,target):
epsilon = 1e-7
pred = clip_by_tensor(pred, epsilon, 1.0 - epsilon) # 不要 0和1,要么是接近0的数,要么是接近1的数
output = -target * torch.log(pred) - (1.0 - target) * torch.log(1.0 - pred)
return output