Squeeze

Shipping
tensor_squeeze

Drop size-1 axes from an array — all of them, or a single specified axis (numpy squeeze)

Signature

Inputs

  • aVector|Matrix|TensorrequiredThe array whose size-1 axes are removed, lowering the rank.

Outputs

  • resultVector|Matrix|TensorThe array with size-1 axes dropped. Unit preserved.

Parameters

KeyTypeDefaultNotes
axistextA single axis to drop (must be size-1; negative counts from the end). Empty = drop ALL size-1 axes.

Description

Squeeze removes size-1 axes from an array, lowering its rank — the numpy squeeze operation. It is a pure reindexing / metadata op: the flat data is unchanged, so the unit is preserved.

With axis empty, it drops every size-1 axis (e.g. a tensor becomes a length-3 Vector). With a specific axis (possibly negative), it drops just that one axis — and errors if that axis is not size-1, so a squeeze can never silently discard data. This is the inverse of tensor_expand_dims, and is the standard way to collapse a row or column back to a plain Vector after a matrix stage.

Mathematics

Examples

Drop all size-1 axes

A tensor [1,2,3] with empty axis squeezes to a length-3 Vector [1,2,3].

Drop one axis (guarded)

Squeezing axis=1 of a tensor errors — axis 1 has length 3, not 1 — so no data is silently lost. axis=0 or axis=2 succeed.

Applications

  • Collapsing a $1 \times n$ row or $n \times 1$ column result back to a plain Vector after matrix math.
  • Removing residual singleton batch/channel axes before a 1-D processing stage.
  • Cleaning up array rank after operations that leave trailing size-1 dimensions.
  • Undoing an expand-dims once its purpose (broadcasting/alignment) is served.

Neat

Squeezing a specific non-size-1 axis is a hard error, not a no-op — the node refuses to pretend it dropped an axis it couldn't.

Like expand-dims, it moves zero floats and preserves the unit; only the shape metadata changes.

Known issues

A non-integer `axis` string is an InvalidParam error.

Requesting a specific axis that is not length 1 errors rather than falling back to dropping nothing.

See also

squeezeaxisnumpytensorshapestateless