padding的规则

·          padding=‘VALID’时,输出的宽度和高度的计算公式(下图gif为例)

    Tensorflow之CNN卷积层池化层padding规则Tensorflow之CNN卷积层池化层padding规则

      输出宽度:output_width = (in_width-filter_width+1)/strides_width  =(5-3+1)/2=1.5【向上取整=2】

    输出高度:output_height = (in_height-filter_height+1)/strides_height  =(5-3+1)/2=1.5【向上取整=2】

    输出的形状[1,2,2,1]

    Tensorflow之CNN卷积层池化层padding规则

import tensorflow as tf
image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0]
input = tf.Variable(tf.constant(image,shape=[1,5,5,1]))  ##1通道输入
fil1 = [-1.0,0,1,-2,0,2,-1,0,1]
filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1]))  ##1个卷积核对应1个featuremap输出

op = tf.nn.conv2d(input,filter,strides=[1,2,2,1],padding='VALID')  ##步长2,VALID不补0操作

init = tf.global_variables_initializer()

with tf.Session() as  sess:
    sess.run(init)
    # print('input:n', sess.run(input))
    # print('filter:n', sess.run(filter))
    print('op:n',sess.run(op))

##输出结果
'''
 [[[[ 2.]
   [-1.]]

  [[-1.]
   [ 0.]]]]
'''

VALID步长2