我很困惑Py手电如何处理一个热向量。在此教程中,神经网络将产生一个单一热向量作为其输出.据我所知,本教程中神经网络的原理结构应如下所示:

然而,labels并不是单一的热向量格式.我得到以下size
print(labels.size())
print(outputs.size())
output>>> torch.Size([4])
output>>> torch.Size([4, 10])奇迹般地,我把outputs和labels传给了criterion=CrossEntropyLoss(),根本没有错误。
loss = criterion(outputs, labels) # How come it has no error?我的假设是:
也许labels会自动转换成一个热向量形式。因此,在将标签传递给丢失函数之前,我尝试将标签转换为一个热向量。
def to_one_hot_vector(num_class, label):
b = np.zeros((label.shape[0], num_class))
b[np.arange(label.shape[0]), label] = 1
return b
labels_one_hot = to_one_hot_vector(10,labels)
labels_one_hot = torch.Tensor(labels_one_hot)
labels_one_hot = labels_one_hot.type(torch.LongTensor)
loss = criterion(outputs, labels_one_hot) # Now it gives me error但是,我得到了以下错误
/opt/pytorch/pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15不支持RuntimeError:多目标
那么,在Pytorch中不支持单一热向量?Pytorch如何计算两个张量outputs = [1,0,0],[0,0,1]和labels = [0,2]的cross entropy?现在对我来说一点意义都没有。
发布于 2019-04-06 14:15:44
PyTorch在其CrossEntropyLoss文档中指出
这个准则要求一个类索引(0到C1)作为一个一维小批张量的每个值的目标。
换句话说,它在概念上将to_one_hot_vector函数构建在CEL中,而不公开单一热API。注意,与存储类标签相比,一个热向量的内存效率很低。
如果给出了一个热向量,并且需要使用类标签格式(例如,为了与CEL兼容),您可以使用argmax,如下所示:
import torch
labels = torch.tensor([1, 2, 3, 5])
one_hot = torch.zeros(4, 6)
one_hot[torch.arange(4), labels] = 1
reverted = torch.argmax(one_hot, dim=1)
assert (labels == reverted).all().item()发布于 2019-09-14 18:20:41
此代码将帮助您同时使用一个热编码和多个热编码。
import torch
batch_size=10
n_classes=5
target = torch.randint(high=5, size=(1,10)) # set size (2,10) for MHE
print(target)
y = torch.zeros(batch_size, n_classes)
y[range(y.shape[0]), target]=1
yOHE的输出
tensor([[4, 3, 2, 2, 4, 1, 1, 1, 4, 2]])
tensor([[0., 0., 0., 0., 1.],
[0., 0., 0., 1., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 1.],
[0., 1., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 1.],
[0., 0., 1., 0., 0.]])设置target = torch.randint(high=5, size=(2,10))时MHE的输出
tensor([[3, 2, 4, 4, 2, 4, 0, 4, 4, 1],
[4, 1, 1, 3, 2, 2, 4, 2, 4, 3]])
tensor([[0., 0., 0., 1., 1.],
[0., 1., 1., 0., 0.],
[0., 1., 0., 0., 1.],
[0., 0., 0., 1., 1.],
[0., 0., 1., 0., 0.],
[0., 0., 1., 0., 1.],
[1., 0., 0., 0., 1.],
[0., 0., 1., 0., 1.],
[0., 0., 0., 0., 1.],
[0., 1., 0., 1., 0.]])如果你需要多个OHE:
torch.nn.functional.one_hot(target)
tensor([[[0, 0, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 1],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 1],
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 1],
[0, 1, 0, 0, 0]],
[[0, 0, 0, 0, 1],
[0, 1, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 1, 0]]])发布于 2021-12-22 08:46:59
正如@Jatentaki明确指出的那样,您可以使用torch.argmax(one_hot, dim=1)将一个热编码向量转换为数字。
但是,如果您仍然希望使用PyTorch中的一次热编码输出来训练您的网络,则可以使用nn.LogSoftmax和NLLLOSS。
import torch
from torch import nn
output_onehot = nn.LogSoftmax(dim=1)(torch.randn(3, 5)) # m = 3 samples, each has n = 5 features
target = torch.tensor([1, 0, 4]) # target values for each sample
nn.NLLLoss()(output_onehot, target)
print(output_onehot)
print(target)
# You can get the probabilities using the exponential function:
print("Probabilities:", torch.exp(output_onehot))输出将如下所示:
tensor([[-0.5413, -2.4461, -2.0110, -1.9964, -2.7851],
[-2.3376, -1.6985, -1.8472, -3.0975, -0.6585],
[-3.2820, -0.7160, -1.5297, -1.5636, -3.0412]])
tensor([1, 0, 4])
Probabilities: tensor([[0.5820, 0.0866, 0.1339, 0.1358, 0.0617],
[0.0966, 0.1830, 0.1577, 0.0452, 0.5176],
[0.0376, 0.4887, 0.2166, 0.2094, 0.0478]])https://stackoverflow.com/questions/55549843
复制相似问题