Are several subsequent `reshape` operations equivalent to only one?

In Pytorch,
I recently stumbled onto code that looks like this.

# initially, my_tensor is one_dimensional, of length b*x*y 
my_tensor = my_tensor.reshape(b, x, y)
my_tensor = my_tensor.reshape(b, x*y)

Is it equivalent to only writing the second line?

my_tensor = my_tensor.reshape(b, x*y)

And in general, is doing several reshape operations always equivalent to only doing the last one?

Intuitively, I think so, but the documentation for reshape doesn’t really mention any invariant, and I couldn’t find information for the inner representation of tensors and how reshape changed that

>Solution :

The reshape operation does not (need to) touch the underlying data. It simply adjusts the "meta-data" about the dimensions. So a series of reshape operations (without any operations in between!!) is equal to a single reshape operation.

Leave a Reply