<p>Keras内部调用提供的<code>initializer</code>,如下所示</p>
<pre><code>weight = K.variable(initializer(shape, dtype=dtype), dtype=dtype, ......
</code></pre>
<p>如您所见,自定义<code>initializer</code>的第二个参数应该是<code>dtype</code>而不是<code>name</code></p>
<p><strong>修复</strong></p>
<pre><code>def initialize_weights(shape, dtype=None):
return np.random.normal(loc = 0.0, scale = 1e-2, size = shape)
def initialize_bias(shape, dtype=None):
return np.random.normal(loc = 0.5, scale = 1e-2, size = shape)
</code></pre>
<p>现在</p>
<pre><code>model = get_siamese_model((105, 105, 1))
model.summary()
</code></pre>
<p>将成功地构建模型</p>
<p>输出</p>
<pre><code>__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_7 (InputLayer) (None, 105, 105, 1) 0
__________________________________________________________________________________________________
input_8 (InputLayer) (None, 105, 105, 1) 0
__________________________________________________________________________________________________
sequential_4 (Sequential) (None, 4096) 38947648 input_7[0][0]
input_8[0][0]
__________________________________________________________________________________________________
lambda_2 (Lambda) (None, 4096) 0 sequential_4[1][0]
sequential_4[2][0]
__________________________________________________________________________________________________
dense_4 (Dense) (None, 1) 4097 lambda_2[0][0]
==================================================================================================
Total params: 38,951,745
Trainable params: 38,951,745
Non-trainable params: 0
_________________________
</code></pre>