Search code examples
tensorflowtensorflow-datasets

How do you get the max value in a tensorflow dataset whilst keeping the same shape and dimension?


Consider the following code below:

import tensorflow as tf
x = tf.constant([5, 1, 2, 4])
tf.reduce_max(x)

The result of the above would give me the output x:

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

However, I want to get the max value in such a format:

<tf.Tensor: shape=(4,), dtype=int32, numpy=array([5,5,5,5]dtype=int32)>

Any ideas how to code this?


Solution

  • Thanks to Flavia Giammarino for helping.

    import tensorflow as tf
    x = tf.constant([5, 1, 2, 4])
    tf.reduce_max(x)
    
    
    test=tf.repeat(tf.reduce_max(x), repeats=x.shape[0])
    print(test)
    

    I got the following output as desired:

    tf.Tensor([5 5 5 5], shape=(4,), dtype=int32)