簡體   English   中英

如何在Keras中結合兩層? 一種帶有嵌入,一種帶有“其他”功能

[英]How to combine two layers in Keras? One with embedding, one with “other” feature

我正在嘗試將嵌入層與數字要素層組合在一起。 我喜歡:

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:圖形已斷開連接:無法在“ input_2 ”層獲取張量Tensor("input_2:0", shape=(?, 82), dtype=float32) input_2 順利訪問了以下先前的層: []

同時結合輸入和輸出。 請幫忙。

您正在覆蓋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)

暫無
暫無

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

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