tensorflow中的lrn函数详解

LRN函数类似DROPOUT和数据增强作为relu激励之后防止数据过拟合而提出的一种处理方法,全称是 local response normalization–局部响应标准化。这个函数很少使用,基本上被类似DROPOUT这样的方法取代,具体原理还是值得一看的

函数原型

def lrn(input, depth_radius=None, bias=None, alpha=None, beta=None,
        name=None):

官方文档给的解释如下,这些官方文档看了没啥卵用,越看越糊涂

sqr_sum[a, b, c, d] =
    sum(input[a, b, c, d - depth_radius : d + depth_radius + 1] ** 2)
output = input / (bias + alpha * sqr_sum) ** beta

见最早的出处AlexNet论文对它的定义, 《ImageNet Classification with Deep ConvolutionalNeural Networks》

  《tensorflow中的lrn函数详解》

i:代表下标,你要计算像素值的下标,从0计算起

j:平方累加索引,代表从j~i的像素值平方求和

x,y:像素的位置,公式中用不到

a:代表feature map里面的 i 对应像素的具体值

N:每个feature map里面最内层向量的列数

k:超参数,由原型中的bias指定

α:超参数,由原型中的alpha指定

n/2:超参数,由原型中的deepth_radius指定

β:超参数,由原型中的belta指定

看一个栗子,理解上诉的参数,进而理解rln函数

import tensorflow as tf  
  
a = tf.constant([  
    [[1.0, 2.0, 3.0, 4.0],  
     [5.0, 6.0, 7.0, 8.0],  
     [8.0, 7.0, 6.0, 5.0],  
     [4.0, 3.0, 2.0, 1.0]],  
    [[4.0, 3.0, 2.0, 1.0],  
     [8.0, 7.0, 6.0, 5.0],  
     [1.0, 2.0, 3.0, 4.0],  
     [5.0, 6.0, 7.0, 8.0]]  
])  
#reshape 1批次  2x2x8的feature map  
a = tf.reshape(a, [1, 2, 2, 8])  
  
normal_a=tf.nn.lrn(a,2,0,1,1)  
with tf.Session() as sess:  
    print("feature map:")  
    image = sess.run(a)  
    print (image)  
    print("normalized feature map:")  
    normal = sess.run(normal_a)  
    print (normal) 

你将得到输出:

    feature map:  
    [[[[ 1.  2.  3.  4.  5.  6.  7.  8.]  
       [ 8.  7.  6.  5.  4.  3.  2.  1.]]  
      
      [[ 4.  3.  2.  1.  8.  7.  6.  5.]  
       [ 1.  2.  3.  4.  5.  6.  7.  8.]]]]  
    normalized feature map:  
    [[[[ 0.07142857  0.06666667  0.05454545  0.04444445  0.03703704  0.03157895  
         0.04022989  0.05369128]  
       [ 0.05369128  0.04022989  0.03157895  0.03703704  0.04444445  0.05454545  
         0.06666667  0.07142857]]  
      
      [[ 0.13793103  0.10000001  0.0212766   0.00787402  0.05194805  0.04  
         0.03448276  0.04545454]  
       [ 0.07142857  0.06666667  0.05454545  0.04444445  0.03703704  0.03157895  
         0.04022989  0.05369128]]]]  

分析如下:

由调用 lrn(a,2,0,1,1)得出 n/2=2,k=0,α=1,β=1,N=8

第一行第一个数来说:i = 0

a = 1,min(N-1, i+n/2) = min(7, 2)=2,j = max(0, i – n/2)=max(0, 0)=0,下标从0~2个数平方求和, b=1/(1^2 + 2^2 + 3^2)=1/14 = 0.071428571
同理,第一行第四个数来说:i = 3

a = 4,min(N-1, i+n/2) = min(7, 5 )=5, j = max(0,1) = 1,下标从1~5进行平方求和,b = 4/(2^2 + 3^2 + 4^2 + 5^2 + 6^2) = 4/90=0.044444444
再来一个,第二行第一个数来说: i = 0

a = 8, min(N-1, i+n/2) = min(7, 2) = 2, j=max(0,0)=0, 下标从0~2的3个数平方求和,b = 8/(8^2 + 7^2 + 6^2)=8/149=0.053691275

其他的也是类似操作
参考文献

【TensorFlow】tf.nn.local_response_normalization详解,lrn正则法如何计算?

TensorFlow之深入理解AlexNet

Tensorflow的LRN是怎么做的

ImageNet Classification with Deep ConvolutionalNeural Networks

 

    原文作者:banana10060342
    原文地址: https://blog.csdn.net/banana1006034246/article/details/75204013
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞