RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 0

MalviyaR: <p>In the first run, the code ran well, but from the second run the system has started throwing error in the following:</p>
<p>for i, (inputs, labels) in enumerate(datasets[‘test’]):<br>
print(labels)</p>
<p>datasets[‘test’] has multiple objects mainly</p>
<ol>
<li>data which is a tensor of size [20000, 32, 32, 3]</li>
<li>labels which is an ndarray of size [20000]</li>
</ol>
<p>I am working on pytorch, python 3.6, windows 10</p>

ptrblck: <p>Could you post the complete error message including the stack trace, please?<br>
Based on the title, the shape mismatch is raised in <code>dim0</code>, but it’s hard to guess what exactly is failing.</p>

MalviyaR: <p>Thanks for your response.<br>
I am using the following repo with customizations for my data:</p><aside class="onebox githubfolder">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">
      <a href="https://github.com/fabio-deep/Variational-Capsule-Routing" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/423;"><img src="https://repository-images.githubusercontent.com/223196605/5ea87a00-46a1-11ea-88d9-946455888a06" class="thumbnail" width="690" height="423"></div>

<h3><a href="https://github.com/fabio-deep/Variational-Capsule-Routing" target="_blank" rel="noopener nofollow ugc">fabio-deep/Variational-Capsule-Routing</a></h3>


  <p><span class="label1">Official Pytorch code for (AAAI 2020) paper "Capsule Routing via Variational Bayes", https://arxiv.org/pdf/1905.11455.pdf</span></p>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<p>
When I run the main.py I get the same error as I get while compiling in console line by line…<br>
Following is the complete stack of error:</p>
<p>Traceback (most recent call last):<br>
File “F:/Richa/Richa_Python_codes/Variational-Capsule-Routing/src/main_r.py”, line 136, in <br>
score = main(parser.parse_known_args()[0])<br>
File “F:/Richa/Richa_Python_codes/Variational-Capsule-Routing/src/main_r.py”, line 107, in main<br>
score = train(model, dataloaders, args)<br>
File “F:\Richa\Richa_Python_codes\Variational-Capsule-Routing\src\train.py”, line 103, in train<br>
test_loss, test_acc = evaluate(model, args, dataloaders[‘test’])<br>
File “F:\Richa\Richa_Python_codes\Variational-Capsule-Routing\src\evaluate.py”, line 13, in evaluate<br>
for i, (inputs, labels) in enumerate(dataloader):<br>
File “C:\Users\user.conda\envs\gpu-test4\lib\site-packages\torch\utils\data\dataloader.py”, line 435, in <strong>next</strong><br>
data = self._next_data()<br>
File “C:\Users\user.conda\envs\gpu-test4\lib\site-packages\torch\utils\data\dataloader.py”, line 475, in _next_data<br>
data = self.<em>dataset_fetcher.fetch(index)  # may raise StopIteration<br>
File “C:\Users\user.conda\envs\gpu-test4\lib\site-packages\torch\utils\data_utils\fetch.py”, line 44, in fetch<br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “C:\Users\user.conda\envs\gpu-test4\lib\site-packages\torch\utils\data_utils\fetch.py”, line 44, in <br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “F:\Richa\Richa_Python_codes\Variational-Capsule-Routing\src\datasets_r.py”, line 120, in <strong>getitem</strong><br>
image = self.transform(self.data[idx])<br>
File “C:\Users\user.conda\envs\gpu-test4\lib\site-packages\torchvision\transforms\transforms.py”, line 67, in <strong>call</strong><br>
img = t(img)<br>
File “C:\Users\user.conda\envs\gpu-test4\lib\site-packages\torch\nn\modules\module.py”, line 727, in <em>call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “C:\Users\user.conda\envs\gpu-test4\lib\site-packages\torchvision\transforms\transforms.py”, line 226, in forward<br>
return F.normalize(tensor, self.mean, self.std, self.inplace)<br>
File “C:\Users\user.conda\envs\gpu-test4\lib\site-packages\torchvision\transforms\functional.py”, line 284, in normalize<br>
tensor.sub</em>(mean).div</em>(std)<br>
RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 0</p>

ptrblck: <p>Thanks for posting the stack trace.<br>
It seems <code>normalize</code> raises the shape mismatch error.<br>
Could you check how many values you’ve passed to <code>transform.Normalize</code> and the number of channels for each input image?<br>
It seems that you might have used two values for the <code>mean</code> and <code>std</code> in <code>Normalize</code> while your input contains 3 channels as seen here:</p>
<pre><code class="lang-python">x = torch.randn(3, 24, 24)
norm = transforms.Normalize((0.5, 0.5), (0.5, 0.5))
out = norm(x)
&gt; RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 0
</code></pre>

MalviyaR: <p>Thanks a lot for your suggestion!<br>
I was making exactly the mistake you have pointed out here during normalization but when I ran the code it threw another error:<br>
RuntimeError: multi-target not supported at C:/cb/pytorch_1000000000000/work/aten/src\THCUNN/generic/ClassNLLCriterion.cu:15</p>
<p>I checked other posts where you have mentioned one-hot is not applicable, mine are labelled images where images and labels are separate variables.</p>
<p>Full stack of the same is the following:<br>
Traceback (most recent call last):<br>
File “F:/Richa/Richa_Python_codes/Variational-Capsule-Routing/src/main_r.py”, line 136, in <br>
score = main(parser.parse_known_args()[0])<br>
File “F:/Richa/Richa_Python_codes/Variational-Capsule-Routing/src/main_r.py”, line 107, in main<br>
score = train(model, dataloaders, args)<br>
File “F:\Richa\Richa_Python_codes\Variational-Capsule-Routing\src\train.py”, line 103, in train<br>
test_loss, test_acc = evaluate(model, args, dataloaders[‘test’])<br>
File “F:\Richa\Richa_Python_codes\Variational-Capsule-Routing\src\evaluate.py”, line 22, in evaluate<br>
loss = F.cross_entropy(yhat, labels.cuda())<br>
File “C:\Users\user.conda\envs\gpu-test4\lib\site-packages\torch\nn\functional.py”, line 2468, in cross_entropy<br>
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)<br>
File “C:\Users\user.conda\envs\gpu-test4\lib\site-packages\torch\nn\functional.py”, line 2264, in nll_loss<br>
ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
RuntimeError: multi-target not supported at C:/cb/pytorch_1000000000000/work/aten/src\THCUNN/generic/ClassNLLCriterion.cu:15</p>

ptrblck: <p>The new error is raised, e.g. if you are passing a target with an additional dimension (which would be the case for a one-hour encoded target).<br>
For a standard multi-classification use case the model output should have the shape <code>[batch_size, nb_classes]</code>, while the target should contain the class indices in the range <code>[0, nb_classes]</code> in the shape <code>[batch_size]</code>.</p>

MalviyaR: <p>Thanks a lot for your suggestion, my target has shape [batch_size, test_sample_size] which is creating problem, although my model gives the output in the shape [batch_size, nb_classes]</p>