一步真实解决RuntimeError: view size is not compatible with input tensor‘s size and stride

在python或者Colab运行咱们程序时候

可能报错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.

这里浅浅记录一下~~~(每次改完过一段时间又忘记了,我觉得有必要给它记个笔记,以供自己后面复习)

报错代码如图:一步真实解决RuntimeError: view size is not compatible with input tensor‘s size and stride

根据报错的上方找到代码具体出错位置

一步真实解决RuntimeError: view size is not compatible with input tensor‘s size and stride
出现这个原因主要就是因为view()需要Tensor中的元素地址是连续的,因为可能出现Tensor不连续的情况,所以修改方法为:
在.view前加.contiguous(),使其变为连续就ok。

最后的效果如下:
一步真实解决RuntimeError: view size is not compatible with input tensor‘s size and stride

 到此就解决了问题,成功解决bug~~~

更多干货,可以关注公众号“蓝胖胖干货”,教你账号注册,快速上网等秘籍~~~咱们一起成长!

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年3月10日
下一篇 2023年3月10日

相关推荐