I am getting this error -
 output = model.forward(images)
---> 90     conf_matrix = confusion_matrix(output, labels, conf_matrix)
     91     p = torch.nn.functional.softmax(output, dim=1)
     92     prediction = torch.argmax(p, dim=1)

 50     preds = torch.argmax(preds, 1)
     51     for p, t in zip(preds, labels):
---> 52         conf_matrix[p, t] += 1
     53 
     54     #print(conf_matrix)

I have implemented a Transfer Learning with VGG16 and tried to print precision, recall, and conf_matrix.  I have printed Vgg16 from models and used it.
model = models.vgg16(pretrained=True)

for param in model.parameters():
    param.requires_grad = True

model.fc = nn.Sequential(nn.Linear(25088, 4096),
                                 nn.ReLU(),
                                 nn.Dropout(0.4),
                                 nn.Linear(4096,4096),
                                 nn.ReLU(),
                                 nn.Dropout(0.4),
                                 nn.Linear(4096,39),
                                 nn.LogSoftmax(dim=1))

I am really not getting where is exactly the problme is with the Conf_matrix
def confusion_matrix(preds, labels, conf_matrix, title='Confusion matrix', cmap=plt.cm.Blues):
    preds = torch.argmax(preds, 1)
    for p, t in zip(preds, labels):
        conf_matrix[p, t] += 1

    #print(conf_matrix)
    #plt.imshow(conf_matrix)
    TP = conf_matrix.diag()
    for c in range(n_classes):
        idx = torch.ones(n_classes).byte()
        idx[c] = 0
        TN = conf_matrix[idx.nonzero()[:,None], idx.nonzero()].sum()
        FP = conf_matrix[c, idx].sum()
        FN = conf_matrix[idx, c].sum()

        Recall = (TP[c] / (TP[c]+FN))
        precision = (TP[c] / (TP[c]+FP))
        f1 = (2 * ((precision * Recall)/(precision + Recall)))

        #print('Class {}\nTP {}, TN {}, FP {}, FN {}'.format(c, TP[c], TN, FP, FN))
        #print('Sensitivity = {}'.format(sensitivity))
        #print('Specificity = {}'.format(specificity))
            
    return conf_matrix

conf_matrix = torch.zeros(n_classes, n_classes)

Lastly, this is my training loop
for images, labels in dataloader_train:
    
    #steps += 1
    images, labels = images.to(device), labels.to(device)
    
    optimizer.zero_grad()
    
    output = model.forward(images)
    conf_matrix = confusion_matrix(output, labels, conf_matrix)
    p = torch.nn.functional.softmax(output, dim=1)
    prediction = torch.argmax(p, dim=1)
    #loss = torch.nn.functional.nll_loss(torch.log(p), y)
    loss = criterion(output, labels)
    loss.backward()
    optimizer.step()
    
    train_loss += loss.item()*images.size(0)

Any help is appriciaed. Thank you.