我不明白为什么以下代码的输出是[7 56].
import tensorflow as tf
x = tf.constant([[1, 2, 4], [8, 16, 32]])
a = tf.reduce_sum(x, -1) # [ 9 18 36]
with tf.Session() as sess:
output_a = sess.run(a)
print(output_a)
Run Code Online (Sandbox Code Playgroud)
我知道按行添加已经完成。但也有人能够解释为什么一些轻-1的reduce_sum功能已被处理,总结行中的所有值?
我运行你的代码,实际上给了我一个不同的答案:
import tensorflow as tf
x = tf.constant([[1, 2, 4], [8, 16, 32]])
a = tf.reduce_sum(x, -1)
tf.print(a)
Run Code Online (Sandbox Code Playgroud)
答案是[7,56],加上1+2+4=7,8+16+32=56。
axis:要减小的尺寸。我的理解:
tf.reduce_sum(x, -1) 等于 tf.reduce_sum(x, 1) ,因为只有 2 维。
[[7]
[56]]
由于这里没有 'keepdims=True',[] 将被删除,我们得到结果 [7,56]
y = tf.constant([[[1, 2, 4], [1, 0, 3]],[[1,2,3],[2,2,1]]])
c = tf.reduce_sum(y, 1) # if (y,-1) will be [[7,4],[6,5]]
tf.print(c) #[[2 2 7], [3 4 4]]
| 归档时间: |
|
| 查看次数: |
7440 次 |
| 最近记录: |