pseudo_teetotaler
pseudo_teetotaler

Reputation: 1575

Multilayer encoder output state to multilayer decoder in Seq2Seq model TF 1.0

Tensorflow Version 1.0

My question is, what dimension of encoder_state argument does tf.contrib.seq2seq attention_decoder_fn_train expects.

Can it take multilayered encoder state output ?

Context :

I want to create a multilayered bidirectional attention based seq2seq in tensorflow 1.0.

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. I concatenate the forward pass and backward pass states to pass it to encoder_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.

Link to my code

1:

Upvotes: 0

Views: 958

Answers (1)

pseudo_teetotaler
pseudo_teetotaler

Reputation: 1575

I found issue with my implementation. So posting it here. The problem was w.r.t. concatenating the encoder_fw_state and 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)

Upvotes: 1

Related Questions