How To Get Minimum Value From Tensors In TensorFlow?

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

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

Syntax: tf.minimum(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 Minimum Value from 2 Variables using TensorFlow

import tensorflow as tf
import numpy as np

Get Min Value of Scaler

a = 10
b = 20

tf.minimum(a,b)

### to get max value only
#tf.minimum(a,b).numpy() # 10

Output >>>

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

Get Min Value of 2 List

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

tf.minimum(l1, l2)

Get Min Value of 2 Tuple

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

tf.minimum(t1,t2)

Get Min Value of List/Tuple with Scaler

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

Get Min Value of 2 Numpy Array

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

tf.minimum(npa1, npa2)

Get Min Value of Numpy array with Scaler

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

tf.minimum(npa1, a)

Get Min Value of Numpy array with single element np array

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

tf.minimum(npa1, npa2)

Get Min Value of 2 TF Constant Tensor

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

tf.minimum(tf_ct1, tf_ct2)

Get Min Value of TF Constant Tensor with Scaler

tf_ct1 = tf.constant([1,2,3,4])
a = 10
tf.minimum(tf_ct1, a)

Get Min Value of 2 TF Variable

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

tf.minimum(tf_vr1, tf_vr2)

Get Min Value of TF Variable with Scaler

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

Leave a Reply

Top