# Convert int into one-hot format

**URL:** <https://discuss.pytorch.org/t/convert-int-into-one-hot-format/507>\
**Category:** Uncategorized\
**Created:** [February 15, 2017, 7:52am UTC](https://discuss.pytorch.org/t/convert-int-into-one-hot-format/507 "2017-02-15T07:52:08Z")\
**Posts on this page:** 1\
**Showing post:** 25

<div class="post-metadata">

**Author:** ![willyd](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/w/85f322/32.png) [@willyd](https://discuss.pytorch.org/u/willyd)\
**Post date:** [April 16, 2018, 1:34pm UTC](https://discuss.pytorch.org/t/convert-int-into-one-hot-format/507/25 "2018-04-16T13:34:32Z")

</div>

It is also possible to abuse broadcasting and do:

```auto
# some labels
labels = torch.arange(3)
labels = labels.reshape(3, 1)

num_classes = 4
one_hot_target = (labels == torch.arange(num_classes).reshape(1, num_classes)).float()

```

gives

```auto
 1 0 0 0
 0 1 0 0
 0 0 1 0
[torch.FloatTensor of size (3,4)]

```

---

_[View the full topic](https://discuss.pytorch.org/t/convert-int-into-one-hot-format/507)._
