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:

```
torch.LongTensor(labels)
```

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] https://www.cnblogs.com/blogwangwang/p/12018897.html

[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] https://www.pythonf.cn/read/125399

### Read More:

- RuntimeError: Found dtype Double but expected Float”
- How to use torch.sum()
- Solve the runtimeerror in RNN: expected scalar type long but found float error
- PyTorch – AttributeError: ‘bool‘ object has no attribute ‘sum‘
- Tensorflow: How to use expand_Dim() to add dimensions
- To solve the problem of increasing video memory when training network (torch)
- tf.layers.conv1d Function analysis (one dimensional convolution)
- Python: CUDA error: an illegal memory access was accounted for
- RuntimeError: each element in
- Solve runtimeerror: reduce failed to synchronize: device side assert triggered problem
- In tensorflow tf.reduce_ Mean function
- RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the
- CheXNet-master: CUDA out of memery [How to Solve]
- Keras:KeyError:‘Failed to format this callback filepath:{val_loss:.4f}.h5. Reason: \‘val_loss\‘‘
- “Typeerror: invalid dimensions for image data” in Matplotlib drawing imshow() function
- Matlab prompt error
- RuntimeError: log_vml_cpu not implemented for ‘Long’
- RuntimeError: ‘lengths’ argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor
- RuntimeError: cuda runtime error (801) : operation not supported at ..
- Solution to unbalanced load of multiple cards (GPU’s 0 card is too high) in Python model training (simple and effective)