How To Calculate Power Of Tensors In TensorFlow?

In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.pow() function. We will learn how to calculate the power of tensors in TensorFlow using tf.pow() function.

tf.pow(): Calculate the power of one value to another, Element wise power calculation
It can be given the power of scaler, Numpy array
It allow tf variable/constant/placeholder/SparceMatrix with Specific datatype

Data Type : float16, float32, float64, int32, int64, complex64, or complex128.

Note: Don’t pass data in integer format, it will through error

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

xTensor of type float16float32float64int32int64complex64, or complex128.
yTensor of type float16float32float64int32int64complex64, or complex128.
nameA name for the operation (optional).

Calculate the Power using TensorFlow

import tensorflow as tf
import numpy as np

Power of Scaler

a = 4
b = 2

tf.pow(a, b)


<tf.Tensor: shape=(), dtype=int32, numpy=16>

Power of Numpy Array

npa1 = np.array([4,9,16], dtype = np.float64)

tf.pow(npa1, 2)
npa1 = np.array([1,2,3], dtype = np.float64)
npa2 = np.array([1,2,3], dtype = np.float64)

tf.pow(npa1, npa2)
y = np.ones(6).reshape(2, 1, 3, 1)*4
print("y : ", y)
tf.pow(y, 2)

Power of Tensor

tf_ct1 = tf.constant([4,9,16], dtype='float')

tf.pow(tf_ct1, 3)
tf_ct1 = tf.constant([4,9,16], dtype='float')
tf_ct2 = tf.constant([3,2,1], dtype='float')

tf.pow(tf_ct1, tf_ct2)

Power of TF Variable

tf_vr1 = tf.Variable([4,9,16], dtype='half')

tf.pow(tf_vr1, 2)
tf_vr1 = tf.Variable([4,9,16], dtype='float')
tf_vr2 = tf.Variable([3,2,1], dtype='float')

tf.pow(tf_vr1, tf_vr2)
tf_vr1 = tf.Variable([4,9,16], dtype='float')
tf_vr2 = tf.Variable([3], dtype='float')

tf.pow(tf_vr1, tf_vr2)

Leave a Reply