Understanding keras.backend.max

I was trying to implement YOLO with 5 anchor boxes. I wanted to use keras.backend.max as described here but it’s not functioning properly or I am misunderstanding it.
The code looks something like this:
`

   from keras import backend as K

   with tf.compat.v1.Session() as test_a:
    example =  tf.random.normal([4, 4, 2, 2], mean=1, stddev=4, seed = 1)
    print(example.eval())
    # test = K.argmax(example, axis=-1)
    # print(test.eval())
    another_test = K.max(example, axis = -1)
    print(another_test.eval())
`

The output is this:
`

  [[[[-2.2452729   6.938395  ]
   [ 1.2613175  -8.770817  ]]

  [[ 1.3969936   3.3648973 ]
   [ 3.3712919  -7.4917183 ]]

  [[-1.8915889   0.7749185 ]
  [ 3.5741792  -0.05729628]]

  [[ 8.426533    3.2713668 ]
   [-0.5313436  -4.9413733 ]]]


 [[[ 6.0470843   0.8987757 ]
   [-0.05851877  7.131255  ]]

  [[-5.9719086  -0.7515718 ]
   [-1.26404     2.2826772 ]]

  [[ 5.531324   -8.113029  ]
    [ 2.9312482  -4.250835  ]]

  [[ 2.4274013  -5.9211335 ]
    [ 0.83932906  4.5986476 ]]]


 [[[-4.5223565   6.9258494 ]
   [ 0.01802081 -1.9305887 ]]

  [[ 0.21641421  1.2868321 ]
   [ 3.5319235  -5.284763  ]]

  [[ 6.317525   -3.693468  ]
   [ 1.1261784   2.9082098 ]]

  [[ 2.747768   -0.26723564]
   [-0.8030013  -6.2242627 ]]]


 [[[ 1.4995985  -2.0826168 ]
   [-1.9849663  -0.12781298]]

  [[-6.835262   -0.35044277]
   [ 5.1207933   7.053607  ]]

  [[ 1.9006321  -0.14264834]
   [ 2.0753016   7.984844  ]]

  [[ 4.695484   -7.2363987 ]
   [-0.25753224  5.841353  ]]]]
[[[-0.0205164   5.3679433 ]
  [ 3.9440122   4.684675  ]
  [ 4.1706614   1.3993862 ]
  [ 9.333472    3.0931065 ]]

 [[ 6.7779894   5.3317556 ]
  [-3.0122952  -1.5482914 ]
  [-0.5402719  -5.03914   ]
  [ 3.0354424   2.9042442 ]]

 [[ 4.730604    2.9970727 ]
  [ 3.8026135   0.04406112]
  [ 5.1083784   9.928441  ]
  [-1.8228705   1.4313807 ]]

 [[ 7.075215   -4.0160027 ]
  [ 2.046008    0.94906276]
  [ 0.6468721   1.6242697 ]
  [-0.2219665   5.4110713 ]]] `

As far as I can comprehend it should return the max value but what is it returning?

First thing to point out: while I’m not as well-versed in TF 1, I believe your example will end up evaluating the maximum of a completely different random tensor then the one you inspect because eval runs a the graph in a different session each time, e.g. tf.random.normal will get called again to evaluate the max. So (I think) you won’t be able to follow the results by inspecting them in this way.

Otherwise, this is the basically the result you should expect. keras.backend.max will return the maximum value along a specified axis/axes, effectively reducing the dimension away. Your examples starts by printing a (4, 4, 2, 2) tensor. After the call to keras.backend.max, with axis=-1 (which wraps to the last axis in the tensor), the result is a tensor with shape (4, 4, 2). That’s because you’re telling the function to “walk” the last axis and find the maximum value of the If you want the absolute maximum value of the entire tensor, axis should be set to None.

Here are some answers that demonstrate keras.backend.max with different axis arguments, and one with keepdims set to True:

with tf.compat.v1.Session() as test_a:
  example = tf.random.normal([4, 4, 2, 2], mean=1, stddev=4, seed = 1)
  # Print absolute maximum value (shape: ())
  abs_max = K.max(example, axis=None)
  print(abs_max.shape)

  # Print maximum values along axis 0, 2 (shape: (4, 2))
  max_axis_0_2 = K.max(example, axis=(0, 2,))
  print(max_axis_0_2.shape)

  # Print maximum values along axis 1 (shape: (4, 2, 2))
  max_axis_1 = K.max(example, axis=(1,))
  print(max_axis_1.shape)

  # Print maximum values along axis 0, 2, and keep reduced axes
  # reduced axes will be size 1
  max_axis_0_2_keep = K.max(example, axis=(0, 2,), keepdims=True)
  print(max_axis_0_2_keep.shape)
1 Like

Thanks for replying!
Is there any way in which I can inspect both the original & the tensor which returns after K.max ?

This works, but I don’t know if it’s the preferred way in TF 1:

import tensorflow as tf
from tensorflow.keras import backend as K
tf.compat.v1.disable_eager_execution()

with tf.compat.v1.Session() as sess:
  example = tf.random.normal([4, 4, 2, 2], mean=1, stddev=4, seed = 1)
  print_example = tf.compat.v1.print(example)
  # Print absolute maximum value (shape: ())
  abs_max = K.max(example, axis=None)
  print_abs_max = tf.compat.v1.print(abs_max)
  sess.run([print_example, print_abs_max])