在 keras 中进行预测时如何禁用 dropout? [英] How to disable dropout while prediction in keras?

查看:131
本文介绍了在 keras 中进行预测时如何禁用 dropout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 keras 的神经网络模型中使用 dropout.一点点代码就像

I am using dropout in neural network model in keras. Little bit code is like

model.add(Dropout(0.5))
model.add(Dense(classes))

为了测试,我使用 preds = model_1.predict_proba(image).

但是在测试时Dropout 也参与预测不应该发生的分数.我搜索了很多来禁用辍学,但还没有得到任何提示.

But while testing Dropout is also participating to predict the score which should not be happen. I search a lot to disable the dropout but didn't get any hint yet.

有没有人在 keras 中测试时禁用 Dropout 的解决方案??

Do anyone have solution to disable the Dropout while testing in keras??

推荐答案

Keras 默认这样做.在 Keras dropout 在测试模式下被禁用.你可以在这里查看代码并看到他们在训练中使用丢弃的输入,在测试时使用实际输入.

Keras does this by default. In Keras dropout is disabled in test mode. You can look at the code here and see that they use the dropped input in training and the actual input while testing.

据我所知,您必须从层构建自己的训练函数并指定训练标志以使用 dropout 进行预测(例如,无法为预测函数指定训练标志).由于生成的训练图像和生成的测试图像之间存在差异,如果您想做 GAN,它会使用中间输出进行训练并作为一个整体来训练网络,这是一个问题.

As far as I know you have to build your own training function from the layers and specify the training flag to predict with dropout (e.g. its not possible to specify a training flag for the predict functions). This is a problem in case you want to do GANs, which use the intermediate output for training and also train the network as a whole, due to a divergence between generated training images and generated test images.

这篇关于在 keras 中进行预测时如何禁用 dropout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆