【问题标题】:How to use MC Dropout on a variational dropout LSTM layer on keras?如何在 keras 上的变分 dropout LSTM 层上使用 MC Dropout?
【发布时间】:2023-12-07 02:01:02
【问题描述】:

我目前正在尝试使用 Keras(张量流后端)建立一个(LSTM)循环神经网络。 我想使用带有 MC Dropout 的变分 dropout。 我相信已经使用 LSTM 层的“recurrent_dropout”选项实现了变分 dropout,但我没有找到任何方法来设置“training”标志以像经典的 Dropout 层那样设置为 true。

【问题讨论】:

    标签: keras lstm keras-layer dropout


    【解决方案1】:

    这在 Keras 中很容易,首先你需要定义一个函数,它接受模型输入和learning_phase

    import keras.backend as K
    f = K.function([model.layers[0].input, K.learning_phase()],
                   [model.layers[-1].output])
    

    对于具有多个输入/输出的功能 API 模型,您可以使用:

    f = K.function([model.inputs, K.learning_phase()],
                   [model.outputs])
    

    然后你可以像f([input, 1])这样调用这个函数,这将告诉Keras在这个调用期间启用学习阶段,执行Dropout。然后你可以多次调用这个函数并结合预测来估计不确定性。

    【讨论】:

    • 感谢您的回答。这是 Yarin Gal 用于 BayesianRNN 实现的工作。它是否像以下示例一样工作?我运行了它,它似乎可以工作,但是 keras 文档对此很模糊......示例:输入 = keras.Input(shape=(10)) x = keras.layers.LSTM(10,recurrent_dropout=0.5)(inputs,训练=真)
    【解决方案2】:

    “Dropout as a Bayesian Approximation: Representing Model Uncertainty in Deep Learning”(2015)的源代码位于https://github.com/yaringal/DropoutUncertaintyExps/blob/master/net/net.py。他们也使用 Keras,代码很容易理解。 Dropout 层在没有 Sequential api 的情况下使用,以便传递训练参数。这是对 Matias 建议的不同处理方式:

    inter = Dropout(dropout_rate)(inter, training=True)
    

    【讨论】:

      最近更新 更多