zoukankan      html  css  js  c++  java
  • TensorFlow池化层-函数

    池化层的作用如下-引用《TensorFlow实践》:

      池化层的作用是减少过拟合,并通过减小输入的尺寸提高性能。他们可以用来对输入进行降采样,但会为后续层保留重要的信息。只使用tf.nn.conv2d来减小输入的尺寸也是可以的,但是池化层的效率更高

    常见的TensorFlow提供的激活函数如下:(详细请参考http://www.tensorfly.cn/tfdoc/api_docs/python/nn.html)

    1.tf.nn.max_pool(value, ksize, strides, padding, name=None)

    Performs the max pooling on the input.

    • value: A 4-D Tensor with shape [batch, height, width, channels] and type float32,float64qint8quint8qint32.
    • ksize: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor.
    • strides: A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.
    • padding: A string, either 'VALID' or 'SAME'. The padding algorithm.
    • name: Optional name for the operation.

    当我们的ksize=[1 3 3 1]式,我们取3X3模板池pool当中最大的数据当做中心点的值,strides作为滑动跳跃的间隔,代码如下所示:

    import tensorflow as tf
    
    batch_size = 1
    input_height = 3
    input_width = 3
    input_channels = 1
    layer_input = tf.constant([
        [
            [[1.0],[0.2],[1.5]],
            [[0.1],[1.2],[1.4]],
            [[1.1],[0.4],[0.4]]
        ]
    ])
    kernel = [batch_size, input_height, input_width,input_channels]
    max_pool = tf.nn.max_pool(layer_input,kernel,[1,1,1,1],padding='VALID',name=None)
    sess = tf.Session()
    sess.run(max_pool)

    输出结果如下(注意max_pool的输入的参数的维数一定要正确,否则会报错):

    2.tf.nn.avg_pool(value, ksize, strides, padding, name=None)

    Performs the average pooling on the input.

    Each entry in output is the mean of the corresponding size ksize window in value.

    • value: A 4-D Tensor of shape [batch, height, width, channels] and type float32,float64qint8quint8, or qint32.
    • ksize: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor.
    • strides: A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.
    • padding: A string, either 'VALID' or 'SAME'. The padding algorithm.
    • name: Optional name for the operation.

    跳跃遍历一个张量,并将被卷积核覆盖的各深度值去平均。当整个卷积核都非常重要时,若需要实现值的缩减,平均池化非常有用,例如输入张量宽度和高度很大,但深度很小的情况。

    import tensorflow as tf
    
    batch_size = 1
    input_height = 3
    input_width = 3
    input_channels = 1
    layer_input = tf.constant([
        [
            [[1.0],[1.0],[1.0]],
            [[1.0],[0.5],[0.0]],
            [[0.0],[0.0],[0.0]]
        ]
    ])
    kernel = [batch_size, input_height, input_width,input_channels]
    avg_pool = tf.nn.mavg_pool(layer_input,kernel,[1,1,1,1],padding='VALID',name=None)
    sess = tf.Session()
    sess.run(avg_pool)

     输出结果如下:(1.0+1.0+1.0+0.5+0.0+0.0+0.0+0.0)/9=0.5

  • 相关阅读:
    每日英语:5 Ways the World Will Change Radically This Century
    每日英语:Foreign Visits Rise Amid Europe's Downturn
    vs2010和Matlab R2011b 混合编程的配置
    每日英语:Hon Hai Riot Underlines Rising Tensions in Chinese Factories
    优雅的实现对外接口,要注意哪些问题?
    一文搞定关系数据库设计要领,值得收藏!
    面试官:如果要存 IP 地址,用什么数据类型比较好?
    卧槽!IDEA 写代码防沉迷了?
    面试官:Minor GC、Major GC、Full GC 区别?我竟然答不上来。。
    Spring Boot + Vue + Shiro 实现前后端分离、权限控制
  • 原文地址:https://www.cnblogs.com/uestc-mm/p/7326924.html
Copyright © 2011-2022 走看看