[英]Multilayer encoder output state to multilayer decoder in Seq2Seq model TF 1.0
Tensorflow Version 1.0 Tensorflow版本1.0
My question is, what dimension of encoder_state
argument does tf.contrib.seq2seq attention_decoder_fn_train
expects. 我的问题是, tf.contrib.seq2seq attention_decoder_fn_train
期望的encoder_state
参数的尺寸是tf.contrib.seq2seq attention_decoder_fn_train
。
Can it take multilayered encoder state output ? 是否可以采用多层编码器状态输出?
Context : 内容 :
I want to create a multilayered bidirectional attention based seq2seq in tensorflow 1.0 . 我想在tensorflow 1.0中创建基于多层双向注意的seq2seq 。
My encoder : 我的编码器:
cell = LSTM(n)
cell = MultiRnnCell([cell]*4)
((encoder_fw_outputs,encoder_bw_outputs),
(encoder_fw_state,encoder_bw_state)) = (tf.nn.bidirectional_dynamic_rnn(cell_fw=cell, cell_bw = cell.... )
Now, the mutilayered bidirectional encoder returns encoder cell_states[c]
and hidden_states[h]
for each layer and also for backward and forward pass. 现在, cell_states[c]
双向编码器为每个层以及向后和向前传递返回编码器cell_states[c]
和hidden_states[h]
。 I concatenate the forward pass and backward pass states to pass it to encoder_state: 我将前进和后退状态串联起来,以将其传递到coder_state:
self.encoder_state = tf.concat((encoder_fw_state, encoder_bw_state), -1)
And I pass this to my decoder : 然后将其传递给我的解码器:
decoder_fn_train = seq2seq.simple_decoder_fn_train(encoder_state=self.encoder_state)
(self.decoder_outputs_train,
self.decoder_state_train,
self.decoder_context_state_train) = seq2seq.dynamic_rnn_decoder(cell=decoder_cell,... )
But it gives following error : 但是它给出了以下错误:
ValueError: The two structures don't have the same number of elements. First structure: Tensor("BidirectionalEncoder/transpose:0", shape=(?, 2, 2, 20), dtype=float32), second structure: (LSTMStateTuple(c=20, h=20), LSTMStateTuple(c=20, h=20)).
My decoder_cell
is also multilayered. 我的decoder_cell
也是多层的。
I found issue with my implementation. 我发现实施存在问题。 So posting it here. 所以在这里发布。 The problem was wrt concatenating the encoder_fw_state
and encoder_bw_state
. 问题是要串联encoder_fw_state
和encoder_bw_state
。 The right way to do is as follows : 正确的方法如下:
self.encoder_state = []
for i in range(self.num_layers):
if isinstance(encoder_fw_state[i], LSTMStateTuple):
encoder_state_c = tf.concat((encoder_fw_state[i].c, encoder_bw_state[i].c), 1, name='bidirectional_concat_c')
encoder_state_h = tf.concat((encoder_fw_state[i].h, encoder_bw_state[i].h), 1, name='bidirectional_concat_h')
encoder_state = LSTMStateTuple(c=encoder_state_c, h=encoder_state_h)
elif isinstance(encoder_fw_state[i], tf.Tensor):
encoder_state = tf.concat((encoder_fw_state[i], encoder_bw_state[i]), 1, name='bidirectional_concat')
self.encoder_state.append(encoder_state)
self.encoder_state = tuple(self.encoder_state)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.