gitmyhub

einops

Python ★ 9.5k updated 21d ago

Flexible and powerful tensor operations for readable and reliable code (for pytorch, jax, TF and others)

A Python library that lets you reshape and rearrange multi-dimensional arrays using simple readable text patterns instead of confusing numbers, works with PyTorch, NumPy, JAX, TensorFlow, and more.

PythonNumPyPyTorchJAXTensorFlowPaddleMLXsetup: easycomplexity 2/5

Einops is a Python library that makes it easier to write and read code that reshapes, transposes, or combines multi-dimensional arrays (called tensors) used in machine learning. In machine learning, data is constantly being sliced, rearranged, and combined across many dimensions, and the standard ways to do this tend to produce code that is hard to follow. Einops offers a text-based notation where you write out the shape transformation you want in a readable pattern, and the library figures out how to perform it.

The core idea borrows from a mathematical notation called Einstein summation, where you describe an operation by naming the dimensions explicitly rather than passing a series of numbers and flags. For example, instead of writing a reshape command with magic numbers, you write something like 'batch channels height width -> batch (channels height width)' and the meaning is clear from the pattern itself. The three main functions are rearrange (for reordering and reshaping), reduce (for reducing dimensions by averaging or summing), and repeat (for expanding a tensor along a new axis).

The library works with the major Python array frameworks: NumPy, PyTorch, JAX, TensorFlow, Paddle, MLX, tinygrad, and others. This means the same einops notation works regardless of which underlying framework a project uses. It also provides layer objects so you can slot these operations directly into a neural network model definition.

Installation is a single pip command. The project ships four tutorial notebooks covering basics, deep learning use cases, packing and unpacking multiple arrays, and PyTorch-specific examples. A companion website hosts documentation and testimonials, and the project has been adopted in over 10,000 other GitHub repositories.

A related paper was accepted at ICLR 2022 and received an oral presentation, which is a competitive distinction in the machine learning research community. The project is actively maintained with recent updates adding new framework backends and compatibility with PyTorch's compilation tools.

Where it fits