Shape:
- Input: LongTensor of arbitrary shape containing the indices to extract
- Weight: Embedding matrix of floating point type with shape `(vocab_size, embedding_dim)`,
where vocab_size = maximum index + 1 and embedding_dim = the embedding size
- Output: `(*, embedding_dim)`, where `*` is the input shape
>>># a batch of 2 samples of 4 indices each>>>input= torch.tensor([[1,2,4,5],[4,3,2,9]])>>># an embedding matrix containing 10 tensors of size 3>>> embedding_matrix = torch.rand(10,3)>>> F.embedding(input, embedding_matrix)
tensor([[[0.8490,0.9625,0.6753],[0.9666,0.7761,0.6108],[0.6246,0.9751,0.3618],[0.4161,0.2419,0.7383]],[[0.6246,0.9751,0.3618],[0.0237,0.7794,0.0528],[0.9666,0.7761,0.6108],[0.3385,0.8612,0.1867]]])>>># example with padding_idx>>> weights = torch.rand(10,3)>>> weights[0,:].zero_()>>> embedding_matrix = weights
>>>input= torch.tensor([[0,2,0,5]])>>> F.embedding(input, embedding_matrix, padding_idx=0)
tensor([[[0.0000,0.0000,0.0000],[0.5609,0.5384,0.8720],[0.0000,0.0000,0.0000],[0.6262,0.2438,0.7471]]])