# How does nn.Embedding work?

**URL:** <https://discuss.pytorch.org/t/how-does-nn-embedding-work/88518>\
**Category:** Uncategorized\
**Created:** [July 9, 2020, 7:47am UTC](https://discuss.pytorch.org/t/how-does-nn-embedding-work/88518 "2020-07-09T07:47:50Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![vdw](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vdw/32/10074_2.png) [@vdw](https://discuss.pytorch.org/u/vdw)\
**Post date:** [July 9, 2020, 8:30am UTC](https://discuss.pytorch.org/t/how-does-nn-embedding-work/88518/3 "2020-07-09T08:30:09Z")

</div>

An Embedding layer is essentially just a Linear layer. So you could define a your layer as `nn.Linear(1000, 30)`, and represent each word as a one-hot vector, e.g., `[0,0,1,0,...,0]` (the length of the vector is 1,000).

As you can see, any word is a unique vector of size 1,000 with a 1 in a unique position, compared to all other words. Now giving such a vector `v` with `v[2]=1` (cf. example vector above) to the Linear layer gives you simply the 2nd row of that layer.

`nn.Embedding` just simplifies this. Instead of giving it a big one-hot vector, you just give it an index. This index basically is the same as the position of the single 1 in the one-hot vector.

---

_[View the full topic](https://discuss.pytorch.org/t/how-does-nn-embedding-work/88518)._
