JavaScript

# The tf.squaredDifference() Function in TensorFlow.js

In the Tensorflow.js library, the tf.squaredDifference() function is used to return the square difference of each element present in two tensors/scalars.

## Computation

(i-j)*(i-j)

Where “i” is the element in the first tensor, and “j” is the element in the second tensor.

Syntax:

tensor.squaredDifference()

Return:

It returns a positive integer even if the result is negative.

## Example 1

Create two 1D tensors with 4 integers and get the squared difference.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<script>

//tensor1
let values1 = tf.tensor1d([0,1,2,3]);
//tensor2
let values2 = tf.tensor1d([1,4,1,5]);

//display
document.write("<b>Tensor-1: </b> "+values1);
document.write("<br>");
document.write("<b>Tensor-2: </b> "+values2);
document.write("<br>");

//return the squared difference
document.write("<b>Squared Difference: </b> "+values1.squaredDifference(values2));

</body>
</html>

Output:

Working:
(0-1)*(0-1) = 1
(1-4)*(1-4)=9
(2-1)*(2-1)=1
(3-5)*(3-5)=4

## Example 2

Create two 1D tensors with 5 integers and get the squared difference.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<script>

//tensor1
let values1 = tf.tensor1d([12,34,56,78,90]);
//tensor2
let values2 = tf.tensor1d([12,34,56,78,90]);

//display
document.write("<b>Tensor-1: </b> "+values1);
document.write("<br>");
document.write("<b>Tensor-2: </b> "+values2);
document.write("<br>");

//return the squared difference
document.write("<b>Squared Difference: </b> "+values1.squaredDifference(values2));
</script>

</body>
</html>

Output:

If the values in a tensor are the same, then the squared difference is 0.

It is also possible to get the squared difference with a tensor and a scalar.

## Example 3

Create a1D tensor with 5 integers, a scalar with a value of 2 and get the squared difference.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<script>

//tensor
let values = tf.tensor1d([12,34,56,78,90]);
//scalar
let value = tf.scalar(2);

//display
document.write("<b>Tensor: </b> "+values);
document.write("<br>");
document.write("<b>Scalar: </b> "+value);
document.write("<br>");

//return the squared difference
document.write("<b>Squared Difference: </b> "+values.squaredDifference(value));
</script>

</body>
</html>

Output:

Working:

1. (12-2)*(12-2)=100
2. (34-2)*(34-2)=1024
3. (56-2)*(56-2)=2916
4. (78-2)*(78-2)=5776
5. (90-2)*(90-2)=7744

## Conclusion

In this article, we saw how to return the square difference of elements in tensors using tf.squaredDifference(). It performs computation using the formula (i-j)*(i-j), where “i” is the element in the first tensor and “j” is the element in the second tensor. Also, we discussed the different examples with different cases