Open In App

Tensorflow.js tf.meshgrid() Function

Last Updated : 06 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .meshgrid() function is used to broadcast arguments in order to analyze on an N-D mesh. Moreover, for a 1D coordinate arrays i.e. *args, this method returns a list of outputs of an N-D coordinate arrays for analyzing expressions on an N-D mesh for a given N.

Note: This function favors cartesian ‘xy’ as well as matrix ‘ij’ indexing protocols. If the indexing parameter is fixed to the  by default value i.e. ‘xy’, then the broadcasting commands for the first two measurements are exchanged. 

Syntax:

tf.meshgrid(x?, y?, __2?)

 

Parameters:  

  • x: The stated tensor along with rank geq one. It is optional and can be of type tf.Tensor, TypedArray, or Array.
  • y: The stated tensor along with rank geq one. It is optional and can be of type tf.Tensor, TypedArray, or Array.
  • __2: It is optional parameter and is of type { indexing?: string; }.

Return Value: It returns tf.Tensor[].

Example 1: Using tensors of rank 1.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining first tensor
const t1 = [1, 1, 3];
  
// Defining second tensor
const t2 = [2, 5, 4];
  
// Calling meshgrid() function
const res = tf.meshgrid(t1, t2);
  
// Printing output
console.log(res);


Output:

Tensor
    [[1, 1, 3],
     [1, 1, 3],
     [1, 1, 3]],Tensor
    [[2, 2, 2],
     [5, 5, 5],
     [4, 4, 4]]

Example 2: Using float values.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling meshgrid() function with
// float values
const output = tf.meshgrid(2.1, 3.3);
  
// Printing output
console.log(output);


Output:

Tensor
     [[2.0999999],],Tensor
     [[3.3],]

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads