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
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor2d([[1, 1, 3], [4, 3, 6]]);
const b = tf.tensor2d([[1, 1], [2, 3], [4, 5]]);
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
import * as tf from "@tensorflow/tfjs"
const x = tf.tensor1d([1, 1, 3]);
const y = tf.tensor1d([1, 1, 2]);
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
import * as tf from "@tensorflow/tfjs"
const x = tf.tensor2d([[1, 3, 3], [4, 5, 4]]);
const y = tf.tensor2d([[2, 1, 2], [2, 4, 5]]);
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
import * as tf from "@tensorflow/tfjs"
const x = tf.tensor1d([2, 3, 5]);
const y = tf.tensor1d([2, 5, 6]);
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
import * as tf from "@tensorflow/tfjs"
const x = tf.tensor2d([[1, 4], [3, 4]]);
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
import * as tf from "@tensorflow/tfjs"
const x = tf.tensor3d([[[1, 2], [3, 5]], [[-1, -2], [-3, -4]]]);
tf.einsum( 'bij->bji' , x).print();
|
Output:
Tensor
[[[1 , 3 ],
[2 , 5 ]],
[[-1, -3],
[-2, -4]]]
Reference: https://js.tensorflow.org/api/latest/#einsum
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Jun, 2021
Like Article
Save Article