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)