RuntimeError: blank must be in label range for CTC loss

shwe87: <pre><code class="lang-auto">optimizer.zero_grad()
        
        # get the output from the model
        output, h = mynet(specs, h)
        print(output.size())
        output = F.log_softmax(output, dim=2)
        output = output.transpose(0,1)
        # calculate the loss and perform backprop
        loss = criterion(output, labels, input_lengths, label_lengths)
        loss.backward()
        # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
        nn.utils.clip_grad_norm_(mynet.parameters(), clip)
        optimizer.step()
</code></pre>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-132-fd3f6611addd&gt; in &lt;module&gt;
     42         output = output.transpose(0,1)
     43         # calculate the loss and perform backprop
---&gt; 44         loss = criterion(output, labels, input_lengths, label_lengths)
     45         loss.backward()
     46         # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--&gt; 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/loss.py in forward(self, log_probs, targets, input_lengths, target_lengths)
   1309     def forward(self, log_probs, targets, input_lengths, target_lengths):
   1310         return F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
-&gt; 1311                           self.zero_infinity)
   1312 
   1313 # TODO: L1HingeEmbeddingCriterion

/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py in ctc_loss(log_probs, targets, input_lengths, target_lengths, blank, reduction, zero_infinity)
   2050     """
   2051     return torch.ctc_loss(log_probs, targets, input_lengths, target_lengths, blank, _Reduction.get_enum(reduction),
-&gt; 2052                           zero_infinity)
   2053 
   2054 

RuntimeError: blank must be in label range
</code></pre>
<pre><code class="lang-auto">criterion = nn.CTCLoss(blank=28, zero_infinity=False)
</code></pre>

shwe87: <p>Can somebody help me with this please? Thank you. I am using a GRU and then a fully connected layer, it classifies alphabet + Space.</p>

Vatsal_Malaviya: <p>Sorry for the reply not being able to answer your error, but I wanted to ask you what method are you using for CTC decoding?<br>
Tensorflow as options like CTC beam search decoder, or CTC greedy search decoder, have you tried to use TensorFlow method while using base PyTorch implementation.</p>

shwe87: <p>No, I am so new to this. I wouldn’t even know how to combine both.<br>
For your reference for the decoder, please visit this blog:<br>
</p><aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://cdn.landen.co/m1g1apvbs0ef/assets/1q989myp.png" class="site-icon" width="16" height="16">
      <a href="https://www.assemblyai.com/blog/end-to-end-speech-recognition-pytorch" target="_blank" rel="nofollow noopener">assemblyai.com</a>
  </header>
  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:128/67;"><img src="https://landen.imgix.net/blog_KKMFzSYvUskeYQpX/assets/ocRRhYlIGMqYwDAv.jpg?w=1200&amp;h=630&amp;fit=crop" class="thumbnail" width="128" height="67"></div>

<h3><a href="https://www.assemblyai.com/blog/end-to-end-speech-recognition-pytorch" target="_blank" rel="nofollow noopener">Building an end-to-end Speech Recognition model in PyTorch</a></h3>

<p>Deep Learning has changed the game in speech recognition with the introduction of end-to-end models. These models take in audio, and directly output transcriptions. Two of the most popular end-to-end models today are Deep Speech by Baidu, and Listen...</p>


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

<p>It explains how to create decoder in pytorch.</p>
<p>I hope I helped you.</p>

Vatsal_Malaviya: <p>It was helpful <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

Unity05: <p>Hi <a class="mention" href="/u/shwe87">@shwe87</a>  - it may be a little bit too late now.<br>
However, you could check your ‘char_index_policy’ - dict. The error you mentioned above is very likely a result of a missing blank label in this dict.</p>
<p>I hope I was able to help you.<br>
Greetings, Unity05</p>