Equivalent of torch EmbeddingBag
Torch claim that EmbeddingBag with mode=&quot;sum&quot; is equivalent to Embedding followed by torch.sum(dim=1), but how can I implement it in detail? Let's say we have
&quot;EE = nn.EmbeddingBag(n, m, mode=&quot;sum&quot;, sparse=True)&quot;, how can we replace the &quot;nn.EmbeddingBag&quot; by &quot;nn.Embeeding&quot; and &quot;torch.sum&quot; equivalently? Many thanks

Consider the following example where all four implementations yield the same result:

nn.EmbeddingBag:
&gt;&gt;&gt; embedding_sum = nn.EmbeddingBag(10, 3, mode='sum')
&gt;&gt;&gt; embedding_sum(input, torch.zeros(1).long())


nn.functional.embedding_bag:
&gt;&gt;&gt; F.embedding_bag(input, embedding_sum.weight, torch.zeros(1).long(), mode='sum')


nn.Embedding:
&gt;&gt;&gt; embedding = nn.Embedding(10, 3)
&gt;&gt;&gt; embedding.weight = embedding_sum.weight
&gt;&gt;&gt; embedding(input).sum(0)


nn.functional.embedding:
&gt;&gt;&gt; F.embedding(input, embedding_sum.weight).sum(0)



