python - 计算 TensorFlow 中 tf.SparseTensor 每一行的平均值

标签 python tensorflow

我想计算 tf.SparseTensor 的 axis=0 的平均值。我想要类似 tf.sparse_reduce_sum 的东西。 TensorFlow 没有提供类似的均值计算函数。有什么方法可以计算每一行中的值,以便将它们除以总和吗?

indices = np.array([[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5],
               [1, 0], [1, 1], [1, 3], [1, 4], [1, 5],
               [2, 1], [2, 2], [2, 3], [2, 4],
               [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5],
               [4, 0], [4, 2], [4, 3], [4, 4], [4, 5]], dtype=np.int64)

values = np.array([7, 6, 7, 4, 5, 4,
                   6, 7, 4, 3, 4,
                   3, 3, 1, 1,
                   1, 2, 2, 3, 3, 4,
                   1, 1, 2, 3, 3], dtype=np.float64)

dense_shape = np.array([5, 6], dtype=np.int64)

tRatings = tf.SparseTensor(indices, values, dense_shape)

最佳答案

您可以通过除以第 0 维的大小来计算减少的总和的减少平均值:

tRatings = tf.SparseTensor(indices, values, dense_shape)
reduced_sum = tf.sparse_reduce_sum(tRatings, 0)  # Sum of each row
reduced_mean = reduced_sum / tf.cast(tRatings.dense_shape[0], tf.float64)  # Mean of each row

关于python - 计算 TensorFlow 中 tf.SparseTensor 每一行的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52763539/

相关文章:

python - NotFoundError(请参阅上面的回溯): Key Variable not found in checkpoint

python - 无法在python3中安装boto

python - 在 Tensorflow 中执行惰性条件

tensorflow - keras 在第一个纪元停止工作

python - Plotly 不显示轴标签或标题

python - 为什么tensorflow的max_pooling2d需要rank 4的输入?

python - tf.gradients() 对 ys 求和,是吗?

python - 不使用 set() 删除重复项

c# - 在 C# 中隐藏大括号

python - 如何计算两条线的交点?