- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章TensorFlow实现Batch Normalization由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1、bn(batch normalization)算法 。
1. 对数据进行归一化处理的重要性 。
神经网络学习过程的本质就是学习数据分布,在训练数据与测试数据分布不同情况下,模型的泛化能力就大大降低;另一方面,若训练过程中每批batch的数据分布也各不相同,那么网络每批迭代学习过程也会出现较大波动,使之更难趋于收敛,降低训练收敛速度。对于深层网络,网络前几层的微小变化都会被网络累积放大,则训练数据的分布变化问题会被放大,更加影响训练速度.
2. bn算法的强大之处 。
1)为了加速梯度下降算法的训练,我们可以采取指数衰减学习率等方法在初期快速学习,后期缓慢进入全局最优区域。使用bn算法后,就可以直接选择比较大的学习率,且设置很大的学习率衰减速度,大大提高训练速度。即使选择了较小的学习率,也会比以前不使用bn情况下的收敛速度快。总结就是bn算法具有快速收敛的特性.
2)bn具有提高网络泛化能力的特性。采用bn算法后,就可以移除针对过拟合问题而设置的dropout和l2正则化项,或者采用更小的l2正则化参数.
3)bn本身是一个归一化网络层,则局部响应归一化层(local response normalization,lrn层)则可不需要了(alexnet网络中使用到).
3. bn算法概述 。
bn算法提出了变换重构,引入了可学习参数γ、β,这就是算法的关键之处:
引入这两个参数后,我们的网络便可以学习恢复出原是网络所要学习的特征分布,bn层的钱箱传到过程如下:
其中m为batchsize。batchnormalization中所有的操作都是平滑可导,这使得back propagation可以有效运行并学到相应的参数γ,β。需要注意的一点是batch normalization在training和testing时行为有所差别。training时μβ和σβ由当前batch计算得出;在testing时μβ和σβ应使用training时保存的均值或类似的经过处理的值,而不是由当前batch计算.
2、tensorflow相关函数 。
1.tf.nn.moments(x, axes, shift=none, name=none, keep_dims=false) 。
x是输入张量,axes是在哪个维度上求解, 即想要 normalize的维度, [0] 代表 batch 维度,如果是图像数据,可以传入 [0, 1, 2],相当于求[batch, height, width] 的均值/方差,注意不要加入channel 维度。该函数返回两个张量,均值mean和方差variance.
2.tf.identity(input, name=none) 。
返回与输入张量input形状和内容一致的张量.
3.tf.nn.batch_normalization(x, mean, variance, offset, scale, variance_epsilon,name=none) 。
计算公式为scale(x - mean)/ variance + offset.
这些参数中,tf.nn.moments可得到均值mean和方差variance,offset和scale是可训练的,offset一般初始化为0,scale初始化为1,offset和scale的shape与mean相同,variance_epsilon参数设为一个很小的值如0.001.
3、tensorflow代码实现 。
1. 完整代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
import
tensorflow as tf
import
numpy as np
import
matplotlib.pyplot as plt
activition
=
tf.nn.relu
n_layers
=
7
# 总共7层隐藏层
n_hidden_units
=
30
# 每层包含30个神经元
def
fix_seed(seed
=
1
):
# 设置随机数种子
np.random.seed(seed)
tf.set_random_seed(seed)
def
plot_his(inputs, inputs_norm):
# 绘制直方图函数
for
j, all_inputs
in
enumerate
([inputs, inputs_norm]):
for
i,
input
in
enumerate
(all_inputs):
plt.subplot(
2
,
len
(all_inputs), j
*
len
(all_inputs)
+
(i
+
1
))
plt.cla()
if
i
=
=
0
:
the_range
=
(
-
7
,
10
)
else
:
the_range
=
(
-
1
,
1
)
plt.hist(
input
.ravel(), bins
=
15
,
range
=
the_range, color
=
'#ff5733'
)
plt.yticks(())
if
j
=
=
1
:
plt.xticks(the_range)
else
:
plt.xticks(())
ax
=
plt.gca()
ax.spines[
'right'
].set_color(
'none'
)
ax.spines[
'top'
].set_color(
'none'
)
plt.title(
"%s normalizing"
%
(
"without"
if
j
=
=
0
else
"with"
))
plt.draw()
plt.pause(
0.01
)
def
built_net(xs, ys, norm):
# 搭建网络函数
# 添加层
def
add_layer(inputs, in_size, out_size, activation_function
=
none, norm
=
false):
weights
=
tf.variable(tf.random_normal([in_size, out_size],
mean
=
0.0
, stddev
=
1.0
))
biases
=
tf.variable(tf.zeros([
1
, out_size])
+
0.1
)
wx_plus_b
=
tf.matmul(inputs, weights)
+
biases
if
norm:
# 判断是否是batch normalization层
# 计算均值和方差,axes参数0表示batch维度
fc_mean, fc_var
=
tf.nn.moments(wx_plus_b, axes
=
[
0
])
scale
=
tf.variable(tf.ones([out_size]))
shift
=
tf.variable(tf.zeros([out_size]))
epsilon
=
0.001
# 定义滑动平均模型对象
ema
=
tf.train.exponentialmovingaverage(decay
=
0.5
)
def
mean_var_with_update():
ema_apply_op
=
ema.
apply
([fc_mean, fc_var])
with tf.control_dependencies([ema_apply_op]):
return
tf.identity(fc_mean), tf.identity(fc_var)
mean, var
=
mean_var_with_update()
wx_plus_b
=
tf.nn.batch_normalization(wx_plus_b, mean, var,
shift, scale, epsilon)
if
activation_function
is
none:
outputs
=
wx_plus_b
else
:
outputs
=
activation_function(wx_plus_b)
return
outputs
fix_seed(
1
)
if
norm:
# 为第一层进行bn
fc_mean, fc_var
=
tf.nn.moments(xs, axes
=
[
0
])
scale
=
tf.variable(tf.ones([
1
]))
shift
=
tf.variable(tf.zeros([
1
]))
epsilon
=
0.001
ema
=
tf.train.exponentialmovingaverage(decay
=
0.5
)
def
mean_var_with_update():
ema_apply_op
=
ema.
apply
([fc_mean, fc_var])
with tf.control_dependencies([ema_apply_op]):
return
tf.identity(fc_mean), tf.identity(fc_var)
mean, var
=
mean_var_with_update()
xs
=
tf.nn.batch_normalization(xs, mean, var, shift, scale, epsilon)
layers_inputs
=
[xs]
# 记录每一层的输入
for
l_n
in
range
(n_layers):
# 依次添加7层
layer_input
=
layers_inputs[l_n]
in_size
=
layers_inputs[l_n].get_shape()[
1
].value
output
=
add_layer(layer_input, in_size, n_hidden_units, activition, norm)
layers_inputs.append(output)
prediction
=
add_layer(layers_inputs[
-
1
],
30
,
1
, activation_function
=
none)
cost
=
tf.reduce_mean(tf.reduce_sum(tf.square(ys
-
prediction),
reduction_indices
=
[
1
]))
train_op
=
tf.train.gradientdescentoptimizer(
0.001
).minimize(cost)
return
[train_op, cost, layers_inputs]
fix_seed(
1
)
x_data
=
np.linspace(
-
7
,
10
,
2500
)[:, np.newaxis]
np.random.shuffle(x_data)
noise
=
np.random.normal(
0
,
8
, x_data.shape)
y_data
=
np.square(x_data)
-
5
+
noise
plt.scatter(x_data, y_data)
plt.show()
xs
=
tf.placeholder(tf.float32, [none,
1
])
ys
=
tf.placeholder(tf.float32, [none,
1
])
train_op, cost, layers_inputs
=
built_net(xs, ys, norm
=
false)
train_op_norm, cost_norm, layers_inputs_norm
=
built_net(xs, ys, norm
=
true)
with tf.session() as sess:
sess.run(tf.global_variables_initializer())
cost_his
=
[]
cost_his_norm
=
[]
record_step
=
5
plt.ion()
plt.figure(figsize
=
(
7
,
3
))
for
i
in
range
(
250
):
if
i
%
50
=
=
0
:
all_inputs, all_inputs_norm
=
sess.run([layers_inputs, layers_inputs_norm],
feed_dict
=
{xs: x_data, ys: y_data})
plot_his(all_inputs, all_inputs_norm)
sess.run([train_op, train_op_norm],
feed_dict
=
{xs: x_data[i
*
10
:i
*
10
+
10
], ys: y_data[i
*
10
:i
*
10
+
10
]})
if
i
%
record_step
=
=
0
:
cost_his.append(sess.run(cost, feed_dict
=
{xs: x_data, ys: y_data}))
cost_his_norm.append(sess.run(cost_norm,
feed_dict
=
{xs: x_data, ys: y_data}))
plt.ioff()
plt.figure()
plt.plot(np.arange(
len
(cost_his))
*
record_step,
np.array(cost_his), label
=
'without bn'
)
# no norm
plt.plot(np.arange(
len
(cost_his))
*
record_step,
np.array(cost_his_norm), label
=
'with bn'
)
# norm
plt.legend()
plt.show()
|
2. 实验结果 。
输入数据分布:
批标准化bn效果对比:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://blog.csdn.net/marsjhao/article/details/72876460 。
最后此篇关于TensorFlow实现Batch Normalization的文章就讲到这里了,如果你想了解更多关于TensorFlow实现Batch Normalization的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
这两种方法似乎都完成了从字符串中删除重音符号的相同工作,但我不确定是否存在一些内部差异,可以使一种方法优于另一种方法 最佳答案 分解处理的不仅仅是重音(在 Unicode 中称为标记),例如韩文音节
我在尝试绘制一些数据时收到此错误: findfont: Font family ['STIXGeneral'] not found. Falling back to DejaVu Sans. Trac
我理解规范化和规范化意味着删除数据表示中任何无意义或模棱两可的部分,将有效相同的数据转换为实际相同的数据。 例如,如果您想获取某些输入数据的哈希值,并且其他任何人对规范的相同数据进行哈希处理都获得相同
#!/usr/local/bin/perl use warnings; use 5.014; use Unicode::Normalize qw(NFD NFC compose); my $strin
我正在尝试将包含“áéíóú”等字符的字符串规范化为“aeiou”以简化搜索。 在对 this question 的回复之后我应该使用 Normalizer 类来完成它。 问题是 normalize
我正在尝试获取外语文本并输出人类可读、文件名安全的等效文本。环顾四周后,似乎最好的选择是 unicodedata.normalize(),但我无法让它工作。我试过将一些答案的确切代码放在这里和其他地方
我是 pymc 和贝叶斯统计的新手。在这里,我试图实现一个极其简单的 pymc 模型,以便与理论结果进行比较。在我的测试用例中,我假设正常先验为 mu~N(20,20) 并且可能性假设为 data~N
我正在编写一个 XPath 表达式,但我修复了一个奇怪的错误,但是以下两个 XPath 表达式之间有什么区别? "//td[starts-with(normalize-space()),'Posted
tf.random.normal 和 tf.distributions.Normal 有什么区别?或者 tf.distributions.Multinomial 和 tf.random.multino
第一步: xcodebuild -project Pods/Pods.xcodeproj build 第二个: xcodebuild archive -project 'test.xcodeproj'
我正在将 Adobe Xd 中的设计转换为 HTML 模板,其中一个元素建议使用以下 css 规则。 top: 149px; left: 54px; width: 463px; height: 2
我正在与另一位数据库设计人员就规范化进行有趣的讨论。在这个例子中,我们有一个 GameTitles 表,每条记录都必须包含游戏发布的年份。他说 2NF 要求所有内容都必须规范化,因此,要符合要求,年份
我正在尝试在 python 中加载文件。如果您运行下面的代码并加载仅包含英文单词的文件,它将加载得很好。 Listado.txt 是一个西类牙语文件,包含以下单词:abacá、abadí、abadía
我一个月前完成了这个游戏,直到今天一切都很好。我在线收到错误: odgovorNormalized = Normalizer.normalize(konResenje, Normalizer.Form
我正在使用gnutls aes加密,这是我的代码 const gnutls_datum_t * key; *key = (const char*)"tZOVP7o/u2vQB+4B/0O0ZqLWvy
我正在尝试训练一个用于面部性别和年龄检测的 cnn 模型。我的训练集包含彩色和灰度的面部图像。我如何标准化这个数据集?或者如何处理混合了灰度和彩色图像的数据集? 最佳答案 请记住,网络只会尝试了解您的
这是 DB 规范化理论中的一个概念: Third normal form is violated when a non-key field is a fact about another non-ke
我正在存储有关棒球统计数据的数据,并希望使用三个表来存储:players、battingStats 和 pitchingStats。就问题而言,每个球员都会有击球统计数据或投球统计数据,但不会两者都有
我现在正在学习推力。我有一个问题:如何用推力进行归一化? 我有一个有效的代码,但我想知道这是否是最佳方法。 struct square { __host__ __device__ float oper
所以我知道我自己可以如何解决这个问题,但是有人知道任何可以处理这个问题的现有库吗? (无论什么语言)? 最佳答案 此 API 仅支持英文,并具有职称规范化:http://api.dataatwork.
我是一名优秀的程序员,十分优秀!