Thursday, September 4, 2025
HomeLanguagesJavascriptTensorflow.js tf.matMul() Function

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:

  • 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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6628 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS