I am not able to decode this error. Please suggest

jojojo: <p>code is for OCR: CNN +CTC</p>
<p>import torch</p>
<p>import torch.nn as nn</p>
<p>from torch.nn.modules import dropout</p>
<p>from torch.nn.modules.dropout import Dropout, Dropout2d</p>
<p>train_on_gpu = torch.cuda.is_available()</p>
<p>class BidirectionalLSTM(nn.Module):</p>
<pre><code>def __init__(self, nIn, nHidden, nOut):

    super(BidirectionalLSTM, self).__init__()

    self.rnn = nn.LSTM(nIn, nHidden, bidirectional=True)

    self.embedding = nn.Linear(nHidden * 2, nOut)

    self.dropout = nn.Dropout(0.3)

def forward(self, input):

    recurrent, _ = self.rnn(input)

    out = self.dropout(recurrent)

    T, b, h = out.size()

    t_rec = out.view(T * b, h)

    output = self.embedding(t_rec)  # [T * b, nOut]

    output = output.view(T, b, -1)

    return output
</code></pre>
<p>class CRNN(nn.Module):</p>
<pre><code>def __init__(self, nclass=76,  imgH=32, nc=1, nh=512, n_rnn=2, leakyRelu=False):

    super(CRNN, self).__init__()

    assert imgH % 16 == 0, 'imgH has to be multiple of 16'

    ks = [3, 3, 3, 3, 3, 3, 2]

    ps = [1, 1, 1, 1, 1, 1, 0]

    ss = [1, 1, 1, 1, 1, 1, 1]

    nm = [32, 64, 128, 256, 256, 512, 512]

    cnn = nn.Sequential()

    def convRelu(i, batchNormalization=False, leakyRelu=False, relu=False):

        nIn = nc if i == 0 else nm[i - 1]

        nOut = nm[i]

        cnn.add_module('Conv{0}'.format(i),

                       nn.Conv2d(nIn, nOut, ks[i], ss[i], ps[i]))

        if batchNormalization:

            cnn.add_module('BatchNormal{0}'.format(

                i), nn.BatchNorm2d(nOut))

        if leakyRelu:

            cnn.add_module('ReLU{}'.format(i),

                           nn.LeakyReLU(0.2, inplace=True))

        if relu:

            cnn.add_module('ReLU{}'.format(i), nn.ReLU(True))

    convRelu(0, leakyRelu=True, batchNormalization=True)

    cnn.add_module('Pooling{}'.format(0), nn.MaxPool2d(2, 2))  # 32x16x64

   # cnn.add_module('dropout{}'.format(0), Dropout2d(p=0.3))

    convRelu(1, leakyRelu=True, batchNormalization=True)

    cnn.add_module('Pooling{}'.format(1), nn.MaxPool2d(2, 2))  # 64x8x32

    # cnn.add_module('dropout{}'.format(1), Dropout2d(p=0.2))

    convRelu(2, batchNormalization=True, relu=True)

    # cnn.add_module('dropout{}'.format(2), Dropout2d(p=0.2))

    convRelu(3, batchNormalization=True, relu=True)

    cnn.add_module('Pooling{}'.format(2),

                   nn.MaxPool2d((2, 2), (2, 1), (0, 1)))  # 256x4x16

    # cnn.add_module('dropout{}'.format(3), Dropout2d(p=0.2))

    convRelu(4, batchNormalization=True, relu=True)

    # cnn.add_module('dropout{}'.format(4), Dropout2d(p=0.2))

    convRelu(5, batchNormalization=True, relu=True)

    cnn.add_module('Pooling{}'.format(3),

                   nn.MaxPool2d((2, 2), (2, 1), (0, 1)))  # 512x2x16

    # cnn.add_module('dropout{}'.format(5), Dropout2d(p=0.2))

    convRelu(6, batchNormalization=True, relu=True)  # 512x1x16

    # cnn.add_module('dropout{}'.format(6), Dropout2d(p=0.2))

    self.cnn = cnn

    self.fc = nn.Sequential(nn.Linear(512*1*26, 1000), nn.ReLU(), nn.Dropout(0.3), nn.Linear(

        1000, 500), nn.ReLU(), nn.Dropout(0.2), nn.Linear(500, nclass), nn.LogSoftmax(-2))

def forward(self, input):

    conv = self.cnn(input)

    b, c, h, w = conv.size()

    print(conv.size())

    #conv = conv.view(b, c, h*w)

    # conv = conv.permute(2, 0, 1)  # [w, b, c]

    conv = conv.view(b, -1)

    # print(output.size())

    output = self.fc(conv)

   # print(output.size())

    return output
</code></pre>
<p>Epoch 1/700</p>
<p>labels = [‘NEUROSCIENTIST’, ‘literally’, ‘KEICHER’, ‘frenzy’, ‘verdon’, ‘FOXHOUND’, ‘BETTERIDGE’, “bobbitt’s”, ‘yigal’, ‘dusing’, ‘hilfiger’, ‘ZELESNIK’, ‘IN’, ‘chasteen’, ‘mezzaluna’, ‘blowdried’, ‘agonists’, “han’s”, “dickens’s”, “CONE’S”, ‘racier’, ‘NATURALISTS(3)’, ‘CODAG’, ‘milkens’, ‘WISER’, ‘enrollments’, ‘DRUIDISM’, ‘WILFRIED’, ‘KILOMETERS’, ‘HOUSDEN’, ‘ODELET’, ‘EVENTUALLY’, ‘dewey’, ‘moons’, ‘mogel’, ‘ronning’, ‘UPDATE’, ‘BABYSITTER’, ‘foreigners’, ‘falconets’, ‘gotham’, ‘MACTAVISH’, ‘cites’, ‘STAMPEDED’, ‘kostick’, ‘purloin’, ‘GAVIOTAS’, ‘KUHNS’, ‘MERKLAN’, ‘accomplice’, ‘LABUS’, ‘manifestations’, ‘MOURA’, ‘SCHWEPPE’, ‘hehmeyer’, ‘willems’, ‘RATIOS’, ‘thematically’, ‘TICKET’, ‘KEES’, ‘vanover’, ‘KLYNVELD’, ‘STERILIZATION’, ‘lxi’]</p>
<p>torch.Size([64, 512, 1, 26])</p>
<p>torch.Size([64, 76])</p>
<p>Traceback (most recent call last):</p>
<p>File “c:\Users\lovis\OneDrive\Desktop\Master Thesis\New code\train9.py”, line 234, in </p>
<pre><code>train(700)
</code></pre>
<p>File “c:\Users\lovis\OneDrive\Desktop\Master Thesis\New code\train9.py”, line 112, in train</p>
<pre><code>loss = criterion(outputs, target, outputs_size, target_lengths)
</code></pre>
<p>File “C:\Users\lovis\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\module.py”, line 889, in _call_impl</p>
<pre><code>result = self.forward(*input, **kwargs)
</code></pre>
<p>File “C:\Users\lovis\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\loss.py”, line 1590, in forward</p>
<pre><code>return F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
</code></pre>
<p>File “C:\Users\lovis\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\functional.py”, line 2307, in ctc_loss</p>
<pre><code>return torch.ctc_loss(
</code></pre>
<p>RuntimeError: Expected 3-dimensional tensor, but got 2-dimensional tensor for argument <span class="hashtag">#1</span> ‘log_probs’ (while checking arguments for ctc_loss_cpu)</p>

ptrblck: <p>The error is given as:</p>
<pre data-code-wrap="python"><code class="lang-python">RuntimeError: Expected 3-dimensional tensor, but got 2-dimensional tensor for argument #1 ‘log_probs’ (while checking arguments for ctc_loss_cpu)
</code></pre>
<p>which points to a wrong shape for <code>log_probs</code>.<br>
Checking the <a href="https://pytorch.org/docs/stable/generated/torch.nn.CTCLoss.html?highlight=ctcloss#torch.nn.CTCLoss">docs</a> of <code>nn.CTCLoss</code> gives the expected shape for <code>log_probs</code> as:</p>
<blockquote>
<p>Log_probs: Tensor of size (T,N,C), where T=input length, N=batch size, and C=number of classes (including blank). The logarithmized probabilities of the outputs (e.g. obtained with <a href="https://pytorch.org/docs/stable/generated/torch.nn.functional.log_softmax.html#torch.nn.functional.log_softmax"><code>torch.nn.functional.log_softmax()</code></a>).</p>
</blockquote>

jojojo: <p>Hi bro. Thanks for replying. I made some changes in the code. Basically I unsqueezed the output got from the last fully connected layer to get the output in shape of(T,N,C) so that it can get feeded to CTC loss function. There is no error now but the training is not producing any output. Please can you help me in telling where Im going wrong. Im attaching the updated code:</p>
<p><strong>MODEL NETWORK</strong><br>
import torch</p>
<p>import torch.nn as nn</p>
<p>from torch.nn.modules import dropout</p>
<p>from torch.nn.modules.dropout import Dropout, Dropout2d</p>
<p>train_on_gpu = torch.cuda.is_available()</p>
<p>class CRNN(nn.Module):</p>
<pre><code>def __init__(self, nclass=76,  imgH=32, nc=1, nh=512, n_rnn=2, leakyRelu=False):

    super(CRNN, self).__init__()

    assert imgH % 16 == 0, 'imgH has to be multiple of 16'

    ks = [3, 3, 3, 3, 3, 3, 2]

    ps = [1, 1, 1, 1, 1, 1, 0]

    ss = [1, 1, 1, 1, 1, 1, 1]

    nm = [32, 64, 128, 256, 256, 512, 512]

    cnn = nn.Sequential()

    def convRelu(i, batchNormalization=False, leakyRelu=False, relu=False):

        nIn = nc if i == 0 else nm[i - 1]

        nOut = nm[i]

        cnn.add_module('Conv{0}'.format(i),

                       nn.Conv2d(nIn, nOut, ks[i], ss[i], ps[i]))

        if batchNormalization:

            cnn.add_module('BatchNormal{0}'.format(

                i), nn.BatchNorm2d(nOut))

        if leakyRelu:

            cnn.add_module('ReLU{}'.format(i),

                           nn.LeakyReLU(0.2, inplace=True))

        if relu:

            cnn.add_module('ReLU{}'.format(i), nn.ReLU(True))

    convRelu(0, leakyRelu=True, batchNormalization=True)

    cnn.add_module('Pooling{}'.format(0), nn.MaxPool2d(2, 2))  # 32x50x16

   # cnn.add_module('dropout{}'.format(0), Dropout2d(p=0.3))

    convRelu(1, leakyRelu=True, batchNormalization=True)

    cnn.add_module('Pooling{}'.format(1), nn.MaxPool2d(2, 2))  # 64x25x8

    # cnn.add_module('dropout{}'.format(1), Dropout2d(p=0.2))

    convRelu(2, batchNormalization=True, relu=True)

    # cnn.add_module('dropout{}'.format(2), Dropout2d(p=0.2))

    convRelu(3, batchNormalization=True, relu=True)

    cnn.add_module('Pooling{}'.format(2),

                   nn.MaxPool2d((1, 2)))  # 256x25x4

    # cnn.add_module('dropout{}'.format(3), Dropout2d(p=0.2))

    convRelu(4, batchNormalization=True, relu=True)

    # cnn.add_module('dropout{}'.format(4), Dropout2d(p=0.2))

    convRelu(5, batchNormalization=True, relu=True)

    cnn.add_module('Pooling{}'.format(3),

                   nn.MaxPool2d((1, 2)))  # 512x25x2

    # cnn.add_module('dropout{}'.format(5), Dropout2d(p=0.2))

    convRelu(6, batchNormalization=True, relu=True)  # 512x24x1

    # cnn.add_module('dropout{}'.format(6), Dropout2d(p=0.2))

    self.cnn = cnn

    self.fc = nn.Sequential(nn.Linear(512*35, 1000), nn.ReLU(), nn.Dropout(0.3), nn.Linear(

        1000, 500), nn.ReLU(), nn.Dropout(0.2), nn.Linear(500, nclass))

def forward(self, input):

    conv = self.cnn(input)

    b, c, h, w = conv.size()

    print(conv.size())

    #conv = conv.view(b, c, h*w)

    # conv = conv.permute(2, 0, 1)  # [w, b, c]

    conv = conv.view(b, -1)

    # print(output.size())

    output = self.fc(conv)

    output = output.unsqueeze(0)

    m = nn.LogSoftmax(2)

    output = m(output)

   # print(output.size())

    return output
</code></pre>
<p>model = CRNN()</p>
<p>print(model)</p>
<p>output is like as pasted below:<br>
Epoch: 1, Training Loss: nan, Validation Loss: inf, Train Accuracy: 0.0, Validation Acuuracy: 0.0 CER: 99.96780712484833<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 2, Training Loss: nan, Validation Loss: inf, Train Accuracy: 0.0, Validation Acuuracy: 0.0 CER: 100.0<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 3, Training Loss: nan, Validation Loss: inf, Train Accuracy: 0.0, Validation Acuuracy: 0.0 CER: 100.0<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 4, Training Loss: nan, Validation Loss: inf, Train Accuracy: 0.0, Validation Acuuracy: 0.0 CER: 100.0<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 5, Training Loss: nan, Validation Loss: inf, Train Accuracy: 0.0, Validation Acuuracy: 0.0 CER: 100.0<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 1, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.98144259535059<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 2, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 3, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 4, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 5, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 6, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 7, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 8, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 9, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 10, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 11, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 12, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 13, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 14, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 15, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 16, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 17, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 18, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 19, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 20, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 21, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 22, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 23, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 24, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 25, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 26, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 27, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 28, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 29, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 30, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 31, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 32, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 33, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 34, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …<br>
Epoch: 35, Training Loss: nan, Validation Loss: inf, Train Accuracy: 8.355614973262032e-05, Validation Acuuracy: 0.0 CER: 99.99164438502673<br>
Validation loss decreased (inf → inf).  Saving model …</p>

ptrblck: <p>Since you are seeing NaN losses, you could check <a href="https://discuss.pytorch.org/t/ctc-loss-produced-nan-when-using-spatial-features-from-alexnet/85248/3">this post</a> which explains common things to check when using CTCLoss.</p>