Keras LSTM模型未得到实例化

Keras LSTM模型未得到实例化,keras,lstm,bidirectional,Keras,Lstm,Bidirectional,我正在尝试为NER任务创建一个基线模型,使用双向LSTM和Keras提供的功能API 我使用的嵌入层是一个100维的特征向量 层的输入是长度的填充序列 MAX_LEN = 575 (注:输入和输出尺寸相同) 我希望在每个时间步都有一个输出,因此我设置了 return_sequences = True 输出仅为通过软最大值层的激活 但在编译模型时,我一直收到这样的警告 UserWarning: Model inputs must come from `keras.layers.Input` (

我正在尝试为NER任务创建一个基线模型,使用双向LSTM和Keras提供的功能API

我使用的嵌入层是一个100维的特征向量

层的输入是长度的填充序列

MAX_LEN = 575
(注:输入和输出尺寸相同)

我希望在每个时间步都有一个输出,因此我设置了

return_sequences = True
输出仅为通过软最大值层的激活

但在编译模型时,我一直收到这样的警告

UserWarning: Model inputs must come from `keras.layers.Input`
(thus holding past layer metadata), they cannot be the output of a 
previous non-Input layer. Here, a tensor specified as input to your model was
not an Input tensor, it was generated by layer embedding_3.
Note that input tensors are instantiated via `tensor = keras.layers.Input(shape)`.
The tensor that caused the issue was: embedding_3_40/embedding_lookup/Identity:0 str(x.name))
伴随着

AssertionError:
回溯:

---> 37 model = Model(inputs = nn_input, outputs = nn_output)
---> 91             return func(*args, **kwargs)
---> 93             self._init_graph_network(*args, **kwargs)
    222             # It's supposed to be an input layer, so only one node
    223             # and one tensor output.
--> 224             assert node_index == 0
我试着调试代码以检查维度,但它们似乎匹配,正如代码中的注释所强调的那样

nn_input = Input(shape = (MAX_LEN,) , dtype = 'int32')

print(nn_input.shape)   #(?, 575)

nn_input = embedding_layer(nn_input)

print(nn_input.shape)   #(?, 575, 100)

nn_out, forward_h, forward_c, backward_h, backward_c = Bidirectional(LSTM(MAX_LEN, return_sequences = True, return_state = True))(nn_input)

print(forward_h.shape)  #(?, 575)
print(forward_c.shape)  #(?, 575)
print(backward_h.shape) #(?, 575)
print(backward_c.shape) #(?, 575)

print(nn_out.shape)     #(?, ?, 1150)

state_h = Concatenate()([forward_h, backward_h])
state_c = Concatenate()([forward_c, backward_c])

print(state_h.shape)    #(?, 1150)
print(state_c.shape)    #(?, 1150)

densor = Dense(100, activation='softmax')
nn_output = densor(nn_out)

print(nn_output.shape)  #(?, 575, 100)

model = Model(inputs = nn_input, outputs = nn_output)
这对一些人来说似乎微不足道,但我担心我对LSTM或至少对Keras的理解存在缺陷

如有必要,我将在编辑中提供更多详细信息


任何帮助都将不胜感激

如错误所示,您必须将层keras.layers.Input的输出张量传递给模型API。在这种情况下,张量nn_输入是嵌入_层的输出。更改用于将嵌入层的输出从nn_输入分配给其他对象的变量名

nn_input = Input(shape = (MAX_LEN,) , dtype = 'int32')
# the line below is the cause of the error. Change the output variable name to like nn_embed. 
nn_input = embedding_layer(nn_input)