简体   繁体   English

为模型创建 Keras 模型输入张量的问题必须来自`keras.layers.Input`?

[英]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.出于某种原因,我正在尝试创建我的 Keras 模型,但它不起作用。 I get this error ValueError: Input tensors to a Model must come from keras.layers.Input .我收到此错误 ValueError: Input keras.layers.Input 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.该模型只接受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您需要将您的嵌入(来自 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.

相关问题 Keras - TypeError:模型的输出张量必须是Keras张量 - 同时建模多输入,多输出网络 - Keras - TypeError: Output tensors to a Model must be Keras tensors - while modelling multiple input , multiple output network Keras 模型输入:3 个数组与 3 个张量的元组不一样? - Keras model input: 3 arrays is not the same as a tuple of 3 tensors? 模型输入必须来自 `tf.keras.Input` ...,它们不能是前一个非输入层的输出 - Model inputs must come from `tf.keras.Input` …, they cannot be the output of a previous non-Input layer TypeError:模型的输出张量必须是Keras张量 - TypeError: Output tensors to a Model must be Keras tensors 模型的输出张量必须是Keras张量 - Output tensors to a Model must be Keras tensors keras.Model 中输入的附加维度从何而来? - where does the additional dimension of the Input in a keras.Model come from? 在带有 Tensorflow 张量的 Keras 模型中使用 InputLayer(或 Input)有什么好处? - What is the advantage of using an InputLayer (or an Input) in a Keras model with Tensorflow tensors? 将输入打印到Keras型号 - Printing the input to a Keras model keras:将图层插入现有 model 的开头(靠近输入) - keras: insert layers to the beginning (near the input) of an existing model 在处理 keras 功能 model - Keras、ZCB20B802A3F21255E054E488 的输入张量时发现意外实例 - Found unexpected instance while processing input tensors for keras functional model - Keras, Tensorflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM