<jupyter_start><jupyter_text>Thinking in tensors in PyTorchHands-on training  by [<NAME>](https://p.migdal.pl) (2019). Version for [AI & NLP Workshop Day](https://nlpday.pl/), 31 May 2019, Warsaw, Poland: **Understanding LSTM and GRU networks in PyTorch**. NLP & AI: 3. Embedding vs one-hot encoding[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/stared/thinking-in-tensors-writing-in-pytorch/blob/master/extra/3%20Embedding%20vs%20one-hot%20encoding.ipynb)<jupyter_code>import torch
from torch import nn
emb = nn.Embedding(10, 3)
emb.weight
words = torch.LongTensor([[2, 2, 4, 1, 5]])
words
emb(words)
words_onehot = torch.zeros((5, 10), dtype=torch.float32)
for i, j in enumerate([ 2,  2,  4,  1,  5]):
    words_onehot[i, j] = 1.
words_onehot
words_onehot.matmul(emb.weight)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15963511972633979, 'fraction_numerical': 0.04446978335233751, 'mean_word_length': 5.455882352941177, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '660469', 'license': 'MIT', 'n_tokens_mistral': 371, 'n_tokens_neox': 335, 'n_words': 72, 'path': 'rnns/3 Embedding vs one-hot encoding.ipynb', 'repo_name': 'soumendukrg/thinking-in-tensors-writing-in-pytorch', 'size': '883'}