reduce_sum()在tensorflow中如何工作? [英] How does reduce_sum() work in tensorflow?

查看:81
本文介绍了reduce_sum()在tensorflow中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习tensorflow,我从tensorflow网站上获取了以下代码.根据我的理解,axis = 0用于行,而axis = 1用于列.

I am learning tensorflow, I picked up the following code from the tensorflow website. According to my understanding, axis=0 is for rows and axis=1 is for columns.

他们如何获得评论中提到的输出?我已经根据我对##的想法提到了输出.

How are they getting output mentioned in comments? I have mentioned output according to my thinking against ##.

import tensorflow as tf

x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x, 0)  # [2, 2, 2] ## [3, 3]
tf.reduce_sum(x, 1)  # [3, 3] ##[2, 2, 2]
tf.reduce_sum(x, [0, 1])  # 6 ## Didn't understood at all.

推荐答案

x的形状为(2, 3)(两行三列):

x has a shape of (2, 3) (two rows and three columns):

1 1 1
1 1 1

通过执行tf.reduce_sum(x, 0),张量沿第一维(行)减小,因此结果为[1, 1, 1] + [1, 1, 1] = [2, 2, 2].

By doing tf.reduce_sum(x, 0) the tensor is reduced along the first dimension (rows), so the result is [1, 1, 1] + [1, 1, 1] = [2, 2, 2].

通过执行tf.reduce_sum(x, 1),张量沿第二维(列)减小,因此结果为[1, 1] + [1, 1] + [1, 1] = [3, 3].

By doing tf.reduce_sum(x, 1) the tensor is reduced along the second dimension (columns), so the result is [1, 1] + [1, 1] + [1, 1] = [3, 3].

通过执行tf.reduce_sum(x, [0, 1]),张量沿两个维度(行和列)均减小,因此结果为1 + 1 + 1 + 1 + 1 + 1 = 6或等效地为[1, 1, 1] + [1, 1, 1] = [2, 2, 2],然后为2 + 2 + 2 = 6(沿行减小,然后减小结果)数组).

By doing tf.reduce_sum(x, [0, 1]) the tensor is reduced along BOTH dimensions (rows and columns), so the result is 1 + 1 + 1 + 1 + 1 + 1 = 6 or, equivalently, [1, 1, 1] + [1, 1, 1] = [2, 2, 2], and then 2 + 2 + 2 = 6 (reduce along rows, then reduce the resulted array).

这篇关于reduce_sum()在tensorflow中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆