交叉熵的数学推导和手撕代码 数学推导 手撕代码 数学推导 手撕代码 import torch import torch.nn.functional as F # 二元交叉熵损失函数 def binary_cross_entropy(predictions, targets): # predictions应为sigmoid函数的输出,即概率值 # targets应为0或1的二进制标签 loss = -torch.mean(targets * torch.log(predictions) + (1 - targets) * torch.log(1 - predictions)) r