# For crossentropy loss function why output and target size cannot be the same

I’m learning to use PyTorch to solve a multi-item, multi-feature, time sequence prediction problem.
In brief, my question is why the size of output and target of crossentropy loss function cannot be the same.
For instance, size of output is (batch_size, num_items), in which each element is a value fitted to the ground true class. Like matrix A:
[[ 0.5 2.1 4.8 3.2 ]
[ 5.0 4.3 2.7 0.2 ]
[ 3.7 0.3 2.0 1.5 ]]
(each row is one batch, and each column is for an item.)
Accordingly, size of the target is also (batch_size, num_items), like matrix B:
[[ 1 0 2 1 ]
[ 0 3 2 1 ]
[ 1 1 1 0 ]]
I suppose the most suitable loss function for my model in pytorch should be crossentropy(one of the pointwise methods?), but if that’s not true, please correct me.
Based on the Doc of pytorch loss functions, Input(output of model): (N, C) where C = number of classes, or (N, C, d_1, d_2, …, d_K) in the case of K-dimensional loss.
Target: (N) where each value is 0:C−1, or (N, d_1, d_2, …, d_K) in the case of K-dimensional loss.
How can I transform or squeeze the shape of my output and target to fit the requirement of crossentropy loss function? Thanks a lot.

`nn.CrossEntropyLoss` is used for multi-class classification use cases, i.e. each sample belongs to a single target class.
In your example, `num_items` in the output would correspond to the number of classes.
Each row would give you the logits of this sample to belong to the class at column x.
Based on this, the target should only contain the target class indices, which are used to calculate the cross entropy loss.

If your targets might take arbitrary values for each element of your output, you might want to use some loss function like `nn.MSELoss`.

In that case your output should have the shape `[batch_size, nb_classes, num_items]`, which could be seen as a temporal signal, where each sample contains the class logits for `nb_classes`.
`nn.CrossEntropyLoss` would then maximize the output of the current neuron which corresponds to the target class.