torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None,
max_norm=None, norm_type=2.0, scale_grad_by_freq=False,
sparse=False, _weight=None)
import torch
import torch.nn as nn
embedding = nn.Embedding(10, 3)
input1 = torch.LongTensor([0,1,2,3,4,5,6,7,8,9])
input2 = torch.LongTensor([0,1,2,3,4,5,6,7,8,9,0,1,2])
print(input1.shape)
print(embedding(input1))
print(embedding(input1).shape)
print(input2.shape)
print(embedding(input2))
print(embedding(input2).shape)
参数说明:
torch.Size([10])
tensor([[ 2.1257e-01, 3.7856e-01, -1.7622e+00],
[-9.1479e-01, 1.3890e+00, -9.7418e-01],
[ 1.8179e+00, -1.0590e+00, -6.9496e-01],
[ 1.7088e-01, -3.6295e-01, -9.2374e-04],
[-1.1558e+00, -1.3642e+00, -3.5481e-01],
[-6.2106e-01, -1.5230e-01, -2.6354e+00],
[ 1.9605e-01, 2.5246e+00, 1.9009e+00],
[ 3.6155e-01, -5.9008e-01, -9.4289e-01],
[ 1.1621e+00, 1.0859e+00, -9.6472e-01],
[ 1.6503e-01, -3.6054e-01, -6.2835e-01]], grad_fn=<EmbeddingBackward0>)
torch.Size([10, 3])
torch.Size([13])
tensor([[ 2.1257e-01, 3.7856e-01, -1.7622e+00],
[-9.1479e-01, 1.3890e+00, -9.7418e-01],
[ 1.8179e+00, -1.0590e+00, -6.9496e-01],
[ 1.7088e-01, -3.6295e-01, -9.2374e-04],
[-1.1558e+00, -1.3642e+00, -3.5481e-01],
[-6.2106e-01, -1.5230e-01, -2.6354e+00],
[ 1.9605e-01, 2.5246e+00, 1.9009e+00],
[ 3.6155e-01, -5.9008e-01, -9.4289e-01],
[ 1.1621e+00, 1.0859e+00, -9.6472e-01],
[ 1.6503e-01, -3.6054e-01, -6.2835e-01],
[ 2.1257e-01, 3.7856e-01, -1.7622e+00],
[-9.1479e-01, 1.3890e+00, -9.7418e-01],
[ 1.8179e+00, -1.0590e+00, -6.9496e-01]], grad_fn=<EmbeddingBackward0>)
torch.Size([13, 3])
Process finished with exit code 0