Keras官方文档:
A Keras model has two modes: training and testing. Regularization mechanisms, such as Dropout and L1/L2 weight regularization, are turned off at testing time.
Keras模型有两种模式:训练和测试。 在测试时关闭正常化机制,例如Dropout和L1 / L2权重正则化。
所以我们需要将模式转为训练模式,在测试时才能使用dropout!!!
在官方文档中
keras.backend.learning_phase()
Returns the learning phase flag.
The learning phase flag is a bool tensor (0 = test, 1 = train) to be passed as input to any Keras function that uses a different behavior at train time and test time.
通过设置learning_phase来控制模式
import keras
keras.backend.set_learning_phase(1)