Thursday, September 18, 2025
HomeLanguagesJavascriptTensorflow.js tf.outerProduct() Function

Tensorflow.js tf.outerProduct() Function

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   ]]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6664 POSTS0 COMMENTS
Nicole Veronica
11837 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7054 POSTS0 COMMENTS
Thapelo Manthata
6738 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS