在Tensorflow中,我会有一个placeholder
,以便我可以根据需要将其提供给网络:
self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
# ...
# Add dropout
with tf.name_scope("dropout"):
self.h_drop = tf.nn.dropout(self.h_pool_flat, self.dropout_keep_prob)
但是,我不确定如何在Keras中这样做:
# in_dropout = tf.placeholder(tf.float32, name="dropout_keep_prob")
in_dropout = Input(shape=(1,), name='dropout')
# ..
# Add droppout
droput = Dropout(in_dropout)(max_pool) # This does not work of course
答案 0 :(得分:1)
在keras Dropout
层中,在训练和测试阶段表现不同,这只是在训练阶段才启用。
要在训练/测试阶段使用dropout,您必须使用keras后端的Lambda
函数将Dropout图层替换为dropout
图层。
from keras.layers.core import Lambda
from keras import backend as K
model.add(Lambda(lambda x: K.dropout(x, level=0.5)))
有关更多参考检查:here。