Hi. Thanks for your help. At last i am able to solve the size issues with my code. But now i have new issue, this code is just giving me the training loss, not the test results. I am running this code. This give me the result for training loss. But when i print test loss and accuracy it returns ‘Nan’ as output. Please guide me why this code is not giving me the test results. Please guide if this code is giving me the training results, then it should give the test results also.
import torch
import torch.nn as nn
import torch.nn.functional as F

class  CharCNN(nn.Module):
    def __init__(self):
        super(CharCNN, self).__init__()
        
        self.conv1 = nn.Sequential(
            nn.Conv1d(num_channels, depth_1, kernel_size=kernel_size_1, stride=stride_size),
            nn.ReLU(),
            #nn.MaxPool1d(kernel_size=2, stride=stride_size),
            #nn.Dropout(0.1),
        )
        
        
        self.conv2 = nn.Sequential(
            nn.Conv1d(depth_1, depth_2, kernel_size=kernel_size_2, stride=stride_size),
            nn.ReLU(),
            nn.MaxPool1d(kernel_size=2, stride=stride_size),
            #nn.Dropout(0.25)
        )            
               
        self.fc1 = nn.Sequential(
            nn.Linear((depth_2)*6, num_hidden),
            nn.ReLU(),
            nn.Dropout(0.5)
        )
        
        self.fc2 = nn.Sequential(
            nn.Linear(num_hidden, num_labels),
            nn.ReLU(),
            #nn.Dropout(0.5)
        )
        
        self.log_softmax = nn.LogSoftmax(1)

    def forward(self, x):
        
        x = self.conv1(x)
        
        x = self.conv2(x)
        
        #y= x.size()
        #print (y)
        
        #x = x.reshape(-1,16384)
        #x = x.view(x.size(0), -1) 
        x = x.view(x.size(0),-1)
        # linear layer
        x = self.fc1(x)
        # output layer
        x = self.fc2(x)
        #out = self.log_softmax(x,dim=1)
        x = self.log_softmax(x)
        return x

    model =  CharCNN()


criterion = nn.CrossEntropyLoss()    
opt = torch.optim.Adam(model.parameters(),lr=learning_rate)

    
for e in range(training_epochs):
    if(train_on_gpu):
        net.cuda()
    train_losses = []    
    
    for batch in iterate_minibatches(train_x, train_y, batch_size):
        x, y = batch
        
        #x= torch.randn(64,52,300)
        
        inputs, targets = torch.from_numpy(x), torch.from_numpy(y)
        if(train_on_gpu):
            inputs, targets = inputs.cuda(), targets.cuda()
            #inputs= inputs.view(batch_size,  128*64)
            #targets = targets.view(batch_size)
        opt.zero_grad()   
        
        
        output = model(inputs)
        
        #print(output.size())
        
        #output=output.squeeze(0)
            
        #loss = criterion(output.float(), targets.long())
        loss = criterion(output, torch.max(targets, 1)[1])
        train_losses.append(loss.item())
        loss.backward()
        opt.step()
        
    print("Epoch: {}/{}...".format(e+1, training_epochs),
        "Train Loss: {:.4f}...".format(np.mean(train_losses)))
    
    
    
test_losses = []              

model = model.eval()
with torch.no_grad():
    accuracy=0
    f1score=0
    for batch in iterate_minibatches(test_x, test_y, batch_size):
        x1, y1 = batch     
        inputs, targets = torch.from_numpy(x1), torch.from_numpy(y1)
    
        if(train_on_gpu):
            inputs, targets = inputs.cuda(), targets.cuda()
            output = model(inputs)
            
            test_loss = criterion(output, torch.max(targets, 1)[1])
            
            
            test_losses.append(test_loss.item())
            

            top_p, top_class = output.topk(1, dim=1)
            equals = top_class == targets.view(*top_class.shape).long()
            accuracy += torch.mean(equals.type(torch.FloatTensor))
            f1score += metrics.f1_score(top_class.cpu(), targets.view(*top_class.shape).long().cpu(), 
            average='weighted')        
                
print("Epoch: {}/{}...".format(e+1, training_epochs),
        "test Loss: {:.4f}...".format(np.mean(test_losses)),
       "test Acc: {:.4f}...".format(accuracy/(len(test_x)//batch_size)),
       "F1-Score: {:.4f}...".format(f1score/(len(test_x)//batch_size)))```