How to?

einops

Jul 6, 2026
langspython
2 Minutes
227 Words
1
We don't write:
2
y = x.transpose(0, 2, 3, 1)
3
We write comprehensible code:
4
y = rearrange(x, 'b c h w -> b h w c')
5
Also:
6
rearrange(ims, "b h w c -> h (b w) c").shape
7
rearrange(ims, "(b1 b2) h w c -> b1 b2 h w c ", b1=2).shape
8
reduce(ims, "b h w c -> h w c", "mean") # Average overbatch
9
reduce(ims, "b h w c -> h w", "min")

and einx

1
z = einx.id("a (b c) -> (b a) c", x, b=2) # Permute and (un)flatten axes
2
z = einx.id("b (q + k) -> b q, b k", x, q=2) # Split
3
z = einx.id("b c, -> b (c + 1)", x, 42) # Append number to each channel
4
z = einx.sum("a [b]", x) # Sum-reduction along second axis
5
z = einx.flip("... (g [c])", x, c=2) # Flip pairs of values along the last axis
6
z = einx.mean("b [...] c", x) # Spatial mean-pooling
7
z = einx.multiply("a..., b... -> (a b)...", x, y) # Kronecker product
8
z = einx.sum("b (s [ds])... c", x, ds=(2, 2)) # Sum-pooling with 2x2 kernel
9
z = einx.add("a, b -> a b", x, y) # Outer sum
10
z = einx.dot("a [b], [b] c -> a c", x, y) # Matrix multiplication
11
z = einx.get_at("b [h w] c, b i [2] -> b i c", x, y) # Gather values at coordinates
Article title:einops
Article author:Julyfun
Release time:Jul 6, 2026
Copyright 2026
Sitemap