CNN之padding:VALID/SAME
#参数说明
fileter = [filter_height, filter_width, pre_feature_maps, post_feature_maps]
input_image = [-1, in_height, in_width, channel]
strides = [1, h, w, 1]
padding = "VALID"表示卷积过程使用全0填充,而padding = "SAME",表示不使用填充;当stride表示卷积的步长,padding和stride共同决定卷积结果大小。我们通过实例来说明结果:
# 训练数据
x = tf.placeholder("float", shape=[None, 784])
# 把x更改为4维张量,第1维代表样本数量,第2维和第3维代表图像长宽, 第4维代表图像通道数, 1表示黑白
x_image = tf.reshape(x, [-1, 28, 28, 1])
# 过滤器大小为5*5, 当前层深度为1, 过滤器的深度为32
conv1_weights = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1), name="W")
#使用全0填充,[in_height/h, in_width/w]
conv1 = tf.nn.conv2d(x_image, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
conv2 = tf.nn.conv2d(x_image, conv1_weights, strides=[1, 2, 2, 1], padding='SAME')
#不使用全0填充,[in_height -(filter_height-1)/h, in_width - (filter_width-1)/w]
conv3 = tf.nn.conv2d(x_image, conv1_weights, strides=[1, 1, 1, 1], padding='VALID')
conv4 = tf.nn.conv2d(x_image, conv1_weights, strides=[1, 2, 2, 1], padding='VALID')
结果如下: