Adelovi
Adelovi

Reputation: 95

How to get the maximum 2D Tensor from a 3D tensor using TensorFlow 1.14?

I am looking for the best and optimized way (without loops) to get a 2D max Tensor from a 3D Tensor based on the maximum one value using TensorFlow 1.14. let's say we have this Tensor and this function(for understanding-it's not working-):

def get_Max(inputs):
    max_indices = [0,0,0]
    for i in range(16):
        for j in range(2048):
            for k in range(10):
                if(inputs[max_indices[0],max_indices[1],max_indices[2]]<inputs[i,j,k]):
                   max_indices = [i,j,k]
    return inputs[:][j]
inputs = tf.random.uniform(shape=[16,2048,10],dtype=tf.dtypes.float32)
output = get_Max(inputs)

So, output tensor must have a shape of [16,10], which are the 16 maximums from 2048. So, How can implement a function that can do this without loops ?

I used tf.math.reduce_max but it's not what i am looking for as it's clear in the image below:

enter image description here

Upvotes: 1

Views: 758

Answers (1)

Mohammad Jafar Mashhadi
Mohammad Jafar Mashhadi

Reputation: 4251

inp = tf.random.uniform(shape=[4, 6, 2], maxval=20, dtype=tf.int32)
print(inp)

array([[[14,  8],
    [18, 10],
    [ 6, 14],
    [ 8,  9],
    [11, 11],
    [14, 13]],

   [[ 7, 18],
    [ 4, 10],
    [15,  6],
    [ 6,  2],
    [19, 11],
    [10,  4]],

   [[ 8,  1],
    [ 1,  3],
    [ 4, 17],
    [15,  7],
    [ 0,  0],
    [ 1,  4]],

   [[ 5,  0],
    [15, 12],
    [ 1, 16],
    [ 3, 17],
    [14, 17],
    [ 2, 18]]], dtype=int32)>

So if I understood correctly, for each inp[i, :, :] like:

    [[14,  8],
    [18, 10],
    [ 6, 14],
    [ 8,  9],
    [11, 11],
    [14, 13]]

you want to keep the item that contains the max number, in this case the second row: [18, 10]. What I'd do is to first find the max number along the last axis:

am = tf.math.reduce_max(inp, axis=2)
am[0, :, :]
[14,
 18,
 14,
 9,
 11,
 14]

and then find the index of the row that contains the max number:

am = tf.math.argmax(am, axis=1)

These will be the js you want, then you can use tf.gather_nd and enumerate to get those values:

# [*enumerate(am)] = [(0, am[0]), (1, am[1]), ...]
tf.gather_nd(inp, [*enumerate(am)])

<tf.Tensor: shape=(4, 2), dtype=int32, numpy=
array([[18, 10],
       [19, 11],
       [ 4, 17],
       [ 2, 18]], dtype=int32)>

Upvotes: 1

Related Questions