view()和reshape()比力

分享
源码 2024-9-18 16:35:35 260 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, 2025-8-14 00:27, Processed in 0.155663 second(s), 31 queries.© 2003-2025 cbk Team.

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