如何在函数 API 中实现双向包装器?
How to implement a bidirectional wrapper in functional API?
双向层是将编码器连接到解码器还是将解码器连接到解码器。
这是编码器的 3 个部分,它们提供给下面的解码器。
#encoding layers
input_context = Input(shape = (maxLen, ), dtype = 'int32', name = 'input_context')
input_ctx_embed = embed_layer(input_context)
encoder_lstm, h1, c1 = LSTM(256, return_state = True, return_sequences = True)(input_ctx_embed)
encoder_lstm2,h2, c2 = LSTM(256, return_state = True, return_sequences = True)(encoder_lstm)
_,h3, c3 = LSTM(256, return_state = True)(encoder_lstm2)
encoder_states = [h1, c1, h2, c2,h3,c3]
#layers for the decoder
input_target = Input(shape = (maxLen, ), dtype = 'int32', name = 'input_target')
input_tar_embed = embed_layer(input_target)
# the decoder lstm uses the final states from the encoder lstm as the initial state
decoder_lstm, context_h, context_c = LSTM(256, return_state = True, return_sequences = True)
(input_tar_embed, initial_state = [h1, c1],)
decoder_lstm2, context_h2, context_c2 = LSTM(256, return_state = True, return_sequences = True)
(decoder_lstm, initial_state = [h2, c2],)
final, context_h3, context_c3 = LSTM(256, return_state = True, return_sequences = True)
(decoder_lstm2, initial_state = [h3, c3],)
dense_layer=Dense(vocab_size, activation = 'softmax')
output = TimeDistributed(dense_layer)(final)
#output=Dropout(0.3)(output)
model = Model([input_context, input_target], output)
不确定双向层在哪里,因为在我看来,如果您想使用 keras.layers.LSTM()
构建 双向 RNN 结构而不使用 keras.layer.Bidirectional()
,然后在keras.layers.LSTM()
中有一个设置叫做go_backwards
,它的默认值是False
,设置它True
使LSTM倒退。如果你 只是问在编码器-解码器结构中将双向 LSTM 放在哪里,那么我的回答将是 "you can put it wherever you want, if that way make your model better."
如果我混淆了什么,请告诉我。
双向层是将编码器连接到解码器还是将解码器连接到解码器。 这是编码器的 3 个部分,它们提供给下面的解码器。
#encoding layers
input_context = Input(shape = (maxLen, ), dtype = 'int32', name = 'input_context')
input_ctx_embed = embed_layer(input_context)
encoder_lstm, h1, c1 = LSTM(256, return_state = True, return_sequences = True)(input_ctx_embed)
encoder_lstm2,h2, c2 = LSTM(256, return_state = True, return_sequences = True)(encoder_lstm)
_,h3, c3 = LSTM(256, return_state = True)(encoder_lstm2)
encoder_states = [h1, c1, h2, c2,h3,c3]
#layers for the decoder
input_target = Input(shape = (maxLen, ), dtype = 'int32', name = 'input_target')
input_tar_embed = embed_layer(input_target)
# the decoder lstm uses the final states from the encoder lstm as the initial state
decoder_lstm, context_h, context_c = LSTM(256, return_state = True, return_sequences = True)
(input_tar_embed, initial_state = [h1, c1],)
decoder_lstm2, context_h2, context_c2 = LSTM(256, return_state = True, return_sequences = True)
(decoder_lstm, initial_state = [h2, c2],)
final, context_h3, context_c3 = LSTM(256, return_state = True, return_sequences = True)
(decoder_lstm2, initial_state = [h3, c3],)
dense_layer=Dense(vocab_size, activation = 'softmax')
output = TimeDistributed(dense_layer)(final)
#output=Dropout(0.3)(output)
model = Model([input_context, input_target], output)
不确定双向层在哪里,因为在我看来,如果您想使用 keras.layers.LSTM()
构建 双向 RNN 结构而不使用 keras.layer.Bidirectional()
,然后在keras.layers.LSTM()
中有一个设置叫做go_backwards
,它的默认值是False
,设置它True
使LSTM倒退。如果你 只是问在编码器-解码器结构中将双向 LSTM 放在哪里,那么我的回答将是 "you can put it wherever you want, if that way make your model better."
如果我混淆了什么,请告诉我。