LSTM 模型没有被实例化
LSTM Model not getting instantiated
我正在尝试为 NER 任务创建一个基线模型,使用双向 LSTM 和 Keras
提供的函数 API
我用的embedding层是100维的特征向量
层的输入是长度为
的填充序列
MAX_LEN = 575
(注意:输入和输出的维度相同)
我想要在每个时间步都有一个输出,因此我设置了
return_sequences = True
输出只是通过soft-max层
的激活
但是在编译模型时我不断收到此警告
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_input 是 embedding_layer 的输出。将用于分配 embedding_layer 输出的变量名从 nn_input 更改为其他名称。
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)
我正在尝试为 NER 任务创建一个基线模型,使用双向 LSTM 和 Keras
提供的函数 API我用的embedding层是100维的特征向量
层的输入是长度为
的填充序列MAX_LEN = 575
(注意:输入和输出的维度相同)
我想要在每个时间步都有一个输出,因此我设置了
return_sequences = True
输出只是通过soft-max层
的激活但是在编译模型时我不断收到此警告
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_input 是 embedding_layer 的输出。将用于分配 embedding_layer 输出的变量名从 nn_input 更改为其他名称。
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)