Einops: A New Flavor of Deep Learning Operations

0.8.2 · active · verified Sun Mar 29

Einops (Einstein operations) is a Python library that provides a flexible and powerful way to reshape and manipulate tensors in deep learning frameworks like PyTorch, TensorFlow, JAX, and NumPy. It simplifies complex tensor operations using a human-readable notation, often replacing verbose permutations, transpositions, and reshaping operations. The library is actively maintained with frequent releases, currently at version 0.8.2.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core `rearrange` operation using NumPy. It shows how to combine dimensions (e.g., batch and height) and flatten dimensions (e.g., width and channel) using a concise, readable pattern string.

import numpy as np
from einops import rearrange

# Suppose we have a batch of 6 images, each 96x96 with 3 color channels
images = np.random.randn(6, 96, 96, 3)
print(f"Original shape: {images.shape}")

# Rearrange to stack images vertically (batch and height become one dimension)
stacked_images = rearrange(images, 'b h w c -> (b h) w c')
print(f"Stacked shape: {stacked_images.shape}")

# Alternatively, flatten width and channel for a 2D representation
flattened_data = rearrange(images, 'b h w c -> b h (w c)')
print(f"Flattened shape: {flattened_data.shape}")

view raw JSON →