• tf.nn.conv2d 和 tf.nn.max_pool 中 padding 分别为 'VALID' 和 'SAME' 的直觉上的经验和测试代码


    这个地方一开始是迷糊的,写代码做比较分析,总结出直觉上的经验.
    某人若想看精准的解释,移步这个网址(http://blog.csdn.net/fireflychh/article/details/73743849),但我觉得直觉上的经验更有用,如下:

    直觉上的经验:

    1. 一件确定的事: padding 无论取 'SAME' 还是取 'VALID', 它在 conv2d 和 max_pool 上的表现是一致的;
    2. padding = 'SAME' 时,输出并不一定和原图size一致,但会保证覆盖原图所有像素,不会舍弃边上的莫些元素;
    3. padding = 'VALID' 时,输出的size总比原图的size小,有时不会覆盖原图所有元素(既,可能舍弃边上的某些元素).
    # -*- coding: utf-8 -*-
    import tensorflow as tf
    import numpy as np
    
    
    def pooling_show():
        a = tf.Variable(tf.random_normal(X))
        pooling = tf.nn.max_pool(a, pooling_filter, pooling_strides, padding=pad)
        # VALID (1, 2, 2, 7)
        # SAME (1, 3, 3, 7)
    
        init = tf.global_variables_initializer()
    
        with tf.Session() as sess:
            sess.run(init)
    
            print 'image: '
            image = sess.run(a)
            print image.shape
    
            print 'pooling result: '
            res = sess.run(pooling)
            print res.shape
    
    
    def conv2d_padding_show():
        # [1, 13, 13, 2] ---> [m, height, width, channel]
        input = tf.Variable(tf.random_normal(X))
        # [6, 6, 2, 7] ---> [height, width, prev_channel, output_channel]
        filter = tf.Variable(tf.random_normal(conv2d_filter))
    
        op = tf.nn.conv2d(input, filter, strides=conv2d_strides, padding=pad)
        # VALID (1, 2, 2, 7)
        # SAME  (1, 3, 3, 7)
    
        init = tf.global_variables_initializer()
    
        with tf.Session() as sess:
            sess.run(init)
    
            print 'image: '
            image = sess.run(input)
            print image.shape
    
            print 'result: '
            res = sess.run(op)
            print res.shape
    
    
    pad = 'VALID'
    
    # X ---> [m, height, width, channel]
    # X = [1, 13, 13, 7]
    X = [1, 8, 8, 3]
    
    # ---> [1, f, f, 1]
    # pooling_filter = [1, 6, 6, 1]
    pooling_filter = [1, 2, 2, 1]
    
    # ---> [1, s, s, 1]
    # pooling_strides = [1, 5, 5, 1]
    pooling_strides = [1, 2, 2, 1]
    
    # ---> [height, width, prev_channel, output_channel]
    # conv2d_filter = [6, 6, 7, 7]
    conv2d_filter = [2, 2, 3, 3]
    
    # ---> [1, s, s, 1]
    # conv2d_strides = [1, 5, 5, 1]
    conv2d_strides = [1, 2, 2, 1]
    
    # 自己改改 X, fileter, strides 的值,配合直觉经验,会有更好的理解
    conv2d_padding_show()
    pooling_show()
    
  • 相关阅读:
    [贪心] JZOJ P3757 随机生成器
    [kmp] JZOJ P3756 动物园
    [kmp] JZOJ P3756 动物园
    [记忆化搜索] JZOJ P3767 路径
    [dfs序][线段树][并查集] JZOJ P3766 大融合
    [归并][随机算法] JZOJ P3765 想法
    [枚举][dfs] JOZJ P3749 Fox and City
    [hash] JZOJ P3669 抄卡组
    [dfs][图] 洛谷 P1330 封锁阳光大学
    [并查集]NOIP 2015 Day1 信息传递
  • 原文地址:https://www.cnblogs.com/ZhongliangXiang/p/8035247.html
Copyright © 2020-2023  润新知