如何在保持相同形状和尺寸的同时获得 tensorflow 数据集中的最大值?

Posted

技术标签:

【中文标题】如何在保持相同形状和尺寸的同时获得 tensorflow 数据集中的最大值?【英文标题】:How do you get the max value in a tensorflow dataset whilst keeping the same shape and dimension? 【发布时间】:2021-12-19 22:20:38 【问题描述】:

考虑下面的代码:

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)>

任何想法如何编码?

【问题讨论】:

试试tf.repeat(tf.reduce_max(x), repeats=x.shape[0]) tf.reduce_max(x, keepdims=True) 【参考方案1】:

感谢 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)

【讨论】:

以上是关于如何在保持相同形状和尺寸的同时获得 tensorflow 数据集中的最大值?的主要内容,如果未能解决你的问题,请参考以下文章