【问题标题】:Keras error "You must feed a value for placeholder tensor 'bidirectional_1/keras_learning_phase' with dtype bool"Keras 错误“您必须使用 dtype bool 为占位符张量‘bidirectional_1/keras_learning_phase’提供一个值”
【发布时间】:2017-08-15 15:18:01
【问题描述】:

下面的代码 sn-p 出现以下错误:

您必须为占位符张量提供一个值 'bidirectional_1/keras_learning_phase' 与 dtype bool

如果我添加 dropout 层 model.add(Dropout(dropout)),它可以工作。有谁知道为什么?后端是Tensorflow,Keras 2.0.1

def prep_model1(embedding_layer1, embedding_layer2, dropout=0.5):

    model0 = Sequential()  
    model0.add(embedding_layer1)
    model0.add(Bidirectional(LSTM(128, return_sequences=False, dropout=dropout)))

    model1 = Sequential() 
    model1.add(embedding_layer2)
    model1.add(Bidirectional(LSTM(128, return_sequences=False, dropout=dropout)))

    model = Sequential()
    model.add(Merge([model0, model1], mode='concat', concat_axis=1))
    #model.add(Dropout(dropout))
    model.add(Dense(1, activation='sigmoid'))

    return model

【问题讨论】:

    标签: tensorflow deep-learning keras keras-layer


    【解决方案1】:

    尝试导入 K 并在模型之前设置学习阶段。

    from keras import backend as K
    
    K.set_learning_phase(1) #set learning phase
    

    来自this issue

    【讨论】:

    • 重要:在构建模型之前使用它。也适用于 BatchNorm。
    • 声明here“学习阶段”是一个标志,是一个指示训练/推理的标志。使用时设置为 1,例如fit 并在使用时为 0,例如predictK.set_learning_phase(False) 将“学习阶段”设置为始终为 0,即 fit 将使模型在推理模式下运行(例如,没有 dropout 和 BatchNorm 行为设置为推理)。
    猜你喜欢
    • 2018-10-23
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 2017-11-02
    • 2017-06-10
    • 2018-12-29
    • 2019-01-23
    相关资源
    最近更新 更多