Open In App

Tensorflow.js tf.neg() Function

Last Updated : 18 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads