'lengths' argument should be a 1D CPU int64 tensor, but got 0D cpu Long tensor

Legolas: <p>Hello,</p>
<p>I use torch 1.8.1+cuda10.2+torchtext0.9.1<br>
Platform: Windows 10<br>
Device: GeForce GTX 1050</p>
<p>This code</p>
<pre><code class="lang-auto">packed_embedded = nn.utils.rnn.pack_padded_sequence(input=embedded, lengths=text_lengths)
</code></pre>
<p>raises the error: “‘lengths’ argument should be a 1D CPU int64 tensor, but got 0D cpu Long tensor.”</p>
<p>I tried the following approaches:</p>
<pre><code class="lang-auto">#approach no 1:
text_lengths = torch.as_tensor(text_lengths, dtype=torch.int64, device='cpu')
packed_embedded = nn.utils.rnn.pack_padded_sequence(embedded, text_lengths)

#approach no 2:
lengths=text_lengths.cpu()

#approach no 3:
packed_embedded = nn.utils.rnn.pack_padded_sequence(embedded, torch.as_tensor(text_lengths.cpu(), dtype=torch.int64))

#approach no 4:
packed_embedded = nn.utils.rnn.pack_padded_sequence(embedded, text_lengths.type(torch.FloatTensor))
</code></pre>
<p>but without success.</p>
<p>I checked the value of</p>
<pre><code class="lang-auto">text_lengths.type()
</code></pre>
<p>in train() and the model and  all the time it has the value of</p>
<pre><code class="lang-auto">torch.LongTensor
</code></pre>
<p>, no matter what I tried to change it.<br>
Is there any other workaround for this problem?</p>
<p>thank you</p>

pascal_notsawo: <p>What do you have as output for <code>text_lengths.shape</code>.</p>

Legolas: <p>Hi,<br>
<a class="mention" href="/u/pascal_notsawo">@pascal_notsawo</a>  in train method I have</p>
<pre><code class="lang-auto">text, labels, text_lengths = train_batch
</code></pre>
<p>and the shape is<code> text_lengths.shape</code> → <code>torch.Size([]) </code></p>

pascal_notsawo: <p>That’s why there is an error.<br>
The <code>lengths</code> parameter of <code>pack_padded_sequence</code> just specifies the length of each sequence of your batch.<br>
Documentation: <a href="https://pytorch.org/docs/stable/generated/torch.nn.utils.rnn.pack_padded_sequence.html" class="inline-onebox" rel="noopener nofollow ugc">torch.nn.utils.rnn.pack_padded_sequence — PyTorch 1.9.0 documentation</a></p>
<p>For an input of shape <code>(T, B, E)</code>, or <code>(B, T, E)</code> if <code>batch_first</code> is <code>True</code>, where <code>T</code> is the length of the longest sequence (equal to <code>lengths[0]</code> if <code>enforce_sorted</code> is <code>True</code>), <code>B</code> is the batch size, and <code>E</code> is any number of dimensions (including 0, but in general it is the embedding dimension).<br>
So <code>lengths</code> contains the length of the <code>B</code> sequences : it must be of shape <code>(B)</code>, and not of empty shape as in your case. This is what causes the problem.</p>
<p>For example if I have the three (<code>B=3</code>) following sequences (here their indexes in the vocabulary after tokenisation, <code>padding_token_index = 0</code>) : <code>[[2, 5, 0, 0], [1, 0, 0, 0], [7, 5, 2, 2]] </code>. Then <code>T = 4 and lengths = [2, 1, 4]</code> (should be a 1D CPU int64 tensor).</p>

Leo2: <p>Did you ever solve this problem. I’m having the same issue.</p>

jiamiao_liu: <p>I solve the problem, sentence_length contain two dim, first dim present batch_size, second dim present each length of sentence. Hope can help you in the answer.</p>