Understanding Einstein’s Notation and einsum Multiplication | by Marcello Politi | Sep, 2024

Picture by Taton Moïse on Unsplash

Carry out higher-order tensor operations with string notation

Introduction

Machine Studying depends closely on linear algebra operations, particularly, it’s vital to know nicely how multiplications between matrices (or extra exactly between tensors) work.

I’ve often observed that these approaching the research of Machine Studying frameworks resembling PyTorch or Tensorflow have many issues when bumping into errors regarding measurement mismatch between tensors. On this brief article, I want to shed some readability on this and introduce the usage of einsum.

From scalars to tensors

In Machine Studying, we differentiate the info in keeping with its dimension (dim). It is not uncommon to work with:

  • Scalars dim = 0
  • Vectors dim = 1
  • Matrices dim = 2
  • Tensors dim = 3 ( or extra)
Picture by Writer

In PyTorch we will very simply create these information varieties.

import torch

# Create a scalar tensor
scalar…