TensorFlow学习笔记(1)——conv2d函数的padding参数详解

conv2d函数原型

    tf.nn.conv2d ( input,  filter,  strides,  padding,  use_cudnn_on_gpu=None,  name=None )

参数说明:

    input:输入图像,shape为 [ batch,  in_height,   in_width,  in_channels ],例如 [ 1, 28, 28, 1 ]

    filter:卷积核,shape为 [ filter_height,  filter_width,  in_channels,  out_channels ],如 [ 5, 5, 1, 1 ] 

    strides:步长,一维向量,长度为 4,如 [ 1, 1, 1, 1 ]

    padding:string类型,取值为‘SAME’或‘VALID’,例如‘SAME’


第一种padding方式:‘SAME’

1、卷积后的Feature map尺寸的计算方式为:

    out_height = ceil ( float ( in_height )  /  float ( strides [1] ) )
    out_width  = ceil ( float ( in_width ) / float ( strides [2] ) )


2、例子:

    import  tensorflow  as  tf

    x_image  =  tf.Variable ( tf.random_normal ( [ 1, 28, 28, 1] ) )

    kernel  =  tf.Variable ( tf.random_normal ( [5, 5, 1, 1] ) ) 

    conv  =  tf.nn.conv2d ( x_image,  kernel,  strides= [ 1, 1, 1, 1 ],  padding='SAME' )

    with  tf.Session ()  as  sess: 

        sess.run ( tf.global_variables_initializer () ) 

        result = ( sess.run ( conv ) ) 

        print ( result.shape )


    程序运行结果:

    ====== RESTART: D:\Python_code\ML\test.py ======
    ( 1,  28,  28, 1 )
    >>> 


3、经过卷积后的Feature map尺寸的计算方式说明:

    out_height = ceil ( float ( in_height ) / float ( strides[1] ) ) = ceil ( float ( 28 ) / float ( 1 ) )  = 28
    out_width  = ceil ( float ( in_width ) / float ( strides[2] ) ) = ceil ( float ( 28 ) / float ( 1 ) )  = 28


第二种padding方式:‘VALID’

1、卷积后的Feature map尺寸的计算方式为:

    out_height = ceil ( float ( in_height - filter_height + 1 ) / float ( strides[1] ) )
    out_width  = ceil ( float ( in_width - filter_width + 1 ) / float ( strides[2] ) )


2、例子:

    import  tensorflow  as  tf

    x_image  =  tf.Variable ( tf.random_normal ( [ 1, 28, 28, 1] ) )

    kernel  =  tf.Variable ( tf.random_normal ( [5, 5, 1, 1] ) ) 

    conv  =  tf.nn.conv2d ( x_image,  kernel,  strides= [ 1, 1, 1, 1 ],  padding='VALID' )

    with  tf.Session ()  as  sess: 

        sess.run ( tf.global_variables_initializer () ) 

        result = ( sess.run ( conv ) ) 

        print ( result.shape )


    程序运行结果:

    ====== RESTART: D:\Python_code\ML\test.py ======
    ( 1,  24,  24, 1 )
    >>> 


3、经过卷积后的Feature map尺寸的计算方式说明:

    out_height = ceil ( float (i n_height - filter_height + 1 ) / float ( strides[1] ) ) = ceil ( float ( 28 - 5+ 1 )  / float ( 1 ) ) = 24
    out_width  = ceil ( float ( in_width - filter_width + 1 ) / float ( strides[2] ) ) = ceil ( float ( 28 - 5+ 1 )  / float ( 1 ) ) = 24






  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值