我知道偏移量在有两个数字时是什么意思,但是当有两个以上数字时是什么意思,例如:
weight = torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
embedding_sum = nn.EmbeddingBag.from_pretrained(weight, mode='sum')
print(list(embedding_sum.parameters()))
input = torch.LongTensor([0,1])
offsets = torch.LongTensor([0,1,2,1])
print(embedding_sum(input, offsets))
结果是:[Parameter containing:
tensor([[1., 2., 3.],
[4., 5., 6.]])]
tensor([[1., 2., 3.],
[4., 5., 6.],
[0., 0., 0.],
[0., 0., 0.]])
谁能帮我?
最佳答案
如source code所示,
return F.embedding(
input, self.weight, self.padding_idx, self.max_norm,
self.norm_type, self.scale_grad_by_freq, self.sparse)
它使用 functional embedding bag ,这解释了 offsets
参数为offsets (LongTensor, optional) – Only used when input is 1D. offsets determines the starting index position of each bag (sequence) in input.
在
EmbeddingBag
docs :If input is 1D of shape (N), it will be treated as a concatenation of multiple bags (sequences). offsets is required to be a 1D tensor containing the starting index positions of each bag in input. Therefore, for offsets of shape (B), input will be viewed as having B bags. Empty bags (i.e., having 0-length) will have returned vectors filled by zeros.
最后一条语句(“空袋(即长度为 0)将返回由零填充的向量。”)解释了结果张量中的零向量。
关于pytorch - pytorch nn.EmbeddingBag 中的偏移量是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65547335/