【问题标题】:Keras: how to use dropout at train and test phase?Keras:如何在训练和测试阶段使用 dropout?
【发布时间】:2019-09-06 20:43:35
【问题描述】:

是否可以在 Keras 的训练和测试阶段使用 dropout?

就像这里描述的: https://github.com/soumith/ganhacks#17-use-dropouts-in-g-in-both-train-and-test-phase

【问题讨论】:

    标签: machine-learning keras dropout


    【解决方案1】:

    当然,您可以在调用Dropout 层时将training 参数设置为True。这样,dropout 将应用于训练和测试阶段:

    drp_output = Dropout(rate)(inputs, training=True)  # dropout would be active in train and test phases
    

    【讨论】:

    • 在 Keras 的 GaussianNoise 层中也可以这样做吗?
    • @david 看起来在 Keras 中你可以使用 keras.backend.set_learning_phase(1) keras.io/backend/#set_learning_phase
    • 但是我想知道在Tensorflow中如果你在Dropout层设置training=True,它会在graph转换为frozen graph或转换为coreml/tflite后保持吗?
    • @mrgloom 我想是的,但我不确定。为什么认为情况可能并非如此?
    • 因为冻结图用于推理而不是训练,所以我不确定我们是否可以在其中切换训练/测试阶段。
    【解决方案2】:

    这两个答案都让我有些困惑。更简单地说,您可能会发现自己在做这样的事情:

    model = Model(...)
    ...
    model.add(Dropout(0.5))
    ...
    model.fit(...)        # invokes Dropout(training=True)
    ...
    model.evaluate(...)   # invokes Dropout(training=False)
    

    也就是说,当你定义你的模型时,你添加了Dropout层和训练期间你想要的退出率。测试和培训之间的比率没有明显变化;相反,它被声明为一个固定值,然后(不可见地)根据调用层的training 参数打开/关闭。见keras.Model

    【讨论】:

    • 立即忏悔:这并没有回答我直到现在才误读的原始问题。但是在试图了解如何使用Dropout 时,这个问题排名很高。该问题询问有关做一些不寻常的事情 - 将辍学应用于测试和培训。这个答案说明了仅将 dropout 应用于训练的更正常情况。
    猜你喜欢
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2018-02-12
    • 2016-10-06
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多