Skip to main content

Min Max Position

Summary

Finding the position of the minimum or maximum value in a given axis

  • tf.argmax()
  • tf.argmin()

Content

Position of Minimum Value

X = tf.random.uniform(shape=[50], minval=1, maxval=100)

# to get the position of the minimum value
X, tf.argmin(X), X[tf.argmin(X)]

"""
(<tf.Tensor: shape=(50,), dtype=float32, numpy=
array([17.734013 , 95.48472 , 58.482388 , 78.88151 , 64.40895 ,
38.730816 , 8.319603 , 94.95384 , 52.413155 , 12.9239025,
86.02268 , 7.4113903, 67.88889 , 35.025444 , 23.60047 ,
82.81168 , 57.993065 , 82.2186 , 85.74505 , 54.120884 ,
69.66284 , 27.195156 , 80.53625 , 31.74381 , 24.519695 ,
30.33681 , 54.3078 , 89.222694 , 23.813492 , 85.804405 ,
54.906315 , 66.093094 , 66.674576 , 83.43016 , 58.683666 ,
96.25571 , 55.63027 , 26.138855 , 65.98061 , 93.25791 ,
74.37076 , 89.52693 , 71.55578 , 22.188017 , 31.843405 ,
32.506294 , 93.34212 , 52.360386 , 68.41451 , 6.997858 ],
dtype=float32)>,
<tf.Tensor: shape=(), dtype=int64, numpy=49>,
<tf.Tensor: shape=(), dtype=float32, numpy=6.997858>)
"""

Position of Maximum Value

X = tf.random.uniform(shape=[50], minval=1, maxval=100)

X, tf.argmax(X), X[tf.argmax(X)]
"""
(<tf.Tensor: shape=(50,), dtype=float32, numpy=
array([13.832081 , 34.121017 , 88.644684 , 16.066666 , 98.641365 ,
48.601196 , 40.79431 , 58.915146 , 16.614832 , 27.061255 ,
66.605515 , 14.059157 , 68.04469 , 25.745338 , 46.18855 ,
42.901367 , 68.76748 , 89.02549 , 53.88989 , 2.517536 ,
53.744823 , 57.705093 , 30.424484 , 17.591778 , 79.91901 ,
27.821213 , 61.605904 , 51.709534 , 72.70406 , 70.62744 ,
9.9101925, 63.1496 , 23.855328 , 71.65855 , 84.96536 ,
28.362629 , 57.578 , 56.093754 , 63.568623 , 45.199787 ,
52.54676 , 35.93845 , 44.717003 , 52.629868 , 33.033478 ,
84.39091 , 37.273 , 63.246048 , 10.915096 , 49.832104 ],
dtype=float32)>,
<tf.Tensor: shape=(), dtype=int64, numpy=4>,
<tf.Tensor: shape=(), dtype=float32, numpy=98.641365>)
"""