Saturday, December 13, 2025
HomeLanguagesJavascriptTensorflow.js tf.squaredDifference() Function

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:

  • 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]
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!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32446 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12030 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7200 POSTS0 COMMENTS
Thapelo Manthata
6897 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS