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)                                                                                         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kelsel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值