# Cross-entropy with one-hot targets

**URL:** <https://discuss.pytorch.org/t/cross-entropy-with-one-hot-targets/13580>\
**Category:** Uncategorized\
**Created:** [February 12, 2018, 10:29pm UTC](https://discuss.pytorch.org/t/cross-entropy-with-one-hot-targets/13580 "2018-02-12T22:29:41Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Dawid\_S](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dawid_s/32/15829_2.png) [@Dawid\_S](https://discuss.pytorch.org/u/Dawid_S)\
**Post date:** [February 13, 2018, 9:36pm UTC](https://discuss.pytorch.org/t/cross-entropy-with-one-hot-targets/13580/5 "2018-02-13T21:36:49Z")

</div>

Sorry for not being clear enough.

I want to use soft targets with Cross Entropy. Soft target is one-hot-like vector, but continuous, not zero/one. The example target is in the example code below.

This is what I want:

```auto
def cross_entropy(input, target, size_average=True):
    """ Cross entropy that accepts soft targets
    Args:
         pred: predictions for neural network
         targets: targets, can be soft
         size_average: if false, sum is returned instead of mean

    Examples::

        input = torch.FloatTensor([[1.1, 2.8, 1.3], [1.1, 2.1, 4.8]])
        input = torch.autograd.Variable(out, requires_grad=True)

        target = torch.FloatTensor([[0.05, 0.9, 0.05], [0.05, 0.05, 0.9]])
        target = torch.autograd.Variable(y1)
        loss = cross_entropy(input, target)
        loss.backward()
    """
    logsoftmax = nn.LogSoftmax()
    if size_average:
        return torch.mean(torch.sum(-target * logsoftmax(input), dim=1))
    else:
        return torch.sum(torch.sum(-target * logsoftmax(input), dim=1))

```

i’m wondering if I can use any PyTorch function to pass soft targets to it, like in the example above. I was trying with BCELoss.

---

_[View the full topic](https://discuss.pytorch.org/t/cross-entropy-with-one-hot-targets/13580)._
