#deep-learning #tensor #tch

einops

Simplistic API for deep learning tensor operations

5 unstable releases

Uses new Rust 2021

0.3.0-alpha.2 Jun 23, 2022
0.3.0-alpha.1 Dec 31, 2021
0.2.1 Sep 4, 2021
0.2.0 Sep 4, 2021
0.1.0 Apr 7, 2021

#45 in Machine learning

Download history 1/week @ 2022-03-09 13/week @ 2022-03-16 15/week @ 2022-03-23 1/week @ 2022-03-30 3/week @ 2022-04-06 1/week @ 2022-04-13 22/week @ 2022-04-27 28/week @ 2022-05-04 62/week @ 2022-05-11 19/week @ 2022-05-18 21/week @ 2022-05-25 34/week @ 2022-06-01 9/week @ 2022-06-08 24/week @ 2022-06-22

67 downloads per month

MIT/Apache

10KB
127 lines

einops crates docs

einops

This library is heavily inspired by python's einops.

Currently tch is the only available backend.

Difference from the python version,

  • All code generated at compile time, avoiding the need for caching
  • One common api for rearrange, reduce and repeat operations
  • Shape and reduction operations can be directly specified in the expression

Getting Started

Transpose

Permute/Transpose dimensions, left side of -> is the original state, right of -> describes the end state

// (28, 28, 3) becomes (3, 28, 28)
let output = einops!("h w c -> c h w", &input);

Composition

Combine dimensions by putting them inside a parenthesis on the right of ->

// (10, 28, 28, 3) becomes (280, 28, 3)
let output = einops!("b h w c -> (b h) w c", &input);

Transpose + Composition

Transpose a tensor, followed by a composing two dimensions into one, in one single expression

// (10, 28, 28, 3) becomes (28, 280, 3)
let output = einops!("b h w c -> h (b w) c", &input);

Decomposition

Split a dimension into two, by specifying the details inside parenthesis on the left, specify the shape of the new dimensions like so b1:2, b1 is a new dimension with shape 2

// (10, 28, 28, 3) becomes (2, 5, 28, 28, 3)
let output = einops!("(b1:2 b2) h w c -> b1 b2 h w c", &input);

New axis can also be specified from variables or fields (struct and enum) using curly braces

let b1 = 2;
let output = einops!("({b1} b2) h w c -> {b1} b2 h w c", &input);

Decomposition + Transpose + Composition

We can perform all operations discussed so far in a single expression

// (10, 28, 28, 3) becomes (56, 140 3)
let output = einops!("b h (w w2:2) c -> (h w2) (b w) c", &input);

Reduce

We can reduce axes using operations like, sum, min, max, mean and prod, if the same operations has to be performed on multiple continuous axes we can do sum(a b c)

// (10, 28, 28, 3) becomes (28, 28, 3)
let output = einops!("mean(b) h w c -> h w c", &input);

Decomposition + Reduce + Transpose + Composition

Single expression for combining all functionalities discussed

// (10, 28, 28, 3) becomes (14, 140, 3)
let output = einops!("b (h max(h2:2)) (w max(w2:2)) c -> h (b w) c", &input);

Repeat

We can repeat axes by specify it on the right side of ->, it can named, or it can simply be a number

// (28, 28, 3) becomes (28, 5, 28, 3)
let output = einops!("h w c -> h repeat:5 w c", &input);

Repeating axis's shape can be from a variables or a field (struct, enum)

let repeat = 5;
let output = einops!("h w c -> h {repeat} w c", &input);

Squeeze

Squeeze axes of shape 1

// (1, 28, 28, 3) becomes (28, 28, 3)
let output = einops!("1 h w c -> h w c")

Dependencies

~0.2–2.3MB
~49K SLoC