查看: 1910|回复: 1

input is not contiguous at /pytorch/torch/lib/THC/generic/THCTensor.c:227

[复制链接]

6

主题

21

帖子

65

积分

注册会员

Rank: 2

积分
65
发表于 2018-10-15 17:24:11 | 显示全部楼层 |阅读模式
pytroch新手,写的一小段代码,发现报错:
  1. batch_size, c, h, w = input.size()
  2. rh, rw = (2, 2)
  3. oh, ow = h * rh, w * rw
  4. oc = c // (rh * rw)
  5. out = input.view(batch_size, rh, rw, oc, h, w)
  6. out = out.permute(0, 3, 4, 1, 5, 2)
  7. out = out.view(batch_size, oc, oh, ow)
复制代码
报错如下:
  1. invalid argument 2: input is not contiguous at /pytorch/torch/lib/THC/generic/THCTensor.c:227
复制代码
求大神告知

回复

使用道具 举报

4

主题

34

帖子

88

积分

注册会员

Rank: 2

积分
88
发表于 2018-10-15 17:26:12 | 显示全部楼层
上述在第7行报错,报错原因是由于浅拷贝。上面式子中input为Variable变量。

上面第5行
  1. out = out.permute(0, 3, 4, 1, 5, 2)
复制代码

时执行了浅拷贝,out只是复制了out从input传递过来的指针,也就是说input要改变out也要随之改变。

解决方法是:
在第6行的时候使用tensor.contiguous(),第6行改成:
  1. out = out.permute(0, 3, 4, 1, 5, 2).contiguous()
复制代码


即可。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表