使用张量流连接两个 RNN 单元时出错

Error when concat two RNN cell with tensorflow

我收到这个错误

AttributeError: 'Tensor' object has no attribute 'c'

尝试执行此功能时

def _add_encoder(self, encoder_inputs, seq_len):
with tf.variable_scope('encoder'):
  cell_fw = tf.contrib.rnn.LSTMCell(self._hps.hidden_dim.value, initializer=self.rand_unif_init, state_is_tuple=False)
  cell_bw = tf.contrib.rnn.LSTMCell(self._hps.hidden_dim.value, initializer=self.rand_unif_init, state_is_tuple=False)
  (encoder_outputs, (fw_st, bw_st)) = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, encoder_inputs, dtype=tf.float32, sequence_length=seq_len, swap_memory=True)
  encoder_outputs = tf.concat(axis=2, values=encoder_outputs) # concatenate the forwards and backwards states
return encoder_outputs, fw_st, bw_st
  # Apply linear layer
  old_c = tf.concat(axis=1, values=[fw_st.c, bw_st.c]) # Concatenation of fw and bw cell

我正在使用 python 3.6,tensorflow 1.7

短版:

删除, state_is_tuple=False

更长的版本:

根据 LSTMCell

的 tensorflow 文档

state_is_tuple:

If True, accepted and returned states are 2-tuples of the c_state and m_state.

If False, they are concatenated along the column axis. This latter behavior will soon be deprecated.

LSTMStateTuple

Stores two elements: (c, h), in that order. Where c is the hidden state and h is the output.

Only used when state_is_tuple=True.

所以我建议改变

cell_fw = tf.contrib.rnn.LSTMCell(self._hps.hidden_dim.value, initializer=self.rand_unif_init, state_is_tuple=False)

cell_fw = tf.contrib.rnn.LSTMCell(self._hps.hidden_dim.value, initializer=self.rand_unif_init)