I am not able to decode this error. Please suggest

code is for OCR: CNN +CTC

import torch

import torch.nn as nn

from torch.nn.modules import dropout

from torch.nn.modules.dropout import Dropout, Dropout2d

train_on_gpu = torch.cuda.is_available()

class BidirectionalLSTM(nn.Module):

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

class CRNN(nn.Module):

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

Epoch 1/700

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’]

torch.Size([64, 512, 1, 26])

torch.Size([64, 76])

Traceback (most recent call last):

File “c:\Users\lovis\OneDrive\Desktop\Master Thesis\New code\train9.py”, line 234, in

train(700)

File “c:\Users\lovis\OneDrive\Desktop\Master Thesis\New code\train9.py”, line 112, in train

loss = criterion(outputs, target, outputs_size, target_lengths)

File “C:\Users\lovis\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\module.py”, line 889, in _call_impl

result = self.forward(*input, **kwargs)

File “C:\Users\lovis\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\loss.py”, line 1590, in forward

return F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,

File “C:\Users\lovis\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\functional.py”, line 2307, in ctc_loss

return torch.ctc_loss(

RuntimeError: Expected 3-dimensional tensor, but got 2-dimensional tensor for argument #1 ‘log_probs’ (while checking arguments for ctc_loss_cpu)

The error is given as:

RuntimeError: Expected 3-dimensional tensor, but got 2-dimensional tensor for argument #1 ‘log_probs’ (while checking arguments for ctc_loss_cpu)

which points to a wrong shape for log_probs.
Checking the docs of nn.CTCLoss gives the expected shape for log_probs as:

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 torch.nn.functional.log_softmax()).

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:

MODEL NETWORK
import torch

import torch.nn as nn

from torch.nn.modules import dropout

from torch.nn.modules.dropout import Dropout, Dropout2d

train_on_gpu = torch.cuda.is_available()

class CRNN(nn.Module):

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

model = CRNN()

print(model)

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

Since you are seeing NaN losses, you could check this post which explains common things to check when using CTCLoss.