JavaScript

Tensorflow.js – tf.add()

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

tf.add()

tf.add() in tensorflow.js is used to add two tensors/scalars.

Scenario-1: Work With Scalar

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

Syntax

tf.add(scalar1,scalar2)

Parameters

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

Return

Return the sum of two scalar values.

Example

Create two scalars and add them using tf.add().

<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.add() </h3>

<script>

//tf.add(value1,value2)

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

</script>

</body>

</html>

Output:

Working:

The Sum of 30 and 70 is 100.

Scenario-2: Work With Tensor

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

Syntax

tf.add(tensor1,tensor2)

Parameters

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

Return

Return the sum of two tensors with respect to each element.

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

Example 1

Create two one-dimensional tensors and add them using tf.add().

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

//tensor2

let values2 = tf.tensor1d([10,20,30,40,50]);

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

document.write("<br>");

document.write("<br>");

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

</script>

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

<script>

//tf.add(values1,values2)

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

</script>

</body>

</html>

Output:

Working:

[1+10,2+20,3+30,4+40,5+50] => [11, 22, 33, 44, 55].

Example 2

Create 2 two-dimensional tensors with 2 rows and 3 columns and add them using tf.add().

<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.add() </h3>

<script>

//tf.add(values1,values2)

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

</script>

</body>

</html>

Output:

Working:

[[1+34,2+10,3+20],[4+30,5+40,6+50]] => [[35, 12, 23], [34, 45, 56]].

Scenario-3: Work With Tensor & Scalar

It can be possible to add each element in a tensor with a scalar.

Syntax:

tf.add(tensor,scalar)

Example

Create a one-dimensional tensor, a scalar, and add them using tf.add().

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

//scalar

let value2 = tf.scalar(10);

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

document.write("<br>");

document.write("<br>");

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

</script>

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

<script>

//tf.add(values1,value2)

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

</script>

</body>

</html>

Output:

Working:

[1+10, 2+10, 3+10, 4+10, 5+10, 6+10] => [11, 12, 13, 14, 15, 16].

Conclusion

So we came to the end of the lesson. tf.add() in tensorflow.js is used to add two tensors/scalars.

We discussed three scenarios to add tensors, scalars, and scalars with a tensor with examples. 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