簡體   English   中英

為模型創建 Keras 模型輸入張量的問題必須來自`keras.layers.Input`?

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

出於某種原因,我正在嘗試創建我的 Keras 模型,但它不起作用。 我收到此錯誤 ValueError: Input keras.layers.Input to a Model must come from 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])

該模型只接受Input s。 您不能將嵌入傳遞給模型的輸入。

  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)

您需要將您的嵌入(來自 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM