gpt4 book ai didi

python - TensorFlow - 在计算张量的平均值时忽略无限值

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:15 24 4
gpt4 key购买 nike

这可能是一个基本问题,但我找不到解决方案:

我需要计算张量的平均值忽略任何非有限值。

例如 mean([2.0, 3.0, inf, 5.0]) 应该返回 3.333 而不是 inf 也不是 2.5.

我试过 sess.run(tf.reduce_mean([2.0, 3.0, inf, 5.0])) 但它返回 inf

最佳答案

您可以使用 is_finite 的组合和 boolean_mask

import tensorflow as tf

x = tf.constant([2, 3, float('Inf'), 5])
mymean = tf.reduce_mean(tf.boolean_mask(x, tf.is_finite(x)))

sess = tf.Session()
sess.run(mymean)

请注意,is_finite 也会删除 NaN 值。

关于python - TensorFlow - 在计算张量的平均值时忽略无限值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43545121/

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