在我们使用 tf.keras.layers.Conv2D 来构建卷积层时,一般使用的权值初始化方法就是这个函数默认的方法,即 ‘glorot_uniform’ 。 源码对其做出了解释:
'''
It draws samples from a uniform distribution within [-limit, limit]
where `limit` is `sqrt(6 / (fan_in + fan_out))`
where `fan_in` is the number of input units in the weight tensor
and `fan_out` is the number of output units in the weight tensor.
'''
也就是说,这个初始化方法使用的均匀分布的范围是 [-limit, limit] ,其中,
l
i
m
i
t
=
s
q
r
t
(
6
f
a
n
i
n
+
f
a
n
o
u
t
)
limit=sqrt(\frac{6}{fan_{in} + fan_{out}})
l i m i t = s q r t ( f a n i n + f a n o u t 6 ) 这里的
f
a
n
i
n
fan_{in}
f a n i n 表示输入神经元的数量,
f
a
n
o
u
t
fan_{out}
f a n o u t 表示输出神经元的数量,即:
f
a
n
i
n
=
c
h
a
n
n
e
l
s
i
n
×
k
e
r
n
e
r
w
i
d
t
h
×
k
e
r
n
e
r
h
e
i
g
h
t
fan_{in}=channels_{in}\times kerner_{width}\times kerner_{height}
f a n i n = c h a n n e l s i n × k e r n e r w i d t h × k e r n e r h e i g h t
f
a
n
o
u
t
=
c
h
a
n
n
e
l
s
o
u
t
×
k
e
r
n
e
r
w
i
d
t
h
×
k
e
r
n
e
r
h
e
i
g
h
t
fan_{out}=channels_{out}\times kerner_{width}\times kerner_{height}
f a n o u t = c h a n n e l s o u t × k e r n e r w i d t h × k e r n e r h e i g h t 假设输入网络的一个 28*28*1 的数据,卷积核形状为 3*3 ,卷积核通道数有 32 个(即输出的通道数有 32 个),那么此时 limit 为:
l
i
m
i
t
=
s
q
r
t
(
6
3
×
3
×
1
+
3
×
3
×
32
)
limit=sqrt(\frac{6}{3\times 3\times 1 + 3\times 3\times 32})
l i m i t = s q r t ( 3 × 3 × 1 + 3 × 3 × 3 2 6 )