0

I am new to Keras and TensorFlow.

This question builds on the following question asked here: What is the meaning of axis=-1 in keras.argmax?

My question here is:

When comparing the actual values in matrix1 and matrix1_lastDropped - after running the following statement:

matrix1_lastDropped = K.argmax(matrix1, axis=-1)

Why does matrix1_lastDropped NOT contain the values in the original matrix matrix1? The matrix1_lastDropped dimensions are [4, 4, 3] (because the last dimension of matrix1 was dropped), but the values should still be the same and adjusted into the new dimensions? Would these not?

Code is:

import tensorflow as tf
from keras import backend as K
with tf.Session() as test_a:
    matrix1 = tf.random_normal([4, 4, 3, 3], mean=1, stddev=4, seed = 1)
    print("matrix1.shape = " + str(matrix1.shape))
    print ("matrix1 values are::" + str(matrix1.eval()))
    
#     print("tf.keras.argmax = " + str(K.argmax(matrix1, axis=-1)))
#     print("tf.keras.max = " + str(K.max(K.argmax(matrix1, axis=-1))))
    
    matrix1_lastDropped = K.argmax(matrix1, axis=-1)
    print ("matrix1_lastDropped shape is:: " + str(matrix1_lastDropped.shape))
   
    print ("matrix1_lastDropped values are::" + str(matrix1_lastDropped.eval()))

Output is:

matrix1.shape = (4, 4, 3, 3)
matrix1 values are::[[[[ -2.24527287   6.93839502   1.26131749]
   [ -8.77081585   1.39699364   3.36489725]
   [  3.37129188  -7.49171829  -1.89158893]]

  [[  0.7749185    3.57417917  -0.05729628]
   [  8.42653275   3.27136683  -0.5313437 ]
   [ -4.94137383   6.04708433   0.8987757 ]]

  [[ -0.05851877   7.13125515  -5.97190857]
   [ -0.75157177  -1.26403999   2.28267717]
   [  5.53132391  -8.11302853   2.93124819]]

  [[ -4.25083494   2.4274013   -5.92113352]
   [  0.83932906   4.59864807  -4.52235651]
   [  6.92584944   0.01802081  -1.93058872]]]


 [[[  0.21641421   1.28683209   3.53192353]
   [ -5.28476286   6.31752491  -3.69346809]
   [  1.12617838   2.9082098    2.74776793]]

  [[ -0.26723564  -0.80300128  -6.22426271]
   [  1.4995985   -2.08261681  -1.98496628]
   [ -0.12781298  -6.83526182  -0.35044277]]

  [[  5.12079334   7.05360699   1.90063214]
   [ -0.14264834   2.07530165   7.98484421]
   [  4.69548416  -7.2363987   -0.25753224]]

  [[  5.84135294   3.779212    -3.26219988]
   [  1.05456042  -3.27085018   0.26369983]
   [ -7.82249355   8.3162384    5.97276115]]]


 [[[ -0.34622049   0.83996451  -0.34342086]
   [ -0.22979593  -2.06771874  -0.14843333]
   [ -0.17881143  -2.23962522  -4.2636075 ]]

  [[  2.50129652   1.71023345  -7.23314571]
   [  2.6297071   -3.02893209   2.17062998]
   [  3.06534362   6.92378616   1.41760826]]

  [[ -8.66411591  -1.42192721   1.18490028]
   [ -1.67261004  -0.61323476   3.82889676]
   [ -6.16030502   2.4496088  -10.06762886]]

  [[  8.92316818   1.62972403   3.10544157]
   [ -7.84809399   2.90044761  -0.81601429]
   [  0.7232089    3.74772048   0.49090755]]]


 [[[  5.77174711   5.56470251  -5.79740763]
   [ -4.30168772   3.1566093    0.95456219]
   [ -2.50534177   4.83609009  -4.48813152]]

  [[  2.12740636  -1.81127858   2.1396203 ]
   [  2.34717274   0.98015857   4.33931494]
   [  2.54941368  -0.04910374  -0.16517568]]

  [[  2.74878979   6.5430727    1.77702928]
   [  0.75588918   4.74197483   8.82833767]
   [  2.06116533  -2.46971226   0.89486873]]

  [[  0.53019679  -1.78584528   0.10209811]
   [ -2.84412932   1.40259576   1.23638427]
   [  0.97154915  -3.31380701   4.75023317]]]]
matrix1_lastDropped shape is:: (4, 4, 3)
matrix1_lastDropped values are::[[[1 0 0]
  [1 2 1]
  [1 2 2]
  [0 0 1]]

 [[2 1 0]
  [2 1 0]
  [1 2 1]
  [1 0 2]]

 [[1 1 0]
  [0 2 1]
  [2 2 0]
  [0 2 0]]

 [[0 2 0]
  [1 2 0]
  [1 2 0]
  [0 0 1]]]
Utpal Mattoo
  • 890
  • 3
  • 17
  • 41
  • I think I have compared index locations to values in those index locations, which is in-correct. But it still does not quite make sense as to which locations should be output as `1`, `0` or `2` – Utpal Mattoo Jun 23 '20 at 16:45
  • I don't quite understand the question. `argmax` gives you the index of the maximum value in the tensor along the given dimension. `matrix1_lastDropped` is not supposed to contain any values as `matrix1`, it is a tensor of indices. – jdehesa Jun 23 '20 at 17:01
  • You may be confused by the same reason as [this other question](https://stackoverflow.com/q/62083079/1782792) though. The values in `matrix1` and `matrix1_lastDropped` do not "correspond" (that is, the value in `matrix1_lastDropped` does not necessarily point to the biggest value across the axis in `matrix1`) because each time you call `run` or `eval` the tensor `matrix1` will get new random values. You would get coherent results if you did `matrix1_val, matrix1_lastDropped_val = test_a.run([matrix1, matrix1_lastDropped])` and checked the values of `matrix1_val` and `matrix1_lastDropped_val`. – jdehesa Jun 23 '20 at 17:06
  • When I `print (matrix1_lastDropped_val[0])` and get `[[1 2 0] [1 0 1] [1 2 0] [1 1 0]]`, I can map each index in `matrix1_lastDropped_val[0][0]` to it corresponding in matrix1. But how do I read this result? – Utpal Mattoo Jun 23 '20 at 19:31
  • I `print (matrix1_lastDropped_val[0])` and get `[[1 2 0] [1 0 1] [1 2 0] [1 1 0]]`, I can map, for example, `matrix1_lastDropped_val[0][0]`, which is `[1 2 0]` to it's corresponding in `matrix1_val`, which is: `matrix1_val[1][2][0]` But how do I read this result? These indexes point all over. For example, shouldn't `matrix1_lastDropped_val[0][0]` map to it's corresponding`matrix1_val[0][0]`? – Utpal Mattoo Jun 23 '20 at 20:06
  • The way to interpret these values is: `matrix1_lastDropped_val[i, j, k]` holds the index of the largest value in `matrix1_val[i, j, k]`. So, if `matrix1_lastDropped_val[0, 0]` is `[1 2 0]` ir means that the largest value in `matrix1_val[0, 0, 0]` is the second one (index `1`), the largest value in `matrix1_val[0, 0, 1]` is the last one (index `2`) and the largest value in `matrix1_val[0, 0, 2]` is the first one (index `0`). – jdehesa Jun 24 '20 at 11:22

0 Answers0