Python learning notes (5) — cross entropy error runtimeerror: 1D target tensor expected, multi target not supported

When I use cross entropy as the loss function, an error occurs:

RuntimeError: 1D target tensor expected, multi-target not supported

I checked the relevant information, and the statements in it are basically:

    the dimension of the input labels should be 1, and the precision cannot be double. It must be replaced by long; Dimensionality reduction of the input label

    But it can’t solve my problem, because my tag data has been processed with the following code after processing:


    And I also printed the dimension of my label data:

    torch.Size([16, 11])

    Here 16 refers to batch_ Size , so it’s not a dimension problem.

    But I was inspired when I read this blog (runtimeerror: multi target not supported at). It says:

    When calculating the cross entropy loss function in pytorch, the correct label input cannot be in one hot format. The function will process itself into one hot format. Therefore, you do not need to enter [0 1], just enter 4.

    My tag data is a multi tag problem, as follows:

    tensor([0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0])

    Then, when passing through loss , crossentropyloss will automatically code it as one-hot , which will increase it by one dimension to:

    tensor([[1., 0.],
            [0., 1.],
            [1., 0.],
            [1., 0.],
            [0., 1.],
            [1., 0.],
            [1., 0.],
            [0., 1.],
            [0., 1.],
            [1., 0.],
            [1., 0.]])

    This leads to the error.

    Therefore, the solution is to use the loss function of the multi label problem. For example, multilabelsoftmarginloss , or the most original mselos .

    reference resources

    [1] Wang’s technical road. Runtimeerror: multi target not supported at [EB/OL]. (December 10, 2019) [October 27, 2021]
    [2] Python free. Solution of “one-dimensional target tensor expectation, multi-objective unsupported” in cross entropy loss function, calculation, lossfunction, error report, 1dtargettensorexpected, multitargetnotsupported, Solution [EB/OL] (2020-07-04) [2021-10-27]

Read More: