JavaScript

# Tensorflow.js – tf.sub()

“If you want to remove the hidden layers from a deep learning model or if you perform filtering on the images, then you may need to subtract the pixels from an image. So by using the tf.sub() function, it is possible to subtract two pixels. We can store the pixels of an image in a scalar or a tensor. In this tutorial, Let’s explore this function by considering different scenarios.”

## tf.sub()

tf.sub() in tensorflow.js is used to subtract the values element wise in two tensors/scalars.

## Scenario-1: Work With Scalar

Scalar will store only one value. But anyway, it returns a tensor.

Syntax

tf.sub(scalar1,scalar2)

Parameters

scalar1 and scalar2 are the tensors that can take only one value as a parameter.

Return

Return difference of two scalar values.

## Example

Create two scalars and perform subtraction on two scalars.

<html>

<!-- CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<script>

//scalar1

let value1 = tf.scalar(30);

//scalar2

let value2 = tf.scalar(70);

document.write("Scalar-1: ",value1);

document.write("<br>");

document.write("<br>");

document.write("Scalar-2: ",value2);

</script>

<h3>Tensorflow.js - tf.sub() </h3>

<script>

//tf.sub(value1,value2)

document.write(tf.sub(value1,value2));

</script>

</body>

</html>

Output:

Working:

The difference between 30 and 70 is -40.

## Scenario-2: Work With Tensor

A tensor can store multiple values; it can be single or multi-dimensional.

Syntax

tf.sub(tensor1,tensor2)

Parameters

tensor1 and tensor2 are the tensors that can take only single or multiple values as a parameter.

Return

Return difference of two tensors with respect to each element.

We must notice that the total number of elements in both the tensors must be equal.

## Example 1

Create two one-dimensional tensors and return the difference using tf.sub().

<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([10,20,30,40,50]);

//tensor2

let values2 = tf.tensor1d([1,2,3,4,5]);

document.write("Tensor-1: ",values1);

document.write("<br>");

document.write("<br>");

document.write("Tensor-2: ",values2);

</script>

<h3>Tensorflow.js - tf.sub() </h3>

<script>

//tf.sub(values1,values2)

document.write(tf.sub(values1,values2));

</script>

</body>

</html>

Output:

Working:

[10-1,20-2,30-3,40-4,50-5] => [9, 18, 27, 36, 45].

## Example 2

Create 2 two-dimensional tensors with 2 rows and 3 columns and apply tf.sub().

<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.tensor2d([1,2,3,4,5,6],[2,3]);

//tensor2

let values2 = tf.tensor2d([34,10,20,30,40,50],[2,3]);

document.write("Tensor-1: ",values1);

document.write("<br>");

document.write("<br>");

document.write("Tensor-2: ",values2);

</script>

<h3>Tensorflow.js - tf.sub() </h3>

<script>

//tf.sub(values1,values2)

document.write(tf.sub(values1,values2));

</script>

</body>

</html>

Output:

Working:

[[1-34,2-10,3-20],[4-30,5-40,6-50]] => [[-33, -8 , -17], [-26, -35, -44]].

## Scenario-3: Work With Tensor & Scalar

It can be possible to subtract each element from a tensor with a scalar.

Syntax

tf.sub(tensor,scalar)

## Example

Create a one-dimensional tensor and a scalar and perform subtraction using tf.sub().

<html>

<!-- CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<script>

//tensor

let values1 = tf.tensor1d([10,20,30,4,5,6]);

//scalar

let value2 = tf.scalar(1);

document.write("Tensor: ",values1);

document.write("<br>");

document.write("<br>");

document.write("Scalar: ",value2);

</script>

<h3>Tensorflow.js - tf.sub() </h3>

<script>

//tf.sub(values1,value2)

document.write(tf.sub(values1,value2));

</script>

</body>

</html>

Output:

Working:

[10-1, 20-1, 30-1, 4-1, 5-1, 6-1] => [9, 19, 29, 3, 4, 5].

## Conclusion

So we came to the end of the lesson. tf.sub() in tensorflow.js is used to subtract two tensors/scalars. We discussed three scenarios to subtract a tensor from a scalar.

Also, we noticed that scalar will store only one value and returns a tensor.

### About the author

#### Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain