import torch
import torch.nn as nn
import torch.nn.functional as F
batchsize = 10
num_classes = 4
x = torch.randn((batchsize, num_classes))
# print(x)
target = torch.randint(num_classes, size=(batchsize,), dtype=torch.long)
print(target)
y = torch.zeros(batchsize, num_classes)
# print(y)
y[range(y.shape[0]), target] = 1
print(y)
def sigmoid(x):
return (1 + (-x).exp()).reciprocal()
def binary_cross_entropy(input, y):
return -(y * input.log() + (1 - y) * (1 - input).log()).mean()
pred = sigmoid(x)
loss = binary_cross_entropy(pred, y)
print(loss)
loss1 = F.binary_cross_entropy_with_logits(pred, y)
print(loss1)
用自定义的和函数库里面的数据结果有点不同,思路还是一样。
tensor([3, 1, 1, 0, 1, 1, 2, 1, 1, 0])
tensor([[0., 0., 0., 1.],
[0., 1., 0., 0.],
[0., 1., 0., 0.],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 1., 0., 0.],
[0., 1., 0., 0.],
[1., 0., 0., 0.]])
tensor(0.8219)
tensor(0.8455)
用numpy写
import numpy as np
import torch.nn.functional as F
import torch
batchsize = 10
num_classes = 4
x = np.random.rand(batchsize, num_classes)
# print(x)
target = np.random.randint(num_classes, size=(batchsize,))
print(target)
y = np.zeros([batchsize, num_classes])
# print(y)
y[range(batchsize), target] = 1
# print(y)
x = torch.from_numpy(x)
y = torch.from_numpy(y)
print(x)
print(y)
loss = F.binary_cross_entropy_with_logits(x, y)
print(loss.item())