Understand TensorFlow tf.pow() with Examples: Compute the Power of the Tensor – TensorFlow Tutorial

By | November 9, 2020

TensorFlow tf.pow() function can compute the power of one tensor to another, in this tutorial, we will use some examples to show you how to use it correctly.

Syntax

tf.pow(
    x,
    y,
    name=None
)

Computes the power of one value to another.

How to use tf.pow()?

We will write some examples to show you how to use.

(1) x and y are matrix, the shape of them are equal

import tensorflow as tf 
  
# Initializing the input tensor 
a = tf.constant([6, 8, 0, 15], dtype = tf.float64) 
b = tf.constant([2, 3, 4, 0],  dtype = tf.float64) 

c = tf.constant(2,  dtype = tf.float64) 

w1 = tf.pow(a, b)

w2 = tf.pow(b, c)

w3 = tf.pow(c, b)
  
# Printing the result 
init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()
with tf.Session() as sess:
    sess.run([init, init_local])
    print(sess.run(w1))
    print(sess.run(w2))
    print(sess.run(w3))

Here tensor a and b is a matrix or vector.

The result of \(a^b\)  is the \({a_i}^{b_i}\)

w1 = tf.pow(a, b)

The shape of w1 is same to a and b, the value of w1 is:

[ 36. 512.   0.   1.]

(2) x is a matrix, y is a scalar

For example:

w2 = tf.pow(b, c)

Here b is a matrix, c is a scalar. The shape of w2 is same to b. The value is:

[ 4.  9. 16.  0.]

It means \(w2_i=b_i^c\)

(3)  x is a scalar, y is a matrix

For example:

w3 = tf.pow(c, b)

Here c is a scalar, b is a matrix. The shape of w3 is same to b.

The value of w3 is:

[ 4.  8. 16.  1.]

It means \(w3_i=c^{b_i}\).

Leave a Reply

Your email address will not be published. Required fields are marked *