JavaScript

# Tensorflow.js – tf.pow()

“tf.pow() in tensorflow.js is used to raise power with respect to the values in another tensor.”

## Scenario 1: Work With Scalar

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

Syntax

tf.pow(scalar1,scalar2)

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

Return
Return remainder of two scalar values.

Example
Create two scalars and raise the power to value present in the second scalar.

<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(3);

//scalar2
let  value2 = tf.scalar(4);

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

document.write("<br>");
document.write("<br>");

document.write("Scalar-2: ",value2);
</script>
<h3>Tensorflow.js - tf.pow() </h3>

<script>
//tf.pow(value1,value2)
document.write(tf.pow(value1,value2));
</script>

</body>
</html>

Output

Working
3 to the power of 4 => 3*3*3*3 = 81.

## Scenario 2: Work With Tensor

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

Syntax

tf.pow(tensor1,tensor2)

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

Return
Power of values.

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 power of elements in a first tensor concerning values in a second tensor.

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

//tensor2
let  values2 = tf.tensor1d([1,2,3]);

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

document.write("<br>");
document.write("<br>");

document.write("Tensor-2: ",values2);
</script>
<h3>Tensorflow.js - tf.pow() </h3>

<script>
//tf.pow(values1,values2)
document.write(tf.pow(values1,values2));
</script>

</body>
</html>

Output

Working
[2 power 1,3 power 2,4 power 3,] => Tensor [2,9,64].

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

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

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

document.write("<br>");
document.write("<br>");

document.write("Tensor-2: ",values2);
</script>
<h3>Tensorflow.js - tf.pow() </h3>

<script>
//tf.pow(values1,values2)
document.write(tf.pow(values1,values2));
</script>

</body>
</html>

Output

## Scenario 3: Work With Tensor & Scalar

It can be possible to raise the power of each element in a tensor by a scalar.

Syntax

tf.pow(tensor,scalar)

Example
Create a one-dimensional tensor,  a scalar, and raise each element in a tensor to a scalar value.

<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(2);

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

document.write("<br>");
document.write("<br>");

document.write("Scalar: ",value2);
</script>
<h3>Tensorflow.js - tf.pow() </h3>

<script>
//tf.pow(values1,value2)
document.write(tf.pow(values1,value2));
</script>

</body>
</html>

Output

## Conclusion

tf.pow() in tensorflow.js is used to raise power with respect to the values in another tensor. Also, we noticed that scalar will store only one value and returns a tensor. While performing tf.pow() on two tensors, ensure that the number of elements in two tensors must be the same.