Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Tensorflow.js tf.layers.dot() Function

  • Last Updated : 01 Sep, 2021

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: 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

tf.layers.dot(args);

Parameters: This function accepts the following parameters: 

  •  args: It is the object with the following field:
    • axes: It defined the axes to dot product to which way it is going.
    • normalize: It is boolean data used to produce the answer of the dot product in cosine value form.
    • inputShape: It defines the shape of the input layer of the model. It is used in the creation of the input layer.
    • dtype: it is the data-type of the layer. It is used for the first layer of the model.
    • name: String is the name for the input layer.
    • weight: It is the tensor whose values are the initial value for the layer.
    • trainable: It defined that is layer is trainable or not. It is a boolean data type.
    • batchInputShape: It defined the batch’s shape for the samples in the input layer. It is used in making of the input layer.
    • batchSize: It is used as supplementary of batchInputShape in the construction of the input layer. It is used in making of the input layer.
    • inputDType: It is the data type for the input data in the layer.

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.

Javascript




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

Javascript




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




My Personal Notes arrow_drop_up
Recommended Articles
Page :