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

阅读: 评论:0

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

在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.

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

报错代码如图:

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


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

最后的效果如下:

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

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

本文发布于:2024-02-01 17:44:57,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170678154438391.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:真实   view   RuntimeError   size   stride
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23