Pytorch 不支持单热向量? [英] Pytorch doesn't support one-hot vector?
问题描述
我对 Pytorch 如何处理 one-hot 向量感到非常困惑.在这个
然而,labels
不是单热矢量格式.我得到以下 size
print(labels.size())打印(输出.大小())输出>>>火炬.大小([4])输出>>>torch.Size([4, 10])
奇迹般地,我将 outputs
和 labels
传递给 criterion=CrossEntropyLoss()
,完全没有错误.
loss = criteria(outputs, labels) # 怎么没有错误?
我的假设:
也许 pytorch 会自动将 labels
转换为 one-hot 矢量形式.因此,我尝试将标签转换为单热向量,然后再将其传递给损失函数.
def to_one_hot_vector(num_class, label):b = np.zeros((label.shape[0], num_class))b[np.arange(label.shape[0]), label] = 1返回 blabel_one_hot = to_one_hot_vector(10,labels)label_one_hot = torch.Tensor(labels_one_hot)label_one_hot = labels_one_hot.type(torch.LongTensor)loss =criteria(outputs, labels_one_hot) # 现在它给了我错误
但是,我收到以下错误
<块引用>运行时错误:不支持多目标/opt/pytorch/pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15
那么,Pytorch
不支持单热向量?Pytorch
如何计算两个张量 outputs = [1,0,0],[0,0,1]
的 cross entropy
和labels = [0,2]
?目前对我来说完全没有意义.
PyTorch 在其 CrossEntropyLoss
那个
该标准期望一个类索引(0 到 C-1)作为 minibatch 大小的一维张量的每个值的目标
换句话说,它有你的 to_one_hot_vector
函数在概念上内置在 CEL
中,并且没有公开 one-hot API.请注意,与存储类标签相比,one-hot 向量的内存效率较低.
如果你得到 one-hot 向量并且需要使用类标签格式(例如与 CEL
兼容),你可以使用 argmax
,如下所示:
导入火炬标签 = torch.tensor([1, 2, 3, 5])one_hot = torch.zeros(4, 6)one_hot[torch.arange(4), 标签] = 1恢复 = torch.argmax(one_hot,dim=1)断言(标签==恢复).所有().项目()
I am very confused by how Pytorch deals with one-hot vectors. In this tutorial, the neural network will generate a one-hot vector as its output. As far as I understand, the schematic structure of the neural network in the tutorial should be like:
However, the labels
are not in one-hot vector format. I get the following size
print(labels.size())
print(outputs.size())
output>>> torch.Size([4])
output>>> torch.Size([4, 10])
Miraculously, I they pass the outputs
and labels
to criterion=CrossEntropyLoss()
, there's no error at all.
loss = criterion(outputs, labels) # How come it has no error?
My hypothesis:
Maybe pytorch automatically convert the labels
to one-hot vector form. So, I try to convert labels to one-hot vector before passing it to the loss function.
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
However, I got the following error
RuntimeError: multi-target not supported at /opt/pytorch/pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15
So, one-hot vectors are not supported in Pytorch
? How does Pytorch
calculates the cross entropy
for the two tensor outputs = [1,0,0],[0,0,1]
and labels = [0,2]
? It doesn't make sense to me at all at the moment.
PyTorch states in its documentation for CrossEntropyLoss
that
This criterion expects a class index (0 to C-1) as the target for each value of a 1D tensor of size minibatch
In other words, it has your to_one_hot_vector
function conceptually built in CEL
and does not expose the one-hot API. Notice that one-hot vectors are memory inefficient compared to storing class labels.
If you are given one-hot vectors and need to go to class labels format (for instance to be compatible with CEL
), you can use argmax
like below:
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()
这篇关于Pytorch 不支持单热向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!