Variable length indexing of embeddings without padding

flyaway: <p>Hi, there,</p>
<p>I am training word embeddings with our new model.<br>
The problem I am facing is my input indexs are in different length.<br>
For example, the input index might be [LongTensor([1,2,3]), LongTensor([5,8])].<br>
Because Embedding class only accept LongTensor, I can not pass in a List.<br>
After some searching, I have two possible solutions:</p>
<ol>
<li>Using padding index. However, this method requires a large amount of cuda memory because my index is actually very sparse. For example, the total number of possible index might be around 100000, while each instance only have 100 activated index. So, in this case, padding is not memory efficient.</li>
<li>Go through the list one by one to index the embedding. This method can work. But this method is too slow. It  is at least 10 times slower than batch.</li>
</ol>
<p>Is there any third method that can resolve my situation?</p>

TheShadow29: <p>Have you looked into pad packed sequence and pack padded sequence? This blog post <a href="https://medium.com/huggingface/understanding-emotions-from-keras-to-pytorch-3ccb61d5a983" rel="nofollow noopener">https://medium.com/huggingface/understanding-emotions-from-keras-to-pytorch-3ccb61d5a983</a> has a section describing packed sequences which might be what you are looking for unless I am misunderstanding your question.</p>

flyaway: <p>Packed sequence can not be used in indexing Embedding.<br>
It only can be used for RNN or LSTM.<br>
Embedding can only be indexed by LongTensor I think.</p>

TheShadow29: <p>You are correct that packed sequence is for lstm.</p>
<p>If you have 100000 indices, how will you back propogate through time? I would use some kind of a sequence window (say 10), and pass at most 10 inputs at a time. You could also do it like in the awd-lstm paper where the bptt window is random (see <a href="https://arxiv.org/pdf/1708.02182.pdf" rel="nofollow noopener">https://arxiv.org/pdf/1708.02182.pdf</a> for more details).</p>
<p>Not sure if this answers your question though.</p>

flyaway: <p>Thanks for the replying.<br>
Perhaps I have not described my problem correctly.<br>
Let me try again.<br>
I have a list of indices, like A = [[1,2,3], [2,3,4,5,7],…],  each of indices has different length.<br>
Now I want to use this 2-D indices to index Embeddings.<br>
For example, Embedding(A[0]) means I want three embeddings at position 1,2,3; Embedding(A[1]) means I want four embeddings at position 2,3,4,5,7.<br>
But, I can not Embedding(A) directly, because each A[i] has different length.</p>
<p>If we use padding, padding(A)=B=[[1,2,3, 0,0], [2,3,4,5,7]], then we can use Embedding(B) to index all the embedding I need (0 is the padding index).<br>
However, the max length of A[i] can be huge, i.e. 1000000.<br>
If we still use padding, it would waste most of the memory.</p>
<p>And in my model, len(A) = 20000 and I need to compute Embedding(A) in every batch.<br>
When I use small dataset, padding is totally fine.<br>
But when I use large dataset, most of the cuda memory is wasted.</p>
<p>My model does not use LSTM or RNN. I do not need bptt here …</p>
<p>Hope I explained clearly.</p>

TheShadow29: <p>Hmm. I see I completely missed the point (probably because I am playing lstms atm haha).</p>
<p>I don’t see any easy solution, unfortunately. One way could be first sort your data in order of length. Make <code>shuffle=False</code> in the <code>Dataloader</code>. Say for the first 100 indices you only have sequences of length max 4, so you can adjust your Dataset output accordingly. Not sure if there are any easier or better approaches available.</p>

flyaway: <p>Thanks for the help.<br>
I will try your method.</p>

sgaseretto: <p>Hello <a class="mention" href="/u/flyaway">@flyaway</a>, any luck solving this problem? I’m interested in doing the exact same thing</p>

Sum_Sum: <p>In case you are interested in finding the mean/max/sum embedding of each row of embeddings (that can be of varying lengths) an EmbeddingBag may give you what you want.<br>
<a href="https://pytorch.org/docs/stable/generated/torch.nn.EmbeddingBag.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.nn.EmbeddingBag.html</a></p>