Skip to content
Related Articles

Related Articles

Tensorflow.js tf.einsum() Function

Improve Article
Save Article
  • Last Updated : 17 Jun, 2021
Improve Article
Save Article

Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .einsum () function is used to Tensor contraction over specified indices and outer product.

Syntax : 

tf.einsum (equation, tensors)

Parameters:

  • equation: It is a first input tensor element which is a string describing the contraction, in the same format as numpy.einsum.
  • . . . tensors: It is a second input tensor element in which the input(s)is used to contract (each one a Tensor), whose shapes should be consistent with equation.

 

Limitations:

  • It does not support 2 input tensors.
  • It doesn’t support duplicate axes for any given input tensor. For example, equation ‘ii→’ is not supported.
  • The … notation is not supported.

Return value: It returns tf.tensor.

Example 1: In this example, we are telling about special cases like Matrix multiplication.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining the first tensor input elements
const a = tf.tensor2d([[1, 1, 3], [4, 3, 6]]);
  
// Defining the second input tensor elements
const b = tf.tensor2d([[1, 1], [2, 3], [4, 5]]);
  
// Calling the einsum() function and printing outputs 
tf.einsum('ij,jk->ik', a, b).print();


Output:

Tensor
    [[14, 19],
     [30, 43]]

Example 2: In this example, we are telling about special cases like Dot product.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining the first input elements
const x = tf.tensor1d([1, 1, 3]);
  
// Defining the second input elements
const y = tf.tensor1d([1, 1, 2]);
  
// Calling the einsum() function
// and printing outputs 
tf.einsum('i,i->', x, y).print();


 

Output:

Tensor
    8

Example 3: In this example, we are telling about special cases like Batch dot product.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining the first tensor input elements
const x = tf.tensor2d([[1, 3, 3], [4, 5, 4]]);
  
// Defining the second tensor input elements 
const y = tf.tensor2d([[2, 1, 2], [2, 4, 5]]);
  
// Calling the einsum() function and printing output
tf.einsum('bi,bi->b', x, y).print();


Output:

Tensor
    [11, 48]

Example 4: In this example, we are telling about special cases like Outer product.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining the first tensor input elements
const x = tf.tensor1d([2, 3, 5]);
  
// Defining the second tensor input elements 
const y = tf.tensor1d([2, 5, 6]);
  
// Calling the einsum() function and printing outputs 
tf.einsum('i,j->ij', x, y).print();


Output:

Tensor
    [[4 , 10, 12],
     [6 , 15, 18],
     [10, 25, 30]]

Example 5: In this example, we are telling about special cases like Matrix transpose.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const x = tf.tensor2d([[1, 4], [3, 4]]);
  
// Calling the einsum() function and 
// printing output
tf.einsum('ij->ji', x).print();


Output:

Tensor
    [[1, 3],
     [4, 4]]

Example 6: In this example, we are telling about special cases like Batch matrix transpose.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const x = tf.tensor3d([[[1, 2], [3, 5]], [[-1, -2], [-3, -4]]]);
  
// Calling the einsum() function and printing output 
tf.einsum('bij->bji', x).print();


Output:

Tensor
    [[[1 , 3 ],
      [2 , 5 ]],

     [[-1, -3],
      [-2, -4]]]

Reference: https://js.tensorflow.org/api/latest/#einsum


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!