# TypeError: \_\_init\_\_() got multiple values for argument 'dim'

**URL:** <https://discuss.pytorch.org/t/typeerror-init-got-multiple-values-for-argument-dim/166161>\
**Category:** vision\
**Created:** [November 17, 2022, 2:17am UTC](https://discuss.pytorch.org/t/typeerror-init-got-multiple-values-for-argument-dim/166161 "2022-11-17T02:17:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khawar\_Islam](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/khawar_islam/32/27640_2.png) [@Khawar\_Islam](https://discuss.pytorch.org/u/Khawar_Islam)\
**Post date:** [November 17, 2022, 2:17am UTC](https://discuss.pytorch.org/t/typeerror-init-got-multiple-values-for-argument-dim/166161/1 "2022-11-17T02:17:47Z")

</div>

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**

```auto
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) -> None:
        """
        the data folder should look like
        - datafolder
            - Images
            - labels.csv        
        """
        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__ == " __main__":
    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

```auto
  return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
Traceback (most recent call last):
  File "/media/cvpr/CM_22/OOD_CV/test.py", line 93, in <module>
    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

```auto
  return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
Traceback (most recent call last):
  File "/media/cvpr/CM_22/OOD_CV/test.py", line 93, in <module>
    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'

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [November 17, 2022, 2:23am UTC](https://discuss.pytorch.org/t/typeerror-init-got-multiple-values-for-argument-dim/166161/2 "2022-11-17T02:23:22Z")

</div>

You need to initialize the module first and call it later assuming you want to stick to the `nn.LogSoftmax` module:

```python
entropy1 = -torch.sum(torch.softmax(pred1[:, :10], dim=1) * nn.LogSoftmax(dim=1)(pred1[:, :10]), dim=-1,

```

---

<div class="post-metadata">

**Author:** ![Khawar\_Islam](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/khawar_islam/32/27640_2.png) [@Khawar\_Islam](https://discuss.pytorch.org/u/Khawar_Islam)\
**Post date:** [November 17, 2022, 2:29am UTC](https://discuss.pytorch.org/t/typeerror-init-got-multiple-values-for-argument-dim/166161/3 "2022-11-17T02:29:09Z")

</div>

I have also keep\_dim parameter but still getting an error.

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

```

**Traceback**

```auto
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

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [November 17, 2022, 2:30am UTC](https://discuss.pytorch.org/t/typeerror-init-got-multiple-values-for-argument-dim/166161/4 "2022-11-17T02:30:56Z")

</div>

The new error is unrelated to the previous one and points to a shape mismatch.  
Check the shape of each tensor in the calculation and try to narrow down which tensor(s) raise the error.
