keras - 有没有办法在 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`
尽管如此,这些都没有奏效。
解决方案
您可以按照此链接创建自定义层,还有一个使用自定义 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
推荐阅读
- azure - Microsoft 多租户应用程序作为个人开发人员 - Azure Active Directory
- java - 如何在 NetBeans 8.2 中添加外部库?
- r - 如何创建按PERMNO分组并在R中按日期排列的累积变量
- python - 我正在尝试从我的用户那里获取:first_name 和 last_name,并注意到在我的 html 代码中正在改进
- android - 为什么我的位图图像出现在警报对话框后面?
- ios - 根据用户选择从数据模型中列出和过滤 - SwiftUI
- excel - 如何从 Excel 中的文本字符串中提取特定数字?
- swift - UITableViewCell 内的 UICollectionView 缓慢滚动
- .net-core - 依赖注入 - .NET Core Windows 窗体
- compilation - 用 openmpi-4.0.5 编译的 Quantum esspresso-6.7.0 问题