TypeError: __init__() got multiple values for argument 'dim'
I am doing testing on two trained models. In first, I am getting below error during testing so I have changed torch.logsoftmax class to nn.LogSoftmax.
Code
from torch.utils.data import Dataset, DataLoader
import pandas as pd
from torchvision import transforms
from PIL import Image
import torch
import torch.nn as nn
from glob import glob
from pathlib import PurePath
import numpy as np
import timm
import torchvision
import time

img_list = glob('/media/cvpr/CM_22/OOD-CV-phase2/phase2-cls/images/*.jpg')

name_list = [
    'aeroplane',
    'bicycle',
    'boat',
    'bus',
    'car',
    'chair',
    'diningtable',
    'motorbike',
    'sofa',
    'train'
]

# conda install pytorch==1.9.0 torchvision==0.10.0 torchaudio==0.9.0 cudatoolkit=10.2 -c pytorch

class PoseData(Dataset):
    def __init__(self, transforms) -&gt; None:
        &quot;&quot;&quot;
        the data folder should look like
        - datafolder
            - Images
            - labels.csv        
        &quot;&quot;&quot;
        super().__init__()
        self.img_list = glob('/media/cvpr/CM_22/OOD-CV-phase2/phase2-cls/images/*.jpg')
        self.img_list = sorted(self.img_list, key=lambda x: eval(PurePath(x).parts[-1][:-4]))
        self.trs = transforms

    def __len__(self):
        return len(self.img_list)

    def __getitem__(self, index):
        image_dir = self.img_list[index]
        image_name = PurePath(image_dir).parts[-1]
        image = Image.open(image_dir)
        image = self.trs(image)

        return image, image_name


if __name__ == &quot;__main__&quot;:
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
    tfs = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        normalize,
    ])

    model1 = timm.models.swin_base_patch4_window7_224(pretrained=False, num_classes=15)
    model1 = torch.nn.DataParallel(model1)
    model1.load_state_dict(torch.load('/media/cvpr/CM_22/OOD_CV/swin15_best.pth.tar')['state_dict'],strict=False)
    model1 = model1.cuda()
    model1.eval()

    model2 = timm.models.convnext_base(pretrained=False, num_classes=15)
    model2 = torch.nn.DataParallel(model2)
    model2.load_state_dict(torch.load('convnext15_best.pth.tar')['state_dict'],strict=False)
    model2 = model2.cuda()
    model2.eval()

    dataset = PoseData(tfs)
    loader = DataLoader(dataset, batch_size=128, shuffle=False, drop_last=False, num_workers=4)

    image_dir = []
    preds = []
    for image, pth in loader:
        image_dir.append(list(pth))
        image = image.cuda()

        with torch.no_grad():

            model1.eval()
            pred1 = model1(image)
            model2.eval()
            pred2 = model2(image)

            entropy1 = -torch.sum(torch.softmax(pred1[:, :10], dim=1) * nn.LogSoftmax(pred1[:, :10], dim=1), dim=-1,
                                  keep_dim=True)
            entropy2 = -torch.sum(torch.softmax(pred2[:, :10], dim=1) * nn.LogSoftmax(pred2[:, :10], dim=1), dim=-1,
                                  keep_dim=True)
            entropy = entropy1 + entropy2

            pred = torch.softmax(pred1[:, :10], dim=1) * (entropy - entropy1) / entropy + torch.softmax(pred2[:, :10],
                                                                                                        dim=1) * (
                               entropy - entropy2) / entropy
            pred = torch.argmax(pred[:, :10], dim=1)
            p = []
            for i in range(pred.size(0)):
                p.append(name_list[pred[i].item()])
        p = np.array(p)
        preds.append(p)
        print(len(np.concatenate(preds)))

    image_dir = np.array(sum(image_dir, []))
    preds = np.concatenate(preds)

    csv = {'imgs': np.array(image_dir), 'pred': np.array(preds),
           }
    csv = pd.DataFrame(csv)
    print(csv)

    csv.to_csv('results.csv', index=False)

Traceback
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
Traceback (most recent call last):
  File &quot;/media/cvpr/CM_22/OOD_CV/test.py&quot;, line 93, in &lt;module&gt;
    entropy1 = -torch.sum(torch.softmax(pred1[:, :10], dim=1) * torch.logsoftmax(pred1[:, :10], dim=1), dim=-1,
AttributeError: module 'torch' has no attribute 'logsoftmax'

Due to PyTorch version conflict, I have replaced with recent PyTorch version but now getting dim error
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
Traceback (most recent call last):
  File &quot;/media/cvpr/CM_22/OOD_CV/test.py&quot;, line 93, in &lt;module&gt;
    entropy1 = -torch.sum(torch.softmax(pred1[:, :10], dim=1) * nn.LogSoftmax(pred1[:, :10], dim=1), dim=-1,
TypeError: __init__() got multiple values for argument 'dim'

After implementing
nn.LogSoftMax(dim=1)(pred1[:, :10])

Traceback
    entropy1 = -torch.sum(torch.softmax(pred1[:, :10], dim=1) * nn.LogSoftmax(dim=1)(pred1[:, :10]), dim=-1, keep_dim=True)
TypeError: sum() received an invalid combination of arguments - got (Tensor, keep_dim=bool, dim=int), but expected one of:
 * (Tensor input, *, torch.dtype dtype)
      didn't match because some of the keywords were incorrect: keep_dim, dim
 * (Tensor input, tuple of ints dim, bool keepdim, *, torch.dtype dtype, Tensor out)
 * (Tensor input, tuple of names dim, bool keepdim, *, torch.dtype dtype, Tensor out)

Then delete keep_dim=True parameter
Traceback
Traceback (most recent call last):
  File &quot;/media/cvpr/CM_22/OOD_CV/test.py&quot;, line 97, in &lt;module&gt;
    pred = torch.softmax(pred1[:, :10], dim=1) * (entropy - entropy1) / entropy + torch.softmax(pred2[:, :10],
RuntimeError: The size of tensor a (10) must match the size of tensor b (128) at non-singleton dimension 1


nn.LogSoftMax is a module that has to be instantiated first and then called (which is when its forward method is executed). Try this instead:
entropy1 = -torch.sum(torch.softmax(pred1[:, :10], dim=1) * nn.LogSoftmax(dim=1)(pred1[:, :10]), dim=-1, keepdim=True)

Instead, you can also use the functional form of this module:
torch.nn.functional.log_softmax(pred1[:, :10], dim=1)

