前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow学习笔记(十二):Normalization

tensorflow学习笔记(十二):Normalization

作者头像
ke1th
发布2019-05-26 12:29:06
1K0
发布2019-05-26 12:29:06
举报
文章被收录于专栏:漫漫深度学习路

Normalization

local_response_normalization

local_response_normalization出现在论文”ImageNet Classification with deep Convolutional Neural Networks”中,论文中说,这种normalization对于泛化是有好处的.

bix,y=aix,y(k+α∑min(0,i+n/2)j=max(0,i−n/2)(ajx,y)2)β

b_{x,y}^i = \frac{a_{x,y}^i}{ (k+\alpha\sum_{j=max(0,i-n/2)}^{min(0,i+n/2)}(a_{x,y}^j)^2)^\beta} 经过了一个conv2d或pooling后,我们获得了[batch_size, height, width, channels]这样一个tensor.现在,将channels称之为层,不考虑batch_size

  • ii代表第ii层
  • aix,ya_{x,y}^i就代表 第ii层的 (x,y)位置所对应的值
  • nn个相邻feature maps.
  • k...α...n...βk...\alpha ... n ...\beta是hyper parameters
  • 可以看出,这个函数的功能就是, aix,ya_{x,y}^i需要用他的相邻的map的同位置的值进行normalization 在alexnet中, k=2,n=5,α=10−4,β=0.75k=2, n=5, \alpha=10^{-4}, \beta=0.75
代码语言:javascript
复制
tf.nn.local_response_normalization(input, depth_radius=None, bias=None, alpha=None, beta=None, name=None)
'''
Local Response Normalization.
The 4-D input tensor is treated as a 3-D array of 1-D vectors (along the last dimension), and each vector is normalized independently. Within a given vector, each component is divided by the weighted, squared sum of inputs within depth_radius. In detail,
'''
"""
input: A Tensor. Must be one of the following types: float32, half. 4-D.
depth_radius: An optional int. Defaults to 5. 0-D. Half-width of the 1-D normalization window.
bias: An optional float. Defaults to 1. An offset (usually positive to avoid dividing by 0).
alpha: An optional float. Defaults to 1. A scale factor, usually positive.
beta: An optional float. Defaults to 0.5. An exponent.
name: A name for the operation (optional).
"""
  • depth_radius: 就是公式里的n/2n/2
  • bias : 公式里的kk
  • input: 将conv2d或pooling 的输出输入就行了[batch_size, height, width, channels]
  • return :[batch_size, height, width, channels], 正则化后

batch_normalization

论文地址 batch_normalization, 故名思意,就是以batch为单位进行normalization - 输入:mini_batch: In={x1,x2,..,xm}In=\{x^1,x^2,..,x^m\} - γ,β\gamma,\beta,需要学习的参数,都是向量 - ϵ\epsilon: 一个常量 - 输出: Out={y1,y2,...,ym}Out=\{y^1, y^2, ..., y^m\} 算法如下: (1)mini_batch mean:

μIn←1m∑i=1mxi

\mu_{In} \leftarrow \frac{1}{m}\sum_{i=1}^m x_i (2)mini_batch variance

σ2In=1m∑i=1m(xi−μIn)2

\sigma_{In}^2=\frac{1}{m}\sum_{i=1}^m(x^i-\mu_In)^2 (3)Normalize

x^i=xi−μInσ2In+ϵ−−−−−−√

\hat x^i=\frac{x^i-\mu_{In}}{\sqrt{\sigma_{In}^2 + \epsilon}} (4)scale and shift

yi=γx^i+β

y^i=\gamma\hat x^i + \beta 可以看出,batch_normalization之后,数据的维数没有任何变化,只是数值发生了变化 OutOut作为下一层的输入 函数: tf.nn.batch_normalization()

代码语言:javascript
复制
def batch_normalization(x,
                        mean,
                        variance,
                        offset,
                        scale,
                        variance_epsilon,
                        name=None):

Args:

  • x: Input Tensor of arbitrary dimensionality.
  • mean: A mean Tensor.
  • variance: A variance Tensor.
  • offset: An offset Tensor, often denoted β\beta in equations, or None. If present, will be added to the normalized tensor.
  • scale: A scale Tensor, often denoted γ\gamma in equations, or None. If present, the scale is applied to the normalized tensor.
  • variance_epsilon: A small float number to avoid dividing by 0.
  • name: A name for this operation (optional).
  • Returns: the normalized, scaled, offset tensor. 对于卷积,x:[bathc,height,width,depth] 对于卷积,我们要feature map中共享 γi\gamma_i 和 βi\beta_i ,所以 γ,β\gamma, \beta的维度是[depth]

现在,我们需要一个函数 返回mean和variance, 看下面.

tf.nn.moments()

代码语言:javascript
复制
def moments(x, axes, shift=None, name=None, keep_dims=False):
# for simple batch normalization pass `axes=[0]` (batch only).

对于卷积的batch_normalization, x 为[batch_size, height, width, depth],axes=[0,1,2],就会输出(mean,variance), mean 与 variance 均为标量。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年10月31日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Normalization
    • local_response_normalization
      • batch_normalization
        • tf.nn.moments()
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档