如何将预训练的Keras模型与批处理归一化层一起使用?

时间:2018-06-28 10:20:24

标签: tensorflow keras

我有一个带有batch_normalization模型的预训练模型。当我跑步时:

model.layers.get_weights

我看到batch_normalization层中存在beta / gama值,这意味着模型已经过训练,并且该值具有含义。

我想加载模型并在tensorflow中使用它。当我跑步时:

sess.run(tf.report_uninitialized_variables(tf.global_variables()))

它为我提供了来自batch_normalization层的变量: unitialized_variable

array(['pretrain_variable/pretrain_variable/batch_normalization_11/moving_mean/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_11/moving_mean/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_11/moving_variance/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_11/moving_variance/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_15/moving_mean/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_15/moving_mean/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_15/moving_variance/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_15/moving_variance/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_9/moving_mean/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_9/moving_mean/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_9/moving_variance/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_9/moving_variance/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_13/moving_mean/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_13/moving_mean/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_13/moving_variance/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_13/moving_variance/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_16/moving_mean/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_16/moving_mean/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_16/moving_variance/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_16/moving_variance/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_14/moving_mean/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_14/moving_mean/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_14/moving_variance/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_14/moving_variance/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_10/moving_mean/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_10/moving_mean/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_10/moving_variance/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_10/moving_variance/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_12/moving_mean/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_12/moving_mean/local_step_1',
       'pretrain_variable/pretrain_variable/batch_normalization_12/moving_variance/biased_1',
       'pretrain_variable/pretrain_variable/batch_normalization_12/moving_variance/local_step_1',
       'train_variable/output_y_0/kernel',
       'train_variable/output_y_0/bias',
       'train_variable/output_y_1/kernel',
       'train_variable/output_y_1/bias',
       'train_variable/output_y_2/kernel',
       'train_variable/output_y_2/bias',
       'train_variable/output_y_3/kernel',
       'train_variable/output_y_3/bias',
       'train_variable/output_y_4/kernel',
       'train_variable/output_y_4/bias',
       'train_variable/output_y_5/kernel',
       'train_variable/output_y_5/bias',
       'train_variable/output_y_6/kernel',
       'train_variable/output_y_6/bias',
       'train_variable/output_y_7/kernel',
       'train_variable/output_y_7/bias',
       'train_variable/output_y_8/kernel',
       'train_variable/output_y_8/bias',
       'train_variable/output_y_9/kernel',
       'train_variable/output_y_9/bias',
       'train_variable/output_y_10/kernel',
       'train_variable/output_y_10/bias',
       'train_variable/output_y_11/kernel',
       'train_variable/output_y_11/bias', 'train_variable/beta1_power',
       'train_variable/beta2_power',
       'train_variable/train_variable/output_y_0/kernel/Adam',
       'train_variable/train_variable/output_y_0/kernel/Adam_1',
       'train_variable/train_variable/output_y_0/bias/Adam',
       'train_variable/train_variable/output_y_0/bias/Adam_1',
       'train_variable/train_variable/output_y_1/kernel/Adam',
       'train_variable/train_variable/output_y_1/kernel/Adam_1',
       'train_variable/train_variable/output_y_1/bias/Adam',
       'train_variable/train_variable/output_y_1/bias/Adam_1',
       'train_variable/train_variable/output_y_2/kernel/Adam',
       'train_variable/train_variable/output_y_2/kernel/Adam_1',
       'train_variable/train_variable/output_y_2/bias/Adam',
       'train_variable/train_variable/output_y_2/bias/Adam_1',
       'train_variable/train_variable/output_y_3/kernel/Adam',
       'train_variable/train_variable/output_y_3/kernel/Adam_1',
       'train_variable/train_variable/output_y_3/bias/Adam',
       'train_variable/train_variable/output_y_3/bias/Adam_1',
       'train_variable/train_variable/output_y_4/kernel/Adam',
       'train_variable/train_variable/output_y_4/kernel/Adam_1',
       'train_variable/train_variable/output_y_4/bias/Adam',
       'train_variable/train_variable/output_y_4/bias/Adam_1',
       'train_variable/train_variable/output_y_5/kernel/Adam',
       'train_variable/train_variable/output_y_5/kernel/Adam_1',
       'train_variable/train_variable/output_y_5/bias/Adam',
       'train_variable/train_variable/output_y_5/bias/Adam_1',
       'train_variable/train_variable/output_y_6/kernel/Adam',
       'train_variable/train_variable/output_y_6/kernel/Adam_1',
       'train_variable/train_variable/output_y_6/bias/Adam',
       'train_variable/train_variable/output_y_6/bias/Adam_1',
       'train_variable/train_variable/output_y_7/kernel/Adam',
       'train_variable/train_variable/output_y_7/kernel/Adam_1',
       'train_variable/train_variable/output_y_7/bias/Adam',
       'train_variable/train_variable/output_y_7/bias/Adam_1',
       'train_variable/train_variable/output_y_8/kernel/Adam',
       'train_variable/train_variable/output_y_8/kernel/Adam_1',
       'train_variable/train_variable/output_y_8/bias/Adam',
       'train_variable/train_variable/output_y_8/bias/Adam_1',
       'train_variable/train_variable/output_y_9/kernel/Adam',
       'train_variable/train_variable/output_y_9/kernel/Adam_1',
       'train_variable/train_variable/output_y_9/bias/Adam',
       'train_variable/train_variable/output_y_9/bias/Adam_1',
       'train_variable/train_variable/output_y_10/kernel/Adam',
       'train_variable/train_variable/output_y_10/kernel/Adam_1',
       'train_variable/train_variable/output_y_10/bias/Adam',
       'train_variable/train_variable/output_y_10/bias/Adam_1',
       'train_variable/train_variable/output_y_11/kernel/Adam',
       'train_variable/train_variable/output_y_11/kernel/Adam_1',
       'train_variable/train_variable/output_y_11/bias/Adam',
       'train_variable/train_variable/output_y_11/bias/Adam_1'],
      dtype=object)

我必须运行此代码才能使用该模型。我担心这会破坏batch_normalization层中的参数。但是,我验证了参数保持不变。 所以,我的问题是,为什么批处理标准化层中的参数会被报告为未初始化,而在sess.run(tf.variables_initializer)之后保持不变?

1 个答案:

答案 0 :(得分:1)

在训练模型之前,将存在beta,gamma值,因为可以初始化这些参数。默认情况下,伽玛将初始化为1,贝塔为0。