如何在Keras中结合两层?一种具有嵌入功能,一种具有“其他”功能

时间:2019-05-29 02:52:28

标签: python keras deep-learning

我正在尝试将嵌入层与数字要素层组合在一起。我确实喜欢:

tensor_feature = Input(shape=(MAX_LENGTH, 3))
tensor_embed = Input(shape=(MAX_LENGTH, ))
tensor_embed = Embedding(len(word2index), 128)(tensor_embed)

merged_tensor = concatenate([tensor_embed, tensor_feature]) 
model = Bidirectional(LSTM(256, return_sequences=True))(merged_tensor)
model = Bidirectional(LSTM(128, return_sequences=True))(model)
model = TimeDistributed(Dense(len(tag2index)))(model)
model = Activation('softmax')(model)
model = Model(inputs=[tensor_embed,tensor_feature],outputs=model)

注意到MAX_LENGTH是82。
不幸的是,我遇到了这样的错误:

  

ValueError:图断开:无法获取张量值   Tensor("input_2:0", shape=(?, 82), dtype=float32)位于“ input_2”层。   可以顺利访问以下先前的图层:[]

同时组合输入和输出。请帮忙。

1 个答案:

答案 0 :(得分:1)

您正在覆盖tensor_embed,这是用于嵌入输出的输入层,并再次将其用作模型中的输入。将您的代码更改为

tensor_feature = Input(shape=(MAX_LENGTH, 3))
tensor_embed_feature = Input(shape=(MAX_LENGTH, ))
tensor_embed = Embedding(len(word2index), 128)(tensor_embed_feature)

merged_tensor = concatenate([tensor_embed, tensor_feature]) 
model = Bidirectional(LSTM(256, return_sequences=True))(merged_tensor)
model = Bidirectional(LSTM(128, return_sequences=True))(model)
model = TimeDistributed(Dense(len(tag2index)))(model)
model = Activation('softmax')(model)
model = Model(inputs=[tensor_embed_feature,tensor_feature],outputs=model)