# Cross Entropy Loss between 3d tensors

**URL:** <https://discuss.pytorch.org/t/cross-entropy-loss-between-3d-tensors/79131>\
**Category:** Uncategorized\
**Created:** [April 30, 2020, 3:08pm UTC](https://discuss.pytorch.org/t/cross-entropy-loss-between-3d-tensors/79131 "2020-04-30T15:08:16Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![KFrank](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@KFrank](https://discuss.pytorch.org/u/KFrank)\
**Post date:** [April 30, 2020, 6:47pm UTC](https://discuss.pytorch.org/t/cross-entropy-loss-between-3d-tensors/79131/6 "2020-04-30T18:47:37Z")

</div>

Hello Matheus!

> [@mfcs](#):
>
> But how the Cross entropy Loss perform the computation between a probability distribution and a class value instead of the probability of the class?

I won’t comment on whether KL-divergence or cross-entropy is  
likely to be the better loss function for your use case.

But (assuming I understand what you are asking), no, you can’t  
use pytorch’s built-in `CrossEntropyLoss` with probabilities for  
targets (sometimes called _soft labels,_ a term I don’t much like).  
It requires integer class labels (even though cross-entropy makes  
perfect sense for targets that are probabilities).

However, you can write your own without much difficulty (or loss  
of performance). See this post:

> [@Soft Cross Entropy Loss (TF has it does Pytorch have it)](https://discuss.pytorch.org/t/soft-cross-entropy-loss-tf-has-it-does-pytorch-have-it/69501/2):
>
> Hello Raaj! I do not believe that pytorch has a “soft” cross-entropy function built in. But you can implement it using pytorch tensor operations, so you should get the full benefit of autograd and gpu acceleration. See this (pytorch version 0.3.0) script: import torch torch.\_\_version\_\_ # define "soft" cross-entropy with pytorch tensor operations def softXEnt (input, target): logprobs = torch.nn.functional.log\_softmax (input, dim = 1) return -(target \* logprobs).sum() / input.shap…

Best.

K. Frank

---

_[View the full topic](https://discuss.pytorch.org/t/cross-entropy-loss-between-3d-tensors/79131)._
