-
批標準化(batch normalization,BN)一般用在激活函數之前,使結果x=Wx+bx各個維度均值爲0,方差爲1。通過規範化讓激活函數分佈在線性區間,讓每一層的輸入有一個穩定的分佈會有利於網絡的訓練。
優點:
1.加大探索步長,加快收斂速度。
2.更容易跳出局部極小。
3.破壞原來的數據分佈,一定程度上防止過擬合。
4.解決收斂速度慢和梯度爆炸。
-
affine:仿射變換,又稱仿射映射,是指在幾何中,一個向量空間進行一次,線性變換並接上一個平移,變換爲另一個向量空間。
-
mean, variance = tf.nn.moments(x, axes, name=None, keep_dims=False)
計算統計矩,輸出兩個張量,mean 是一階矩即均值,variance 則是二階中心矩即方差,axes表示在哪個維度上求解,是個list。
-
在採用隨機梯度下降算法訓練神經網絡時,使用 tf.train.ExponentialMovingAverage 滑動平均操作的意義在於提高模型在測試數據上的健壯性(robustness)。
參考:Tensorflow深度學習之九:滑動平均模型 -
tf.control_dependencies(control_inputs),此函數指定某些操作執行的依賴關係。返回一個控制依賴的上下文管理器,使用 with 關鍵字可以讓在這個上下文環境中的操作都在 control_inputs 執行
with tf.control_dependencies([a, b]): c = .... d = ...
在執行完 a,b 操作之後,才能執行 c,d 操作。意思就是 c,d 操作依賴 a,b 操作
-
y = tf.identity(x)是一個op操作表示將x的值賦予y,
-
y = x只是一個內存拷貝,並不是一個op,而control_dependencies只有當裏面是一個Op的時候才能起作用。
-
tf.cond,如果斷言 pred 爲 true 則返回 true_fn() ,否則返回 false_fn().(棄用參數)
cond (pred , true_fn = None , false_fn = None , strict = False , name = None , fn1 = None , fn2 = None)
"""Batch Normalization for TensorFlow.
Parag K. Mital, Jan 2016.
"""
import tensorflow as tf
def batch_norm(x, phase_train, scope='bn', affine=True):
"""
Batch normalization on convolutional maps.
from: https://stackoverflow.com/questions/33949786/how-could-i-
use-batch-normalization-in-tensorflow
Only modified to infer shape from input tensor x.
Parameters
----------
x
Tensor, 4D BHWD input maps
phase_train
boolean tf.Variable, true indicates training phase:is_training
scope
string, variable scope
affine
whether to affine-transform outputs
Return
------
normed
batch-normalized maps
"""
with tf.variable_scope(scope):
shape = x.get_shape().as_list()
beta = tf.Variable(tf.constant(0.0, shape=[shape[-1]]),
name='beta', trainable=True)
gamma = tf.Variable(tf.constant(1.0, shape=[shape[-1]]),
name='gamma', trainable=affine)
# 定義待更新變量列表
batch_mean, batch_var = tf.nn.moments(x, [0, 1, 2], name='moments')
# 定義滑動平均模型的類
ema = tf.train.ExponentialMovingAverage(decay=0.9)
def mean_var_with_update():
"""Summary
Returns
-------
name : TYPE
Description
"""
# 使用滑動平均模型
ema_apply_op = ema.apply([batch_mean, batch_var])
with tf.control_dependencies([ema_apply_op]):
return tf.identity(batch_mean), tf.identity(batch_var)
mean, var = tf.cond(phase_train,
mean_var_with_update,
lambda: (ema.average(batch_mean), ema.average(batch_var))
)
normed = tf.nn.batch_norm_with_global_normalization(
x, mean, var, beta, gamma, 1e-3, affine)
return normed