Open In App

Tensorflow.js tf.matMul() Function

Last Updated : 18 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • a: This is the first matrix in dot product operation.
  • b: This is the second matrix in dot product operation.
  • transposeA: This is optional and if it is set to true, then a is transposed before multiplication.
  • transposeB: This is optional and if it is set to true, then b is transposed before multiplication.

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

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

Example 1:

Javascript




// 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:

Javascript




// 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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads