使用bidirectional_dynamic_rnn时,没有足够的值来解析seq2seq.dynamic_decode上的ValueError

时间:2018-01-12 17:42:10

标签: python tensorflow deep-learning lstm recurrent-neural-network

我试图基于谷歌的NMT模型(https://github.com/tensorflow/nmt)建立我自己的神经机器翻译器。 一切顺利,简单的编码器/解码器,添加了BeamSearch,然后是Attention。现在我试图在编码器上添加双向rnn并遇到这个错误:

  

ValueError:没有足够的值来解包(预期2,得到0)

传递seq2seq.TrainingHelper和seq2seq.BasicDecoder后,在seq2seq.dynamic_decode函数上生成此错误。有人可以帮我解决这个问题吗?

我基于代码的这一特定部分:

https://github.com/tensorflow/nmt/blob/master/nmt/model.py#L589

我的代码:

    if encoder_type == "bidirectional":

        num_bi_layers = int(n_layers / 2)
        num_residual_layers = n_layers - 1
        num_bi_residual_layers = int(num_residual_layers / 2)

        cell_list = []
        for i in range(n_layers):
            cell = tf.contrib.rnn.BasicLSTMCell(hidden_units, forget_bias=forget_bias)

            if (i >= n_layers - num_residual_layers):
                cell = tf.contrib.rnn.ResidualWrapper(cell, residual_fn=None)
                if dropout > 0.0:
                    cell = tf.contrib.rnn.DropoutWrapper(
                        cell=cell, input_keep_prob=(1.0 - dropout))

            cell_list.append(cell)

        if len(cell_list) == 1:  # Single layer.
            fw_cell = cell_list[0]
            bw_cell = cell_list[0]
        else:  # Multi layers
            fw_cell = tf.contrib.rnn.MultiRNNCell(cell_list)
            bw_cell = tf.contrib.rnn.MultiRNNCell(cell_list)

        bi_outputs, bi_state = tf.nn.bidirectional_dynamic_rnn(
                                                        fw_cell,
                                                        bw_cell,
                                                        encoder_inputs_embedded,
                                                        dtype=dtype,
                                                        sequence_length=encoder_inputs_length,
                                                        time_major=False,
                                                        swap_memory=True)

        encoder_outputs, bi_encoder_state = tf.concat(bi_outputs, -1), bi_state

        if num_bi_layers == 1:
            encoder_last_state = bi_encoder_state
        else:
            # alternatively concat forward and backward states
            encoder_state = []
            for layer_id in range(num_bi_layers):
                encoder_state.append(bi_encoder_state[0][layer_id])  # forward
                encoder_state.append(bi_encoder_state[1][layer_id])  # backward
            encoder_last_state = tuple(encoder_state)

我认为错误是在binnn状态提取中,但令我困惑的是源代码实际上有效,表明这是正确的。我按照README步骤执行它,并使用整个模型完成所有操作......

此外,您可以按照整个代码(Jupyter笔记本上的整个错误):

https://github.com/denisb411/seq2seq-NMT-tensorflow/blob/master/seq2seq_tensorflow_1_4.ipynb

追溯:

https://pastebin.com/LeetGrpn

0 个答案:

没有答案