gpt4 book ai didi

pytorch - PyTorch 中的 .contigious().flatten() 与 .view(-1) 相同吗?

转载 作者:行者123 更新时间:2023-12-02 02:15:42 24 4
gpt4 key购买 nike

这些完全一样吗?

myTensor.contiguous().flatten()
myTensor.view(-1)

他们会返回相同的自动分级功能等吗?

最佳答案

不,它们并不完全相同。

  • myTensor.contigious().flatten():

在这里,contiguous()返回存储在连续内存中的 myTensor 副本,或者返回 myTensor 本身(如果它已经是连续的)。然后,flatten()将张量 reshape 为单一维度。但是,返回的张量可能与 myTensor 相同的对象、 View 或副本,因此无法保证输出的连续性。

相关documentation :

It’s also worth mentioning a few ops with special behaviors:

  • reshape(), reshape_as() and flatten() can return either a view or new tensor, user code shouldn’t rely on whether it’s view or not.

  • contiguous() returns itself if input tensor is already contiguous, otherwise it returns a new contiguous tensor by copying data.

  • myTensor.view(-1):

在这里,view()返回与 myTensor 具有相同数据的张量,并且仅当 myTensor 已经连续时才有效。结果may not be contiguous取决于 myTensor 的形状。

关于pytorch - PyTorch 中的 .contigious().flatten() 与 .view(-1) 相同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67214586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com