Open In App

Tensorflow.js tf.dot() 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.dot() function is used to compute the dot product of two given matrices or vectors, t1 and t2.

Syntax: 

tf.dot(t1, t2);

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

  • t1: It is the first tensor for the dot operation.
  • t2: It is the second tensor for the dot operation.

Return Value: It returns the dot product of two given matrices or vectors t1 and t2.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
let geek1 = tf.tensor1d([2, 3]);
let geek2 = tf.tensor2d([[2, 3], [4, 5]]);
let geek3 = tf.tensor2d([[2, 3, 4], [5, 6, 7]]);
  
let a =geek1.dot(geek2);
let b =geek2.dot(geek1);
let c =geek2.dot(geek3);
  
a.print();
b.print();
c.print();


Output:

Tensor
    [16, 21]
Tensor
    [13, 23]
Tensor
    [[19, 24, 29],
     [33, 42, 51]]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
let geek1 = tf.tensor1d([3, 4]);
let geek2 = tf.tensor2d([[3, 4], [5, 6]]);
  
tf.dot(geek1, geek2).print();


Output:

Tensor
    [29, 36]

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads