首页 > 解决方案 > 有没有办法在 Keras 的推理过程中激活 dropout,同时冻结批处理规范层

问题描述

我试图在推理期间使用模型中的 dropout 层来测量模型不确定性,如Yurin Gal 概述的方法中所述

这篇文章中描述了一个解决方案: 如何使用 Keras 计算预测不确定性?,它定义了一个新的 Keras 函数 self.f = K.function([self.graph.layers[0].input, K.learning_phase()], [self.graph.layers[-1].output])

但是,如果使用的模型具有批量标准化层,则此方法不成立。因为这将使模型不使用在训练期间学习的均值和方差,而是根据当前批次设置新的。

因此,我正在寻找一种方法将批处理层训练参数设置为 false,但将 dropout 层保持在训练模式?

我使用 KerasefficientNet B0 作为模型,在自定义数据keras_efficientNet上进行了训练

我已经尝试自己更改图层设置

`
        for layer in self.graph.layers[4].layers:
            if 'batch_norm' in layer.name:
                layer._trainable = False
                layer._inbound_nodes[0].output_tensors[0]._uses_learning_phase = False
                layer._inbound_nodes[0].input_tensors[0]._uses_learning_phase = False
            if 'dropout' in layer.name:
                layer._inbound_nodes[0].output_tensors[0]._uses_learning_phase = True
            for weight in self.graph.layers[4].weights:
                if 'batch_norm' in weight.name:
                    weight._trainable = False`

尽管如此,这些都没有奏效。

标签: kerasdeep-learninguncertainty

解决方案


您可以按照此链接创建自定义层,还有一个使用自定义 dropout 层的示例,以便您可以在训练期间操纵训练值以及推理时间以使 dropout 在推理时间运行

class CustomDropout(keras.layers.Layer):
def __init__(self, rate, **kwargs):
    super(CustomDropout, self).__init__(**kwargs)
    self.rate = rate

def call(self, inputs, training=None):
    if training: #you can remove this line,, so that you can use dropout on inference time
        return tf.nn.dropout(inputs, rate=self.rate)
    return inputs

推荐阅读