我想计算 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/