• 【CNN记录】tensorflow中reduceMax、reduceMin、reduceSum、reduceMean介绍


    reduceSum 

    tf.reduce_sum(
        input_tensor, axis=None, keepdims=False, name=None
    )

    说明:在指定维度上求多维tensor元素之和,并且按照axis消除该维度,除非keepdims为True

    input_tensor:输入张量

    axis:需要降维的维度

    keepdims:是否保留维度

    name:操作数的名字(可选)

    举个栗子

    1. >a = np.random.random_integers(3,size=(2,3,4))
    2. >input = tf.constant(a)
    3. tf.Tensor(
    4. [[[2 3 1 1]
    5. [1 1 2 2]
    6. [1 2 2 1]]
    7. [[1 2 1 2]
    8. [2 2 3 3]
    9. [3 1 3 3]]], shape=(2, 3, 4), dtype=int32)
    10. >output = tf.reduce_sum(input);
    11. tf.Tensor(45, shape=(), dtype=int32)
    12. #axis为None的时候,所有维度都会被降维(展成一维加起来),所有元素加起来为45
    13. >output = tf.reduce_sum(input,axis=0)
    14. tf.Tensor(
    15. [[3 5 2 3]
    16. [3 3 5 5]
    17. [4 3 5 4]], shape=(3, 4), dtype=int32)
    18. #axis=0时,最外的维度相加,这里就是2+1、3+2、1+1、1+2 ...,最终得到一个(3,4)的张量
    19. 如果keepdims=True
    20. >output = tf.reduce_sum(input,axis=0,keepdims=True)
    21. tf.Tensor(
    22. [[[3 5 2 3]
    23. [3 3 5 5]
    24. [4 3 5 4]]], shape=(1, 3, 4), dtype=int32)
    25. #保留了axis指定的维度,(2,3,4)变成(1,3,4)
    26. >output = tf.reduce_sum(input,axis=1);
    27. tf.Tensor(
    28. [[4 6 5 4]
    29. [6 5 7 8]], shape=(2, 4), dtype=int32)
    30. #axis=1时,dim1维度相加,这里就是2+1+1、3+1+2、1+2+2、1+2+1 ...,最终得到一个(2,4)的张量

    reduceMax

    tf.reduce_max(
        input_tensor, axis=None, keepdims=False, name=None
    )

    参数说明参考reducesum 

    继续举个栗子

    1. >a = np.random.random_integers(3,size=(2,3,4)) #创建一个固定dim的numpy
    2. >input = tf.constant(a)
    3. tf.Tensor(
    4. [[[2 3 1 2]
    5. [3 2 2 1]
    6. [2 1 1 1]]
    7. [[2 3 3 2]
    8. [3 1 3 1]
    9. [1 1 2 1]]], shape=(2, 3, 4), dtype=int32)
    10. >output = tf.reduce_max(input)
    11. tf.Tensor(3, shape=(), dtype=int32)
    12. #axis为None时取所有元素最大值
    13. >output = tf.reduce_max(input,axis=0)
    14. tf.Tensor(
    15. [[2 3 3 2]
    16. [3 2 3 1]
    17. [2 1 2 1]], shape=(3, 4), dtype=int32)
    18. #axis=0的时候,按照dim0取最大值,即:max(2,2)、max(3,3)、max(1,3)、max(2,2)。。。
    19. #最终得到一个(3,4)的tensor

    reduceMin

    tf.reduce_min(
        input_tensor, axis=None, keepdims=False, name=None
    )

    说明:同reduceMax,只是取最小值

     reduceMean

    说明:指定axis代表的维度求均值,并且按照axis消除该维度,除非keepdims为True

    1. >input = tf.constant([[1., 1.], [2., 2.]])
    2. >output = tf.reduce_mean(input)
    3. tf.Tensor(1.5, shape=(), dtype=float32)
    4. #所有元素求均值
    5. >output = tf.reduce_mean(input, 0)
    6. tf.Tensor([1.5 1.5], shape=(2,), dtype=float32)
    7. #dim0维度求均值,即计算[1,2]、[1,2]均值为1.5、1.5
    8. >output = tf.reduce_mean(input, 1)
    9. tf.Tensor([1. 2.], shape=(2,), dtype=float32)
    10. #dim0维度求均值,即计算[1,1]、[2,2]均值为1.、2.

    其实这些都差不多,包括argmin、argmax

  • 相关阅读:
    【数据结构】——栈(Last In First Out)与队列(First In First Out)详解
    2023上海工博会,正运动展位现场直击(二)
    GO语言-channel-定时器
    consul 备份还原导入导出
    XML - XML学习/XML文件解析器(C++)实现
    这可能是我见过最可爱的乒乓女孩了!
    【Qt】QCheckBox
    Camera1 源码解析系列(三)—— Camera1 hw_get_module() 解析
    LCR 170. 交易逆序对的总数
    C++——类和对象(中)(1)
  • 原文地址:https://blog.csdn.net/q2519008/article/details/127964600