神经网络与深度学习(邱锡鹏)编程练习4 FNN 交叉熵-二分类 numpy vs. pytorch

import numpy as np
import torch
import torch.nn as nn


def cross_entropy_error(y, t):
    delta = 1e-7  # 添加一个微小值可以防止负无限大(np.log(0))的发生。
    return -np.sum(t * np.log(y + delta))


input = torch.Tensor([[0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]])
target = torch.tensor([[0.0, 0, 1, 0, 0, 0, 0, 0, 0, 0]])

loss = nn.BCELoss(reduction='none')  # pytorch
lossinput = torch.sigmoid(input)  # 使用BCELoss前,要用sigmoid
output = loss(lossinput, target)
print("nn.BCELoss计算loss的结果:", output)
print()  # pytorch

print("cross_entropy_error计算的loss:")
print(cross_entropy_error(lossinput.data.numpy(), target.data.numpy()))  # numpy

posted on 2022-06-05 23:14  HBU_DAVID  阅读(167)  评论(0编辑  收藏  举报

导航