TensorFlow学习笔记(4)tf.sum, 求和

用例对应的源代码,觉得有帮助可以Star

import tensorflow as tf

#  tf.reduce_sum(
#      input_tensor,
#      axis=None,
#      keepdims=None,
#      name=None,
#      reduction_indices=None,
#      keep_dims=None
#      )
#
#  Computes the sum of elements across dimensions of a tensor. (deprecated arguments)


#  1 2  4
#  8 16 32
#
#  x has a shape of (2, 3) (two rows and three columns):
#
#  The 0 axis in tensorflow is the rows, 1 axis is the columns.
#  By doing tf.reduce_sum(x, 0) the tensor is reduced along the first dimension
#  (rows), so the result is [1, 2, 4] + [8, 16, 32] = [9, 18, 32].
#
#  By doing tf.reduce_sum(x, 1) the tensor is reduced along the second dimension
#  (columns), so the result is [1, 9] + [2, 16] + [4, 32] = [7, 56].

x = tf.constant([[1, 2, 4], [8, 16, 32]])
a = tf.reduce_sum(x, 0)  # [ 9 18 36]
b = tf.reduce_sum(x, 1)  # [ 7 56]
c = tf.reduce_sum(x, [0, 1])  # 63
d = tf.reduce_sum(x, 1, keepdims=True) # [[ 7]
                                       #  [56]]


with tf.Session() as sess:
    print(sess.run(tf.rank(x)))

    output_a = sess.run(a)
    print(output_a)
    output_b = sess.run(b)
    print(output_b)
    output_c = sess.run(c)
    print(output_c)
    output_d = sess.run(d)
    print(output_d)                                                                                         
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,112评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,739评论 2 59
  • operator.itemgetter屏幕快照 2018-07-31 下午5.22.57.png 在排序中也很常用...
    ivywenyuan阅读 246评论 0 0

友情链接更多精彩内容