Pytorch不支持一键热矢量? [英] Pytorch doesn't support one-hot vector?

查看:71
本文介绍了Pytorch不支持一键热矢量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Pytorch如何处理一键矢量感到非常困惑.在此

但是,标签并非一站式矢量格式.我得到以下 size

  print(labels.size())打印(outputs.size())输出>>>火炬大小([4])输出>>>torch.Size([4,10]) 

奇迹般地,我将输出 labels 传递给 criterion = CrossEntropyLoss(),完全没有错误.

  loss =条件(输出,标签)#为什么没有错误? 

我的假设:

也许 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返回blabels_one_hot = to_one_hot_vector(10,labels)labels_one_hot =火炬张量(labels_one_hot)labels_one_hot = labels_one_hot.type(torch.LongTensor)损失=标准(输出,labels_one_hot)#现在它给我错误 

但是,出现以下错误

RuntimeError:在以下位置不支持多目标/opt/pytorch/pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15

那么, Pytorch 中不支持一键矢量吗?pytorch 如何计算两个张量 outputs = [1,0,0],[0,0,1] labels = [0,2] 吗?此刻对我来说根本没有意义.

解决方案

PyTorch在其 CrossEntropyLoss 那个

此标准期望将类别索引(0到C-1)作为大小为minibatch的一维张量的每个值的目标

换句话说,它具有概念上内置在 CEL 中的 to_one_hot_vector 函数,并且不公开一键式API.请注意,与存储类标签相比,一键通"向量的内存效率低.

如果为您提供了一个单向矢量,并且需要使用类标签格式(例如,与 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)断言(标签==恢复).all().item() 

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆