Saturday, October 18, 2025
HomeLanguagesJavascriptTensorflow.js tf.neg() Function

Tensorflow.js tf.neg() Function

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

The .neg() function is used to calculate -1 * x i.e. input tensor and is done element wise.

Syntax:  

tf.neg(x)

Parameters:  

  • x: It is the tensor input, and it can be of type tf.Tensor, or TypedArray, or Array.

Return Value: It returns the tf.Tensor object.

Example 1:  

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const y = tf.tensor2d([6, 0, -9, -1], [4, 1]);
  
// Calling neg() method and
// printing output
y.neg().print();


Output:

Tensor
    [[-6],
     [0 ],
     [9 ],
     [1 ]]

Example 2: In this example, the parameter is passed directly to the neg function.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const y = tf.tensor2d([
    1.4, 0.98, -9.9, -1.5, 2.0, 6.5], [2, 3]);
  
// Calling neg() method and
var res = tf.neg(y);
  
// printing output
res.print();


Output:

Tensor
    [[-1.4, -0.98, 9.8999996],
     [1.5 , -2   , -6.5     ]]

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

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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS