Open In App

Tensorflow.js tf.matMul() Function

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

The tf.matMul() function is used to compute the dot product of two matrices, A * B.



Syntax:

tf.matMul (a, b, transposeA?, transposeB?)

Parameters: This function accepts a parameter which is illustrated below:



Return Value: It returns the dot product of two matrices.

Below are the examples that illustrates the use of tf.matMul() function.

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a tensor of some elements
let geek1 = tf.tensor2d([2, 1], [1, 2]);
let geek2 = tf.tensor2d([11, 12, 13, 14], [2, 2]);
  
// Calling the .avgPool3d() function over
// the above tensor as its parameter and 
// printing the result.
geek1.matMul(geek2).print();

Output:

Tensor
     [[35, 38],]

Example 2:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a tensor of some elements
let geek1 = tf.tensor2d([2, 1], [1, 2]);
let geek2 = tf.tensor2d([61, 62, 63, 64], [2, 2]);
  
// Calling the .avgPool3d() function over
// the above tensor as its parameter and 
// printing the result.
tf.matMul(geek1, geek2).print();

Output:

Tensor
     [[185, 188],]

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

Article Tags :