padding的规则

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

    

    输出宽度: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]

    

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

    如果strides=[1,3,3,1]的情况又是如何呢?   

    输出宽度:output_width  = (in_width-filter_width+1)/strides_width  =(5-3+1)/3=1

    输出高度:output_height = (in_height-filter_height+1)/strides_height  =(5-3+1)/3=1

    输出的形状[1,1,1,1],因此输出的结果只有一个

    

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,3,3,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)) ##输出结果
'''
op:
[[[[ 2.]]]]
'''

VALID步长3

padding=‘SAME’时,输出的宽度和高度的计算公式

    输出宽度:output_width  = in_width/strides_width=5/2=2.5【向上取整3】

    输出高度:output_height = in_height/strides_height=5/2=2.5【向上取整3】

    则输出的形状:[1,3,3,1]

    那么padding补0的规则又是如何的呢?【先确定输出形状,再计算补多少0】

    pad_width = max((out_width-1)*strides_width+filter_width-in_width,0)=max((3-1)*2+3-5,0)=2

    pad_height = max((out_height-1)*strides_height+filter_height-in_height,0)=max((3-1)*2+3-5,0)=2

    pad_top = pad_height/2=1

    pad_bottom = pad_height-pad_top=1

    pad_left = pad_width/2=1

    pad_right = pad_width-pad_left=1

        

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='SAME') ##步长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)) ##输出结果
'''
op:
[[[[ 3.]
[ 1.]
[-4.]] [[ 3.]
[ 0.]
[-3.]] [[ 4.]
[-1.]
[-3.]]]]
'''

SAME步长2

    如果步长为3呢?补0的规则又如何?

    输出宽度:output_width  = in_width/strides_width=5/3=2

    输出高度:output_height = in_height/strides_height=5/3=2

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

    那么padding补0的规则又是如何的呢?【先确定输出形状,再计算补多少0】

    pad_width = max((out_width-1)*strides_width+filter_width-in_width,0)=max((2-1)*3+3-5,0)=1

    pad_height = max((out_height-1)*strides_height+filter_height-in_height,0)=max((2-1)*3+3-5,0)=1

    pad_top = pad_height/2=0【向下取整】

    pad_bottom = pad_height-pad_top=1

    pad_left = pad_width/2=0【向下取整】

    pad_right = pad_width-pad_left=1

    

import tensorflow as tf
print(3/2)
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,3,3,1],padding='SAME') ##步长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)) ##输出结果
'''
op:
[[[[ 2.]
[-3.]] [[ 0.]
[-3.]]]]
'''

SAME步长3

最新文章

  1. C#的Socket实现UDP协议通信
  2. sphinx 增量索引 实现近实时更新
  3. Linux下设置网卡随系统启动
  4. 微信内置浏览器中,点击下拉框出现页面乱跳转现象(iphone)
  5. DIV------使用 <div> 元素的网页布局
  6. AJAX同步与异步
  7. [Machine Learning] 深度学习中消失的梯度
  8. three.js 文字显示不出来
  9. ABP示例程序-使用AngularJs,ASP.NET MVC,Web API和EntityFramework创建N层的单页面Web应用
  10. leetcode-只出现一次的数字
  11. C# 加载DotNetBar组件
  12. Asp.Net Core通过HttpStatusCode状态处理响应结果
  13. 如何访问https的网站?-【httpclient】
  14. nuxtjs中修改head及vuex的使用
  15. Centos7部署kubernetes API服务(四)
  16. windows环境下lib和dll的区别和联系详细
  17. sublime + emmet(Zen Coding)
  18. 照片管家iOS-实现本地相册、视频、安全保护、社交分享源码下载Demo
  19. BootStrapValidate 简单使用
  20. springboot配置kafka生产者和消费者详解

热门文章

  1. 如何编写testbench的总结(非常实用的总结)
  2. java Thread源码分析
  3. 手机能连接上ipsec
  4. Spring源码--Bean的管理总结(一)
  5. ESP8266-12F 中断
  6. git 的add .
  7. spring IOC(Spring 生命周期,先1.构造方式,2,初始化方法,3,目标方法,4,销毁方法)
  8. php array_pop()函数 语法
  9. POJ 2229 sumset ( 完全背包 || 规律递推DP )
  10. 移动端与PHP服务端接口通信流程设计(基础版)