【TensorFlow】tf.nn.lrn 局部响应归一化函数

 归一化、标准化和正则化的关系

局部响应归一化,可防止过拟合,原理是生物学上的 ‘侧抑制’,局部神经元的活动创建竞争机制,使得其中响应比较大的值变得相对更大,并抑制其他反馈较小的神经元,增强了模型的泛化能力(通俗地讲就是 强的更强,弱的更弱)

这个技术主要是深度学习训练时的一种提高准确度的技术方法,LRN一般是在激活、池化后进行的一种处理方法。

LRN 一般用于使用了 RELU 激活函数的层中。

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

1、参数说明:

  • input 表示输入数据,
  • depth_radius 表示使用前后几层进行归一化操作,
  • bias 表示偏移量,
  • alpha 和 beta 表示系数。

2、参考资料:

 Vici__lrn1

 

3、验证公式与函数相对应:

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
import tensorflow as tf
import numpy as np

# 对x的[1, 1, 1, 1]进行局部响应归一化操作,验证公式与函数相对应

# 方式一:公式计算
x = np.array([i for i in range(1, 33)]).reshape([2, 2, 2, 4])
sqr_sum = np.zeros_like(x)
sqr_sum[1, 1, 1, 1] = sum(x[1, 1, 1, 0:3] ** 2)
print(sqr_sum[1, 1, 1, 1])
z = (x[1, 1, 1, 1] / (0 + sqr_sum[1, 1, 1, 1]*1)) ** 1 

# 方式二:调用函数
sess = tf.Session()
y = tf.nn.lrn(input=x, depth_radius=1, bias=0, alpha=1, beta=1)

print('Method 1 result: ',z)
print('Method 2 result: ',sess.run(y[1, 1, 1, 1]))


'''
    output:
            Method 1 result:  0.0111029
            Method 2 result:  0.0111029
'''

参考文献:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
局部响应归一化(Local Response Normalization,LRN)是一种常用的神经网络正则化方法,其作用是对神经元的输出进行归一化,使得其对输入的微小变化不敏感,从而提高模型的泛化能力。下面是使用Python实现局部响应归一化的示例代码: ```python import numpy as np def local_response_normalization(x, alpha=1e-4, k=2, beta=0.75, n=5): # x为输入数据,alpha为参数,k为参数,beta为参数,n为参数 # 在这里,我们假设输入数据为一个四维张量,即(batch_size, height, width, channels) # 首先计算分母部分 square = np.square(x) scale = k for i in range(n//2): scale += alpha * square[:, :, :, i:i+1] for i in range(n//2, x.shape[-1]-n//2): scale += alpha * (square[:, :, :, i-n//2:i-n//2+1] + square[:, :, :, i+n//2:i+n//2+1]) for i in range(x.shape[-1]-n//2, x.shape[-1]): scale += alpha * square[:, :, :, i:i+1] # 计算最终的输出 return x / np.power(scale, beta) ``` 在这里,我们假设输入数据为一个四维张量,即(batch_size, height, width, channels),其中: - alpha:一个超参数,控制归一化的强度。 - k:一个超参数,控制归一化的强度。 - beta:一个超参数,控制归一化的强度。 - n:一个超参数,控制归一化局部范围大小。 实现中,我们先计算分母部分,然后计算最终的输出。其中,分母部分的计算需要分三种情况进行处理。最终的输出即为输入数据除以分母部分的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值