Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading