torch.view和numpy.reshape区别

相同之处
都可以用来重新调整 tensor 的形状。
不同之处
1.view 函数只能用于 contiguous 后的 tensor 上,也就是只能用于内存中连续存储的 tensor。如果对 tensor 调用过 transpose, permute 等操作的话会使该 tensor 在内存中变得不再连续,此时就不能再调用 view 函数。因此,需要先使用 contiguous 来返回一个 contiguous copy。
2.reshape 则不需要依赖目标 tensor 是否在内存中是连续的。

posted @ 2021-08-02 09:24  吴莫愁258  阅读(381)  评论(0编辑  收藏  举报