Skip to content
Related Articles

Related Articles

Tensorflow.js tf.squaredDifference() Function

View Discussion
Improve Article
Save Article
  • Last Updated : 12 May, 2021
View Discussion
Improve Article
Save Article

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.squaredDifference() function is used to return (a – b) * (a – b) element-wise, where “a” is the first specified tensor and “b” is the second tensor. It Supports broadcasting.

Syntax:

tf.squaredDifference(a, b)

Parameters: This function accepts two parameters which are illustrated below:

  • a: The first specified tensor.
  • b: The second specified tensor. It must have same data type as “a”.

Return Value: It returns (a – b) * (a – b) element-wise, where “a” is the first specified tensor and “b” is the second tensor.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing two tensors
const a = tf.tensor1d([1, 3, 5, 7]);
const b = tf.tensor1d([1, 2, 9, 4]);
  
// Calling the .squaredDifference() function 
// over the above tensor as its parameters
a.squaredDifference(b).print();


Output:

Tensor
   [0, 1, 16, 9]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Broadcasting squared difference  a with b.
const a = tf.tensor1d([1, 3, 6, 7]);
const b = tf.scalar(4);
  
// Calling the .squaredDifference() function 
a.squaredDifference(b).print();


Output:

Tensor
   [9, 1, 4, 9]
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!