如何在保持相同形状和维度的同时获得张量流数据集中的最大值?

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

考虑以下代码:

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

上面的结果会给我输出 x:

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

但是,我想以这种格式获取最大值:

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

知道如何编码吗?

感谢 Flavia Giammarino 的帮助。

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)

我根据需要得到以下输出:

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