Saturday, September 6, 2025
HomeLanguagesJavascriptTensorflow.js tf.onesLike() Function

Tensorflow.js tf.onesLike() Function

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.

The tf.onesLike() function is used to create a tf.Tensor with all elements set to 1 with the same shape as the given tensor.

Syntax:

tf.onesLike (x)

Parameters:

  • x: a tf.Tensor whose all elements will be set as 1.

Return Value: It returns a tf.Tensor.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
const x = tf.tensor([1, 2]);
  
// Printing the tensor after setting the value of tensor to 1
tf.onesLike(x).print();


Output:

Tensor
   [1, 1]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
const x = tf.tensor2d([8, 2, 5, 6], [2, 2]);
  
// Printing the tensor after setting the value of tensor to 1
tf.onesLike(x).print();


Output:

Tensor
   [[1, 1],
    [1, 1]]

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

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
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS