我试图在推理期间使用模型中的 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`
尽管如此,这些都没有奏效。