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 ol>
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: ‘lengths’ argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor
- Wrong classification of Python — assertion cur_ Target is greater than or equal to 0 and cur_ Target is less than n_ classes failed
- The key “target densitydpi” is not supported
- Learning notes of Python 3: debugger speedups using Python not found
- raise ValueError(‘Expected input batch_size ({}) to match target batch_size ({}).‘
- Failed to find target with hash string ‘android-25’ in:D:\SDK
- Failed to find target with hash string”android-21″ in:D:\AndroidVersionSdk
- RuntimeError: Found dtype Double but expected Float”
- OpenGL learning notes and other learning thinking
- Multi-object editing not supported error resolution
- Solve the runtimeerror in RNN: expected scalar type long but found float error
- RuntimeError: cuda runtime error (801) : operation not supported at ..
- Android learning notes 03: some problems and solutions in the learning process
- Android Studio | Failed to find target with hash string ‘android-26’ in: D:\Android\sdk
- Canal synchronization error target column: name not matched
- Uncaught Error: _registerComponent(…): Target container is not a DOM element
- Could not find module ‘xxxxxxx‘ for target ‘arm64-apple-ios-simulator‘;
- ORA-12545: Connect failed because target host or object does not exist
- TNS-12545: Connect failed because target host or object does not exist
- OpenGL learning notes: Problems and Solutions