.flatten()
和 .view(-1)
都在 PyTorch 中展平张量。有什么区别?
.flatten()
是否复制张量的数据?.view(-1)
更快吗?- 是否存在
.flatten()
不起作用的情况?
最佳答案
除了@adeelh 的评论之外,还有另一个区别:torch.flatten()
导致 .reshape()
,而 differences between .reshape()
and .view()
是:
[...]
torch.reshape
may return a copy or a view of the original tensor. You can not count on that to return a view or a copy.Another difference is that reshape() can operate on both contiguous and non-contiguous tensor while view() can only operate on contiguous tensor. Also see here about the meaning of contiguous
对于上下文:
社区在一段时间内请求
flatten
功能,在Issue #7743 之后,该功能已在 PR #8578 中实现.可以看到flatten的实现here ,其中可以在
return
行中看到对.reshape()
的调用。
关于python - PyTorch 中的 .flatten() 和 .view(-1) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57234095/