View size is not compatible with input tensor's size and stride

tree5680: <p>i edited .view() to .reshape() but same error still occured…</p>
<pre><code class="lang-auto">def accuracy(output, target, topk=(1,)):
  maxk = max(topk)
  batch_size = target.size(0)

  _, pred = output.topk(maxk, 1, True, True)
  pred = pred.t()
  correct = pred.eq(target.view(1, -1).expand_as(pred))

  res = []
  for k in topk:
    correct_k = correct[:k].reshape(-1).float().sum(0)
    res.append(correct_k.mul_(100.0/batch_size))
  return res
</code></pre>
<p>full error sentence:<br>
RuntimeError: view size is not compatible with input tensor’s size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(…) instead.</p>
<p>i don’t know why it still cry an error like this…</p>

ptrblck: <p>The posted error is raised, if the tensor it not contiguous in memory and thus <code>view</code> will fail.<br>
You could either use <code>.reshape</code> instead as suggested in the error message or <code>.contiguous().view(...)</code> alternatively.</p>

tree5680: <p>thank you! ptrblck!<br>
reshape doesn’t work for me but .contiguous() works! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

HungNguyen: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a>, it works</p>

abhiskl: <p>Neither .contiguous().view(…) nor .reshape(…) is working. I am using google colab. How to make it work?</p>
<p>def accuracy(output, target, topk=(1,)):<br>
maxk = max(topk)<br>
batch_size = target.size(0)</p>
<p>_, pred = output.topk(maxk, 1, True, True)<br>
pred = pred.t()<br>
correct = pred.eq(target.view(1, -1).expand_as(pred))<br>
print(correct.shape)</p>
<p>res = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for k in topk:<br>
correct_k = correct[:k].reshape(-1).float().sum(0)<br>
res.append(correct_k.mul_(100.0/batch_size))<br>
return res</p>

ptrblck: <p>What exactly does not work?<br>
You would need to explain your issues with more details as your post shows code, which is neither properly formatted nor executable.</p>

abhiskl: <p>I am calling the same function i.e. accuracy(output, target, topk=(1,)); and  same runtime error is reported as that reported by tree5680:</p>
<p>I checked with both changes i.e. .view(…) to .reshape(…) and  .view(…) to .contiguous().view(…)</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/9/6928bd96c978caf14846a88e30938b17847eeb77.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/6928bd96c978caf14846a88e30938b17847eeb77" title="1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/6928bd96c978caf14846a88e30938b17847eeb77_2_690x89.jpeg" alt="1" data-base62-sha1="f0hq58BLO261uOs3r8RLsv7GTMX" width="690" height="89" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/6928bd96c978caf14846a88e30938b17847eeb77_2_690x89.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/6928bd96c978caf14846a88e30938b17847eeb77_2_1035x133.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/6928bd96c978caf14846a88e30938b17847eeb77_2_1380x178.jpeg 2x" data-dominant-color="F8F9F8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">1</span><span class="informations">1645×214 39.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Could you post a minimal and executable code snippet reproducing the issue as I cannot trigger the error using:</p>
<pre data-code-wrap="python"><code class="lang-python">pred = torch.randint(0, 2, (10, 10,))
target = torch.randint(0, 2, (1, 10))

correct = pred.eq(target.view(1, -1).expand_as(pred))
print(correct.shape)
# torch.Size([10, 10])

k = 3
correct_k = correct[:k].reshape(-1).float().sum(0)
print(correct_k)
# tensor(15.)
</code></pre>

abhiskl: <p>prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))<br>
print(prec1)</p>
<h1><a name="tensor93750-devicecuda0-1" class="anchor" href="#tensor93750-devicecuda0-1"></a>tensor(9.3750, device=‘cuda:0’)</h1>
<p>print(prec5)</p>
<h1><a name="tensor437500-devicecuda0-2" class="anchor" href="#tensor437500-devicecuda0-2"></a>tensor(43.7500, device=‘cuda:0’)</h1>
<p>Thank you very much ptrblck for your time. I tried with another google account to use google colab, and surprisingly, both the alternatives of .view() as suggested by you worked.</p>
<p>Regards,<br>
abhiskl</p>