一步真实解决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

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

出现这个原因主要就是因为view()需要Tensor中的元素地址是连续的,因为可能出现Tensor不连续的情况,所以修改方法为:
在.view前加.contiguous(),使其变为连续就ok。

最后的效果如下:

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

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

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

Original: https://blog.csdn.net/Thebest_jack/article/details/124773210
Author: 蓝胖胖▸
Title: 一步真实解决RuntimeError: view size is not compatible with input tensor‘s size and stride

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/730693/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球