Open In App

Tensorflow.js tf.layers.dot() 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. Tensorflow.js tf.layers.dot() function is used to apply the dot product between the two tensors provided.

Syntax: 



tf.layers.dot(args);

Parameters: This function accepts the following parameters: 

Returns: It returns the product tensor of two tensors.



Below are some examples of this function.

Example 1: In this example,  we will make simply a dot product of tensor with  -2 axis rotation of values.




// import * as tf from "@tensorflow/tfjs"
 
// Generating tensor of [3,3] shape and size.
const geek_tens1 = tf.randomUniform([3 ,3]);
const geek_tens2 = tf.randomUniform([3 ,3]);
 
// Making tensor compatible to apply to dot product.
const geek_Input = [geek_tens1 ,geek_tens2];
 
// Calling dot product
const geek_config = {axes: -2};
const geek_DotPro = tf.layers.dot(geek_config);
const geek_DotProRes = geek_DotPro.apply(geek_Input);
 
// Printing our result
console.log(geek_DotProRes);
geek_DotProRes.print();

 
 Output: 

Tensor
    [[0.7034817],
     [0.2338114],
     [1.1493738]]
Tensor
    [[0.7034817],
     [0.2338114],
     [1.1493738]]

 Example 2: In this example,  we will make a dot product of two tensors and see the result in cosine form. 




import * as tf from "@tensorflow/tfjs"
 
// Collect both outputs and print separately.
// Generating tensor of [3,3] shape and size.
const geek_tens1 = tf.randomUniform([2 ,4]);
const geek_tens2 = tf.randomUniform([2 ,4]);
 
// Making tensor compatible to apply to dot product.
const geek_Input = [geek_tens1 ,geek_tens2];
 
// Calling dot product
const geek_config = {axis: 1, normalize: true,
        dtype: 'int32', name: 'DotProduct' };
const geek_DotPro = tf.layers.dot(geek_config);
const geek_DotProRes = geek_DotPro.apply(geek_Input);
 
// Printing our result
console.log(geek_DotProRes);

 
Output: 

Tensor
    0.6817111968994141

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


Article Tags :