Search code examples
pythonnumpypytorchnaming-conventions

What is the rationale behind the naming of the squeeze and unsqueeze operations?


I was wondering if there is some (mathematical, historical, etc) reason behind the operations to remove and add singleton dimensions being called squeeze and unsqueeze. I am mainly asking from a torch perspective but it seems that those names are used also in many other programming languages and libraries.

Especially because you could easily make a case for the naming to be reversed and you would not notice much of a difference. It would make even more sense to me that an operation that removes singleton dimensions would be called un-squeeze.

It seems that something more descriptive like add_singleton_dimension and remove_singleton_dimension would help to better convey the meaning of those operations (like the numpy function expand_dims does).

So, is this because the names squeeze and unsqueeze:

  • have been inherited from other programming languages/paradigms
  • are defined and used in mathematical language
  • retrocompatibility with existing libraries

Solution

  • I know numpy, not pytorch. But my guess is the pytorch borrowed squeeze from numpy, and invented unsqueeze to do the 'reverse'.

    numpy has lots of ways of adding size 1 dimensions - arr[:,None,:], reshape, and reshape wrapper expand_dims.

    With the shape/strides approach to multidimensional arrays, adding and removing size 1 dimensions is a trivial operation.

    Talking about 'squeezing out all size 1 dimensions' sounds obvious to me (a native English speaker). numpy where arrays can have 0 or more dimensions is a natural place for this. MATLAB probably didn't add this function until it allowed more than 2 dimensions. Even then 2 is the lower boundary. Specifying which dimensions you want to remove is also a later addition to numpy.

    Many array operations have physical equivalents. Squeeze brings to mind squeezing water out a sponge. Flatten is obvious. Ravel is another, though English blurs the distinction between that and 'unravel'. Ultimately those analogies suggest where the name comes from, but shouldn't be used as definitions.