Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.
The .outerProduct() function is used to find the outer product of two stated vectors i.e. v1 and v2.
Syntax:
tf.outerProduct(v1, v2)
Parameters: This function accepts three parameters which are illustrated below:
- v1: It is the first vector in the outer product function, and it can be of type tf.Tensor1D, or TypedArray, or Array.
- v2: It is the second vector in the outer product function and it can be of type tf.Tensor1D, or TypedArray, or Array.
Return Value: It returns the tf.Tensor2D object.
Example 1:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining tensor input elements const y = tf.tensor1d([1, 15, 38, 7]); const z = tf.tensor1d([5, 12, 21, 9]); // Calling outerProduct() method and // printing output tf.outerProduct(y, z).print(); |
Output:
Tensor [[5 , 12 , 21 , 9 ], [75 , 180, 315, 135], [190, 456, 798, 342], [35 , 84 , 147, 63 ]]
Example 2:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining float values var val1 = [0.5, 9.5, .56]; var val2 = [0.51, 1.5, .63]; // Calling outerProduct() method var res = tf.outerProduct(tf.tensor1d(val1), tf.tensor1d(val2)) // Printing output res.print(); |
Output:
Tensor [[0.255 , 0.75 , 0.315 ], [4.8449998, 14.25, 5.9850001], [0.2856 , 0.84 , 0.3528 ]]