caffe 中 BatchNorm layer设定

转载于http://blog.csdn.net/u012939857/article/details/70740283

BN层的设定一般是按照conv->bn->scale->relu的顺序来形成一个block。 
关于bn,有一个注意点,caffe实现中的use_global_stats参数在训练时设置为false,测试时设置为true。 
因为在训练时bn作用的对象是一个batch_size,而不是整个训练集,如果没有将其设置为false,则有可能造成bn后数据更加偏离中心点,导致nan或87.3365的问题。

caffe 中为什么bn层要和scale层一起使用

这个问题首先要理解batchnormal是做什么的。它其实做了两件事。 
1) 输入归一化 x_norm = (x-u)/std, 其中u和std是个累计计算的均值和方差。 
2)y=alpha×x_norm + beta,对归一化后的x进行比例缩放和位移。其中alpha和beta是通过迭代学习的。 
那么caffe中的bn层其实只做了第一件事。scale层做了第二件事。 
这样也就理解了scale层里为什么要设置bias_term=True,这个偏置就对应2)件事里的beta。

代码:

第一种情况,phase: TRAIN/TEST都不加 ,caffe会自动匹配去设置use_global_stats的值


 
 
  1. layer {
  2. name: "conv1"
  3. type: "Convolution"
  4. bottom: "data"
  5. top: "conv1"
  6. param{
  7. lr_mult: 1
  8. decay_mult: 1
  9. }
  10. param{
  11. lr_mult: 2
  12. decay_mult: 0
  13. }
  14. convolution_param{
  15. num_output: 32
  16. kernel_size: 5
  17. weight_filler{
  18. type: "xavier"
  19. }
  20. bias_filler{
  21. type: "constant"
  22. }
  23. }
  24. }
  25. layer {
  26. name: "BatchNorm1"
  27. type: "BatchNorm"
  28. bottom: "conv1"
  29. top: "conv1"
  30. param {
  31. lr_mult: 0
  32. decay_mult: 0
  33. }
  34. param {
  35. lr_mult: 0
  36. decay_mult: 0
  37. }
  38. param {
  39. lr_mult: 0
  40. decay_mult: 0
  41. }
  42. }
  43. layer {
  44. name: "scale1"
  45. type: "Scale"
  46. bottom: "conv1"
  47. top: "conv1"
  48. scale_param {
  49. bias_term: true
  50. }
  51. }
  52. layer{
  53. name: "relu1"
  54. type: "ReLU"
  55. bottom: "conv1"
  56. top: "conv1"
  57. }
第二种情况:加上 use_global_stats, 测试的时候再改成true


 
 
  1. layer {
  2. name: "BatchNorm1"
  3. type: "BatchNorm"
  4. bottom: "conv1"
  5. top: "conv1"
  6. param {
  7. lr_mult: 0
  8. decay_mult: 0
  9. }
  10. param {
  11. lr_mult: 0
  12. decay_mult: 0
  13. }
  14. param {
  15. lr_mult: 0
  16. decay_mult: 0
  17. }
  18. batch_norm_param {
  19. use_global_stats: false
  20. }
  21. }






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值