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