Skip to content
Related Articles

Related Articles

Improve Article

Tensorflow.js tf.neg() Function

  • Last Updated : 18 May, 2021
Geek Week

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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!

My Personal Notes arrow_drop_up
Recommended Articles
Page :