简体   繁体   中英

Issue in creating Keras Model Input tensors to a Model must come from `keras.layers.Input`?

for some reason I am trying to create my Keras model but it won't work. I get this error ValueError: Input tensors to a Model must come from keras.layers.Input . Received: (missing previous layer metadata). [Error when creating the model last line]

I tried separating the inputs but it didn't work, any help please? Here's a snippet of my code

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])

The model only accepts Input s. You can't pass embeddings to the inputs of a model.

  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])]

Sounds like this:

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


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

you need to convert yours embeddings(either from keras or any other external model like Glove, Bert) into keras inputs like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM