Open In App

Tensorflow.js tf.linspace() Function

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 .linspace() function is used to find a regularly distanced series of numbers for a stated interval.



Syntax :  

tf.linspace(start, stop, num)

Parameters:  



Return Value: It returns the tf.Tensor1D object.

Example 1:  




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining all the parameters
var strt = 3;
var stp = 20;
var num = 5;
  
// Calling linspace() method and
// Printing output
tf.linspace(strt, stp, num).print();

Output:

Tensor
    [3, 7.25, 11.5, 15.75, 20]

Example 2: Using float values for all the stated parameters.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling linspace() method and
// Printing output
tf.linspace(12.4, 29.7, 5.4).print();

Output:

Tensor
    [12.3999996, 16.3318176, 20.2636356, 24.1954536, 28.1272717]

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

Article Tags :