@zqu1992
I can confirm that, and this also happens with the nn.embedding version:
predefined_weights = torch.rand(10, 3)
emb = torch.nn.Embedding(num_embeddings=10, embedding_dim=3, padding_idx=0, _weight=predefined_weights)
emb(torch.LongTensor([[0,2,0,5]]))
It “pads” with the vector at idx=0 from predefined_weights instead of a zero vector.
From the code, it looks like it zeros-out the vector in padding_idx in reset_parameters but only
if _weight is None
otherwise, it uses the given _weight parameter.
IMO, the behavior is fine and the documentation should adjust, cause if you provide your own weights, you should get full control over the weights, including the padding vector.
Roy.