tf.reduce_sum的使用

import tensorflow as tf

a = tf.constant([[[1,2],[11,12]], [[21,22],[31,32]]])
sum1 = tf.reduce_sum(a)
sum2 = tf.reduce_sum(a, 0)
sum3 = tf.reduce_sum(a, 1)
sum4 = tf.reduce_sum(a, 2)
sum5 = tf.reduce_sum(a, [0, 1])
sum6 = tf.reduce_sum(a, [1, 2])
sum7 = tf.reduce_sum(a, 0, keepdims=True)
with tf.Session() as sess:
    print(sess.run(sum1))
    print(sess.run(sum2))
    print(sess.run(sum3))
    print(sess.run(sum4))
    print(sess.run(sum5))
    print(sess.run(sum6))
    print(sess.run(sum7))

输出结果:

132
[[22 24]
 [42 44]]
[[12 14]
 [52 54]]
[[ 3 23]
 [43 63]]
[64 68]
[ 26 106]
[[[22 24]
  [42 44]]]
>>> 

你可能感兴趣的:(TensorFlow,tf.reduce_sum,TensorFlow,深度学习,python)