How to Get Maximum Value from Tensors in TensorFlow?

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

tf.maximum() : Returns the maximum value by comparing of x and y (i.e. x > y ? x : y) element-wise.
It work with list, tuple, scaler, Numpy array
tf variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple

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

Args
xTensor. Must be one of the following types: bfloat16halffloat32float64int8uint8int16uint16int32uint32int64uint64.
yTensor. Must have the same type as x.
nameA name for the operation (optional).
Returns
Tensor. Has the same type as x.

Get maximum Value from 2 Variables using TensorFlow

import tensorflow as tf
import numpy as np

Get Max Value of Scaler

a = 10
b = 20
tf.maximum(a,b)

#tf.maximum(a,b).numpy() # 20

Output>>>

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

Get Max Value from 2 List

l1 = [1,2,3,4]
l2 = [5,6,7,8]

tf.maximum(l1, l2)
l1 = [1,2,3,4]
l2 = [5]

tf.maximum(l1, l2)

Get Max Value from 2 Tuple

t1 = (1,2,3,4)
t2 = (5,6,7,8)

tf.maximum(t1,t2)

Get Max Value of List/Tuple with Scaler

l1 = [1,2,3,4]
a =10
tf.maximum(l1, a)

Get Max Value of 2 Numpy Array

npa1 = np.array([1,4,3,9,5])
npa2 = np.array([2,2,3,4,5])

tf.maximum(npa1, npa2)

Get Max Value of Numpy array with Scaler

npa1 = np.array([1,2,3,4,5])
a = 3

tf.maximum(npa1, a)

Get Max Value of Numpy array with single element np array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([[2]])

tf.maximum(npa1, npa2)

Get Max Value of 2 TF Constant Tensor

tf_ct1 = tf.constant([2,2,3,8])
tf_ct2 = tf.constant([3,2,5,4])

tf.maximum(tf_ct1, tf_ct2)

Get Max Value of TF Constant Tensor with Scaler

tf_ct1 = tf.constant([1,2,3,4])
a = 10
tf.maximum(tf_ct1, a)
tf_ct1 = tf.constant([1,2,3,4])
l1 = [3,6,8,2]
tf.maximum(tf_ct1, l1)

Get Max Value of 2 TF Variable

tf_vr1 = tf.Variable([2,2,3,8])
tf_vr2 = tf.Variable([3,2,5,4])

tf.maximum(tf_vr1, tf_vr2)

Get Max Value of TF Variable with Scaler

tf_vr1 = tf.Variable([3,2,5,4])
a = 3
tf.maximum(tf_vr1, a)

Leave a Reply