Problem with expected tensor size of input of ctc_loss function

username: nicosoto0
cooked: <p>Hi pytorch community. I am having problems using torch.nn.functional.ctc_loss()</p>
<p>I have the following tensors:</p>
<pre><code class="lang-python">prob_txt_pred:  torch.Size([225, 51, 54])
target_words:  torch.Size([51, 30])
length_input:  torch.Size([51])
target_len_words:  torch.Size([51])
</code></pre>
<p>I am giving them to the ctc_loss function in the following way:</p>
<pre><code class="lang-python">nf.ctc_loss(prob_txt_pred, target_words, length_input, target_len_words)
</code></pre>
<p>and I am getting an error that says that dimension 1 of the tensor: ‘targets’ (in this case tensor_words) is expected to be at least 225.</p>
<p>The full error says:<br>
RuntimeError: Expected tensor to have size at least 225 at dimension 1, but got size 51 for argument <span class="hashtag">#2</span> ‘targets’ (while checking arguments for ctc_loss_gpu)</p>
<p>Any idea why?</p>
<p>thanks for your time!</p>

username: drkw
cooked: <p>Maybe this includes torch bug.</p>
<p>This message caused in aten/src/ATen/native/LossCTC.cpp at line 86</p>
<p>Real message I think is</p>
<blockquote>
<p>RuntimeError: Expected tensor to have size at least {‘max(prob.shape[0])’} at dimension 1, but got size {‘<strong>length_input[index]</strong>’} for argument <span class="hashtag-raw">#2</span> ‘<strong>log_probs</strong>’ (while checking arguments forctc_loss_gpu)</p>
</blockquote>
<p>And error is caused because you set number bigger than prob.shape[0] for length_input.<br>
You need to set number of input length.</p>

username: tom
cooked: <p>CTC loss takesinputs batch first.</p>
<p>Best regards</p>
<p>Thomas</p>

username: pfriesch
cooked: <p>Like <a class="mention" href="/u/drkw">@drkw</a> said, this error is wrong and it means the given input lengths are too long. I stumbled on this because I forgot to remove the receptive field from my input lengths.</p>
<p><a class="mention" href="/u/drkw">@drkw</a> proposed a fix:<br>
<aside class="onebox githubpullrequest">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/pull/16917" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
      <a href="https://github.com/drkw1git" target="_blank" rel="nofollow noopener">
    <img alt="drkw1git" src="https://avatars1.githubusercontent.com/u/12480847?v=4" class="thumbnail onebox-avatar" width="90" height="90">
  </a>

<h4>
  <a href="https://github.com/pytorch/pytorch/pull/16917" target="_blank" rel="nofollow noopener">ctc_loss error message bug fix.</a>
</h4>

<div class="date">
  by <a href="https://github.com/drkw1git" target="_blank" rel="nofollow noopener">drkw1git</a>
  on <a href="https://github.com/pytorch/pytorch/pull/16917" target="_blank" rel="nofollow noopener">08:14AM - 09 Feb 19 UTC</a>
</div>

<div class="github-commit-stats">
  <strong>1 commits</strong>
  changed <strong>1 files</strong>
  with <strong>1 additions</strong>
  and <strong>1 deletions</strong>.
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>

username: jpdevicente
cooked: <p><a class="mention" href="/u/nicosoto0">@nicosoto0</a> I’m having the same problem. Could you solve it?</p>

username: brifuture
cooked: <p>The size is determined by you seq length, for example, the size of <code>target_len_words</code> is 51, but each element of <code>target_len_words</code> may be greater than 1, so the target_words size may not be 51.</p>
<p>if the value of target_len_words.sum() == 225, then your target size should be sum(target_lengths) which equals to 225 (one-dimension targets). In your case, two-dimension targets seems right. Maybe it is a bug of torch?</p>

username: add023
cooked: <p><a href="https://discuss.pytorch.org/u/ptrblck">ptrblck</a> Hello, I would be grateful if someone can help me here. I have a model whose high level architecture is<br>
<code>CNN -&gt; Transformer Encoder -&gt; CTC</code>.</p>
<p>When the batch size 2 the CTC loss works (using batch size 2 because of GPU limitations). Here is an example pipeline.</p>
<p><strong>CNN output:</strong><br>
<code>size [2, 168, 1024] # 2 videos, 168 frames/video, 1024 spatial embedding output</code></p>
<p><strong>ctc_loss:</strong></p>
<pre><code class="lang-auto">def ctc_loss(log_probs, targets, input_lengths, target_lengths, blank=0,
             reduction='mean', zero_infinity=False):
    # type: (Tensor, Tensor, Tensor, Tensor, int, str, bool) -&gt; Tensor

    return torch.ctc_loss(log_probs, targets, input_lengths, target_lengths, blank, _Reduction.get_enum(reduction),
                          zero_infinity)

</code></pre>
<pre><code class="lang-auto">log_probs: size [226, 2, 70]
targets: size [2, 18]
input_lengths: size [2], value: tensor([78,78])
target_lengths: size [2]
</code></pre>
<p>During training in one of the epochs when the batch size is 1 the runtime error pops up.</p>
<p><strong>CNN output:</strong><br>
<code>size [1, 42, 1024] # 1 video, 42 frames, 1024 spatial embedding output</code></p>
<p><strong>ctc_loss:</strong></p>
<pre><code class="lang-auto">log_probs: size [42, 1, 70]
targets: size [1]
input_lengths: size [1], value: tensor([77])
target_lengths: size [1]
</code></pre>
<p><code>RuntimeError: Expected input_lengths to have value at most 42, but got value 77 (while checking arguments for ctc_loss_gpu)</code></p>
<p>When the batch size is 2, the <code>input_lengths</code> tensor has value [78, 78] and the ctc loss works but it doesn’t work when batch size is 1. Any idea what’s going on?<br>
Thanks.</p>

username: tom
cooked: <p>In the batch size 2 example you have 226 time steps in log probs and 78 &lt;= 226.<br>
In the batch size 1 example you have 42 time steps in log probs and 77 &gt; 42.<br>
You cannot feed fewer time steps than your input size claims the input has.</p>
<p>Best regards</p>
<p>Thomas</p>

username: add023
cooked: <p>Thanks <a class="mention" href="/u/tom">@tom</a>, that was the issue.</p>