RuntimeError when use view after permute

I have a Tensor [batch, 512, w, h], and I want to change it size to (-1,512) to through Linear layer,

fusion_layer = fusion_layer.permute(2, 3, 0, 1)
fusion_layer = fusion_layer.view(-1, 512)
fusion_layer = self.bn1(self.fc1(fusion_layer))
fusion_layer = fusion_layer.view(w, h, -1, 256)

but when I run the code, there is a RuntimeError:

File “/home/sfwu/PycharmProjects/colorNet/colornet.py”, line 119, in forward
fusion_layer = fusion_layer.view(-1, 512)
File “/home/sfwu/anaconda3/lib/python3.5/site-packages/torch/autograd/variable.py”, line 471, in view
return View(*sizes)(self)
File “/home/sfwu/anaconda3/lib/python3.5/site-packages/torch/autograd/_functions/tensor.py”, line 98, in forward
result = i.view(*self.sizes)
RuntimeError: input is not contiguous at /b/wheel/pytorch-src/torch/lib/TH/generic/THTensor.c:231

so what’s wrong?

Hi,

The problem is that view() can only be called on a contiguous tensor.
You should add fusion_layer = fusion_layer.contiguous() between the permute() and view() calls.

1 Like

Thanks, It does work!