f8730462-89fe-4fca-aaf8-15cdf085608c
prompter
how to use nn.embedding
4238551d-c59b-40a7-b5f0-85a7010a6e5a
f8730462-89fe-4fca-aaf8-15cdf085608c
assistant
The `nn.Embedding` module in PyTorch is used for converting sparse one-hot vector representations of words into dense continuous-valued vectors, also known as word embeddings. Here's how you can use it:

1. Initialize the `nn.Embedding` layer with the desired number of words (`num_embeddings`) and the size of the word embeddings (`embedding_dim`):
```python
import torch
import torch.nn as nn

embedding = nn.Embedding(num_embeddings, embedding_dim)
```
2. The `nn.Embedding` layer takes in a tensor of integers as input, where each integer represents a word, and maps it to the corresponding word embedding.
```python
inputs = torch.LongTensor([1, 2, 3, 4, 5])
embedded = embedding(inputs)
print(embedded.shape)
# Output: torch.Size([5, embedding_dim])
```
Note that the input tensor must be of type `torch.LongTensor` and its values must be indices into the word embeddings, not the words themselves.