Pytorch不支持一键热矢量?

时间:2019-04-06 14:02:53

标签: python machine-learning pytorch

我对Pytorch如何处理一键向量感到非常困惑。在此tutorial中,神经网络将生成一个热向量作为其输出。据我了解,本教程中神经网络的示意图结构应为:

enter image description here

但是,Dim ie As New InternetExplorer, ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") With ie .Visible = True .Navigate2 "" & Sheets("Home").Range("C3").Text While .Busy Or .readyState < 4: DoEvents: Wend Sheets("Unit Data").Select Dim listings As Object, listing As Object, headers(), results() Dim r As Long, list As Object, item As Object headers = Array("size", "features") Set list = .document.getElementsByClassName("units-table main") '.unit_size medium, .features, .promo_offers, .board_rate_wrapper p, .board_rate Dim rowCount As Long rowCount = .document.querySelectorAll(".units-table main li").Length ReDim results(1 To rowCount, 1 To UBound(headers) + 1) For Each listing In list For Each item In listing.getElementsByTagName("li") r = r + 1 On Error Resume Next results(r, 1) = item.getElementsByClassName("container size")(0).innerText results(r, 2) = item.getElementsByClassName("description")(0).innerText On Error GoTo 0 Next Next ws.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers ws.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results .Quit End With 并不是一键矢量格式。我得到以下import PDFKit let pdfDocument = PDFDocument(url: pdfUrl)! let thumb = pdfDocument.page(at: pageIndex)?.thumbnail(of: size, for: .mediaBox)

labels

奇迹般的,我将sizeprint(labels.size()) print(outputs.size()) output>>> torch.Size([4]) output>>> torch.Size([4, 10]) 传递给outputs,根本没有错误。

labels

我的假设:

也许pytorch会自动将criterion=CrossEntropyLoss()转换为一键矢量形式。因此,在将标签传递给损失函数之前,我尝试将标签转换为一键矢量。

loss = criterion(outputs, labels) # How come it has no error?

但是,出现以下错误

  

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

因此,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 如何计算两个张量PytorchPytorch的{​​{1}}?此刻对我来说根本没有意义。

2 个答案:

答案 0 :(得分:2)

我很困惑你的困惑。 PyTorch在其CrossEntropyLoss的文档中明确指出

  

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

换句话说,它具有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()

答案 1 :(得分:1)

此代码将帮助您同时进行一种热编码多种热编码

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
y

OHE中的输出

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]]])