what does offsets mean in pytorch nn.EmbeddingBag?
I know offsets meaning when it has two numbers, but what does it mean when more than two numbers,for example:
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))

the result is :
[Parameter containing:
tensor([[1., 2., 3.],
        [4., 5., 6.]])]
tensor([[1., 2., 3.],
        [4., 5., 6.],
        [0., 0., 0.],
        [0., 0., 0.]])

who can help me?

import torch
import torch.nn as nn

weight = torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
embedding_sum = nn.EmbeddingBag.from_pretrained(weight, mode='sum')
print(embedding_sum.weight)

&quot;&quot;&quot; output
Parameter containing:
tensor([[1., 2., 3.],
        [4., 5., 6.]])
&quot;&quot;&quot;

input = torch.LongTensor([0, 1])
offsets = torch.LongTensor([0, 1, 2, 1])

According to these offsets you will get the following samples
&quot;&quot;&quot;
sample_1: input[0:1] # tensor([0])
sample_2: input[1:2] # tensor([1])
sample_3: input[2:1] # tensor([])
sample_4: input[1:]  # tensor([1])
&quot;&quot;&quot;

Embedding the samples above
# tensor([0]) =&gt; lookup 0  =&gt; embedding_sum.weight[0] =&gt; [1., 2., 3.]
# tensor([1]) =&gt; lookup 1  =&gt; embedding_sum.weight[1] =&gt; [4., 5., 6.]
# tensor([])  =&gt; empty bag                            =&gt; [0., 0., 0.]
# tensor([1]) =&gt; lookup 1  =&gt; embedding_sum.weight[1] =&gt; [4., 5., 6.]

print(embedding_sum(input, offsets))

&quot;&quot;&quot; output
tensor([[1., 2., 3.],
        [4., 5., 6.],
        [0., 0., 0.],
        [4., 5., 6.]])
&quot;&quot;&quot;

One more example:
input = torch.LongTensor([0, 1])
offsets = torch.LongTensor([0, 1, 0])

According to these offsets you will get the following samples
&quot;&quot;&quot;
sample_1: input[0:1] # tensor([0])
sample_2: input[1:0] # tensor([])
sample_3: input[0:]  # tensor([0, 1])
&quot;&quot;&quot;

Embedding the samples above
# tensor([0])    =&gt; lookup 0 =&gt; embedding_sum.weight[0] =&gt; [1., 2., 3.]
# tensor([])     =&gt; empty bag =&gt; [0., 0., 0.]
# tensor([0, 1]) =&gt; lookup 0 and 1 then reduce by sum 
#                =&gt; embedding_sum.weight[0] + embedding_sum.weight[1] =&gt; [5., 7., 9.]

print(embedding_sum(input, offsets))

&quot;&quot;&quot; output
tensor([[1., 2., 3.],
        [0., 0., 0.],
        [5., 7., 9.]])
&quot;&quot;&quot;

