在为模型创建Keras模型输入张量时,问题必须来自“ keras.layers.Input”?

时间:2019-02-11 17:26:21

标签: python tensorflow keras nlp

由于某种原因,我试图创建我的Keras模型,但无法正常工作。我收到此错误ValueError:模型的输入张量必须来自keras.layers.Input。收到:(缺少上一层元数据)。 [创建模型最后一行时出错]

我尝试分离输入,但是没有用,请帮忙吗?这是我的代码段

word_embedding_layer = emb.get_keras_embedding(trainable = True,
                                            input_length = 20, 
                                            name='word_embedding_layer') 


pos_embedding_layer = Embedding(output_dim = 5,
                         input_dim = 56,
                         input_length = 20,
                         name='pos_embedding_layer')





 inputs_and_embeddings = [(Input(shape = (sent_maxlen,),
                                            dtype="int32",
                                            name = "word_inputs"),
                                      word_embedding_layer),
                                     (Input(shape = (sent_maxlen,),
                                            dtype="int32",
                                            name = "predicate_inputs"),
                                      word_embedding_layer),
                                     (Input(shape = (sent_maxlen,),
                                            dtype="int32",
                                            name = "postags_inputs"),
                                      pos_embedding_layer),
            ]




## --------> 9] Concat all inputs and run on deep network
        ## Concat all inputs and run on deep network

outputI = predict_layer(dropout(latent_layers(keras.layers.concatenate([embed(inp)
                                                            for inp, embed in inputs_and_embeddings],
                                                       axis = -1))))


## --------> 10]Build model 
model = Model( map(itemgetter(0), inputs_and_embeddings),[outputI])

2 个答案:

答案 0 :(得分:3)

该模型仅接受Input个。您不能将嵌入传递到模型的输入。

  inputs = [Input(sent_maxlen,), dtype='int32', name='word_inputs'),
            Input(sent_maxlen,), dtype='int32', name='predicate_inputs')
            Input(sent_maxlen,), dtype='int32', name='postags_inputs')]

  embeddings = [word_embedding_layer(inputs[0]), 
                word_embedding_layer(inputs[1]),
                pos_embedding_layer(inputs[2])]

像这样的声音

outputI = predict_layer(dropout(latent_layers(keras.layers.concatenate(embeddings))))


## --------> 10]Build model 
model = Model(inputs, outputI)

答案 1 :(得分:0)

您需要将您的嵌入(来自于keras或其他任何外部模型,例如Glove,Bert)转换为像这样的keras输入

headline_embeddings = model.encode(headlines) #from bert
snippets_embeddings = model.encode(snippets)#from bert
h_embeddings = np.asarray(snippets_embeddings) #into numpy format
s_embeddings = np.asarray(headline_embeddings)
headline = Input(name = 'h_embeddings', shape = [1]) #converting into keras inputs
snippet = Input(name = 's_embeddings', shape = [1])
model = Model(inputs = ([headline, snippet]), outputs = merged) #keras model input