Open In App

Tensorflow.js tf.squaredDifference() 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.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:



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:




// 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:




// 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]
Article Tags :