gpt4 book ai didi

python - 沿 tensorflow 中给定张量的轴计算模式和模式计数

转载 作者:行者123 更新时间:2023-12-04 09:44:59 25 4
gpt4 key购买 nike

我有一个二维数组:

c=np.array([[1, 3, 0, 0, 3],
[1, 3, 1, 0, 2],
[1, 3, 1, 2, 2]])

我想沿轴 = 0 计算模式和模式计数。
因此结果应该是这样的:
mode = [1,3,1,0,2], mode-count=[3,3,2,2,2]

我已经在 TensorFlow 网站上进行了搜索,但除此之外找不到任何有用的 API
tf.unique_with_counts ,它需要一个一维张量。

我不想在数组的每一列上运行循环 c为了使用 tf.unique_with_counts用于计算模式和模式计数。
任何带有示例的建议都是最受欢迎的。

最佳答案

c=np.array([[1, 3, 0, 0, 3],
[1, 3, 1, 0, 2],
[1, 3, 1, 2, 2]])
c = tf.constant(c)

模式
tf.map_fn(lambda x: tf.unique_with_counts(x).y[tf.argmax(tf.unique_with_counts(x).count, output_type=tf.int32)], tf.transpose(c))

<tf.Tensor: shape=(5,), dtype=int32, numpy=array([1, 3, 1, 0, 2])>

模式计数
tf.map_fn(lambda x: tf.reduce_max(tf.unique_with_counts(x).count), tf.transpose(c))

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

我这样做只是在 map_fn 中使用 unique_with_counts

关于python - 沿 tensorflow 中给定张量的轴计算模式和模式计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62170618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com