tf.reduce_sum (API r1.3)

tf.reduce_sum (API r1.3)

https://github.com/tensorflow/docs/tree/r1.3/site/en/api_docs/api_docs/python/tf
site/en/api_docs/api_docs/python/tf/reduce_sum.md

reduce_sum(
    input_tensor,
    axis=None,
    keep_dims=False,
    name=None,
    reduction_indices=None
)

Defined in tensorflow/python/ops/math_ops.py.
See the guide: Math > Reduction

Computes the sum of elements across dimensions of a tensor.
计算一个张量的各个维度上元素的总和。

压缩求和,用于降维。

Reduces input_tensor along the dimensions given in axis. Unless keep_dims is true, the rank of the tensor is reduced by 1 for each entry in axis. If keep_dims is true, the reduced dimensions are retained with length 1.
函数中的 input_tensor 是按照 axis 中已经给定的维度来减少的。除非 keep_dims 是 true,否则张量的秩将在 axis 的每个条目中减少1。如果 keep_dims 为true,则减小的维度将保留为长度 1。

If axis has no entries, all dimensions are reduced, and a tensor with a single element is returned.
如果 axis 没有条目,则缩小所有维度,并返回具有单个元素的张量。

reduction [rɪ'dʌkʃ(ə)n]:n. 减少,下降,缩小,还原反应

For example:

x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x)  # 6
tf.reduce_sum(x, 0)  # [2, 2, 2]
tf.reduce_sum(x, 1)  # [3, 3]
tf.reduce_sum(x, 1, keep_dims=True)  # [[3], [3]]
tf.reduce_sum(x, [0, 1])  # 6

1. Args

  • input_tensor: The tensor to reduce. Should have numeric type.
  • axis: The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(input_tensor), rank(input_tensor)). (如果为 None (默认),则缩小所有尺寸。)
  • keep_dims: If true, retains reduced dimensions with length 1.
  • name: A name for the operation (optional). (操作的名称 (可选)。)
  • reduction_indices: The old (deprecated) name for axis.

二维的 input_tensor 张量,0 表示按列求和,1 表示按行求和,[0, 1] 表示先按列求和再按行求和。

2. Returns

The reduced tensor.

3. numpy compatibility

Equivalent to np.sum.

deprecate ['deprɪkeɪt]:vt. 反对,抨击,轻视,声明不赞成
equivalent [ɪ'kwɪv(ə)l(ə)nt]:adj. (在价值、数量等方面) 相等的,等价的,等效的,等量的,同意义的 n. 对等的人 (或事物),当量

4. Example

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys
import numpy as np
import tensorflow as tf

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")

t1 = tf.constant([[0, 1, 2], [3, 4, 5]], dtype=np.float32)

rs0 = tf.reduce_sum(t1)
rs1 = tf.reduce_sum(t1, 0)
rs2 = tf.reduce_sum(t1, 1)
rs3 = tf.reduce_sum(t1, 1, keep_dims=True)
rs4 = tf.reduce_sum(t1, [0, 1])

with tf.Session() as sess:
    input_t1 = sess.run(t1)
    print("input_t1.shape:", input_t1.shape)
    print("input_t1:\n", input_t1)
    print('\n')

    output0 = sess.run(rs0)
    print("output0.shape:", output0.shape)
    print("output0:\n", output0)
    print('\n')

    output1 = sess.run(rs1)
    print("output1.shape:", output1.shape)
    print("output1:\n", output1)
    print('\n')

    output2 = sess.run(rs2)
    print("output2.shape:", output2.shape)
    print("output2:\n", output2)
    print('\n')

    output3 = sess.run(rs3)
    print("output3.shape:", output3.shape)
    print("output3:\n", output3)
    print('\n')

    output4 = sess.run(rs4)
    print("output4.shape:", output4.shape)
    print("output4:\n", output4)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
2019-08-21 10:40:04.972994: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-08-21 10:40:05.041651: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-21 10:40:05.041890: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.28GiB
2019-08-21 10:40:05.041901: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
input_t1.shape: (2, 3)
input_t1:
 [[0. 1. 2.]
 [3. 4. 5.]]


output0.shape: ()
output0:
 15.0


output1.shape: (3,)
output1:
 [3. 5. 7.]


output2.shape: (2,)
output2:
 [ 3. 12.]


output3.shape: (2, 1)
output3:
 [[ 3.]
 [12.]]


output4.shape: ()
output4:
 15.0

Process finished with exit code 0

tf.reduce_sum (API r1.3)_第1张图片

你可能感兴趣的:(tf.reduce_sum,API,r1.3)