Keras 中的 Seq2Seq 双向编码器解码器
Posted
技术标签:
【中文标题】Keras 中的 Seq2Seq 双向编码器解码器【英文标题】:Seq2Seq Bidirectional Encoder Decoder in Keras 【发布时间】:2018-11-21 17:51:53 【问题描述】:我正在尝试使用 Keras 实现一个 seq2seq 编码器-解码器,编码器上的双向 lstm 如下:
from keras.layers import LSTM,Bidirectional,Input,Concatenate
from keras.models import Model
n_units = 8
n_input = 1
n_output = 1
# encoder
encoder_inputs = Input(shape=(None, n_input))
encoder = Bidirectional(LSTM(n_units, return_state=True))
encoder_outputs, forward_h, forward_c, backward_h, backward_c = encoder(encoder_inputs)
state_h = Concatenate()([forward_h, backward_h])
state_c = Concatenate()([forward_c, backward_c])
encoder_states = [state_h, state_c]
# decoder
decoder_inputs = Input(shape=(None, n_output))
decoder_lstm = LSTM(n_units*2, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
这是我在最后一行遇到的以下错误:
ValueError: Dimensions must be equal, but are 8 and 16 for
'lstm_2_1/MatMul_4' (op: 'MatMul') with input shapes: [?,8], [16,16].
有什么想法吗?
【问题讨论】:
您的代码在我的机器上运行没有任何错误(尽管在创建Model
对象并调用compile
和fit
之后)。你能发布你正在使用的完整代码吗?可能是你没有发的部分有问题。
你是对的,错误指向了这个块的最后一行,但是真正的错误是从与推理解码器相关的另一行传播的!谢谢,现在解决了!
【参考方案1】:
虽然错误指向问题中块的最后一行,但这是由于推理解码器中隐藏单元的数量错误。解决了!
完整的工作代码:
from keras.layers import LSTM,Bidirectional,Input,Concatenate
from keras.models import Model
n_units = 8
n_input = 1
n_output = 1
# encoder
encoder_inputs = Input(shape=(None, n_input))
encoder = Bidirectional(LSTM(n_units, return_state=True))
encoder_outputs, forward_h, forward_c, backward_h, backward_c = encoder(encoder_inputs)
state_h = Concatenate()([forward_h, backward_h])
state_c = Concatenate()([forward_c, backward_c])
encoder_states = [state_h, state_c]
# decoder
decoder_inputs = Input(shape=(None, n_output))
decoder_lstm = LSTM(n_units*2, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
decoder_dense = Dense(n_output, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
# define inference encoder
encoder_model = Model(encoder_inputs, encoder_states)
# define inference decoder
decoder_state_input_h = Input(shape=(n_units*2,))
decoder_state_input_c = Input(shape=(n_units*2,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model([decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states)
【讨论】:
嗨,现在您已经定义了编码器和解码器模型,您将如何训练它们?您将如何将这两个 Keras 模型组合成一个可以使用 mode.fit() 进行训练的自动编码器模型?谢谢! 将此作为参考blog.keras.io/…【参考方案2】:https://***.com/a/50820218/10706937
from keras.layers import LSTM,Bidirectional,Input,Concatenate
from keras.models import Model
n_units = 8
n_input = 1
n_output = 1
# encoder
encoder_inputs = Input(shape=(None, n_input))
encoder = Bidirectional(LSTM(n_units, return_state=True))
encoder_outputs, forward_h, forward_c, backward_h, backward_c = encoder(encoder_inputs)
state_h = Concatenate()([forward_h, backward_h])
state_c = Concatenate()([forward_c, backward_c])
encoder_states = [state_h, state_c]
# decoder
decoder_inputs = Input(shape=(None, n_output))
decoder_lstm = LSTM(n_units*2, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
decoder_dense = Dense(n_output, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
# define inference encoder
encoder_model = Model(encoder_inputs, encoder_states)
# define inference decoder
decoder_state_input_h = Input(shape=(n_units*2,))
decoder_state_input_c = Input(shape=(n_units*2,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model([decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states)
如何删除上述评论中的推理编码器。我的意思是如何在没有推理编码器的情况下解决错误
【讨论】:
以上是关于Keras 中的 Seq2Seq 双向编码器解码器的主要内容,如果未能解决你的问题,请参考以下文章