view()和reshape()比力

分享
源码 2024-9-18 16:35:35 104 0 来自 中国
PyTorch Tensor自带的方法view()和reshape():

  • view(): Returns a new tensor with the same data as the self tensor but of a different shape.
  • reshape(): Returns a tensor with the same data and number of elements as self but with the specified shape.


利用下来,都是作用都是更改Tensor的Shape,而且性能没啥差异但是,view要求tensor元素连续,若不连续,则保举用reshape
Traceback (most recent call last):
File "d:/pytorch/tensor_reshape.py", line 9, in <module>
print(y.view(9))
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.
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-19 04:25, Processed in 0.147938 second(s), 32 queries.© 2003-2025 cbk Team.

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