本文介绍了Keras错误“您必须使用dtype bool输入占位符张量'bidirectional_1/keras_learning_phase'的值"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下代码段,我收到以下错误:

I get the following error for the code snippet below:

如果我添加辍学层model.add(Dropout(dropout)),它将起作用.有人知道为什么吗?后端是Tensorflow Keras 2.0.1

If I add the dropout layer model.add(Dropout(dropout)), it works. Anyone knows why? The back-end is 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

推荐答案

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

Try to import K and set learning phase before your model.

from keras import backend as K

K.set_learning_phase(1) #set learning phase

来自此问题

这篇关于Keras错误“您必须使用dtype bool输入占位符张量'bidirectional_1/keras_learning_phase'的值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 03:01