Is .contiguous().flatten() the same as .view(-1) in PyTorch?

Viewed 666

Are these exactly the same?

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

Will they return the same auto grad function etc?

2 Answers

No, they are not exactly the same.

  • myTensor.contiguous().flatten():

Here, contiguous() either returns a copy of myTensor stored in contiguous memory, or returns myTensor itself if it is already contiguous. Then, flatten() reshapes the tensor to a single dimension. However, the returned tensor could be the same object as myTensor, a view, or a copy, so the contiguity of the output is not guaranteed.

Relevant 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):

Here, view() returns a tensor with the same data as myTensor, and will only work if myTensor is already contiguous. The result may not be contiguous depending on the shape of myTensor.

Some illustration:

xxx = torch.tensor([[1], [2], [3]])
xxx = xxx.expand(3, 4)
print ( xxx )
print ( xxx.contiguous().flatten() )
print ( xxx.view(-1) )


tensor([[1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3]])
tensor([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-7-281fe94f55fb> in <module>
      3 print ( xxx )
      4 print ( xxx.contiguous().flatten() )
----> 5 print ( xxx.view(-1) )

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.
Related