【问题标题】:Error when concat two RNN cell with tensorflow使用张量流连接两个 RNN 单元时出错
【发布时间】:2018-05-03 10:44:44
【问题描述】:

我收到这个错误

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,张量流 1.7

【问题讨论】:

    标签: python python-3.x tensorflow rnn tensor


    【解决方案1】:

    短版:

    删除, state_is_tuple=False

    加长版:

    根据LSTMCell的tensorflow文档,

    state_is_tuple:

    如果为 True,则接受和返回的状态是 c_state 和 m_state 的 2 元组

    如果为 False,则它们沿列轴连接。 后一种行为很快将被弃用

    还有LSTMStateTuple

    按顺序存储两个元素:(c, h)。其中 c 是隐藏状态,h 是输出。

    仅在 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)
    

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 2021-01-30
      • 2018-03-13
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      相关资源
      最近更新 更多