【问题标题】:Tensorflow: how to obtain intermediate cell states (c) from LSTMCell using dynamic_rnn?Tensorflow:如何使用dynamic_rnn从LSTMCell获取中间单元状态(c)?
【发布时间】:2017-12-11 00:37:53
【问题描述】:

默认情况下,函数dynamic_rnn只输出每个时间点的隐藏状态(称为m),可以通过以下方式获得:

cell = tf.contrib.rnn.LSTMCell(100)
rnn_outputs, _ = tf.nn.dynamic_rnn(cell,
                                   inputs=inputs,
                                   sequence_length=sequence_lengths,
                                   dtype=tf.float32)

还有没有办法获得中间(非最终)单元状态 (c)?

tensorflow 贡献者 mentions 可以使用单元格包装器来完成:

class Wrapper(tf.nn.rnn_cell.RNNCell):
  def __init__(self, inner_cell):
     super(Wrapper, self).__init__()
     self._inner_cell = inner_cell
  @property
  def state_size(self):
     return self._inner_cell.state_size
  @property
  def output_size(self):
    return (self._inner_cell.state_size, self._inner_cell.output_size)
  def call(self, input, state)
    output, next_state = self._inner_cell(input, state)
    emit_output = (next_state, output)
    return emit_output, next_state

但是,它似乎不起作用。有什么想法吗?

【问题讨论】:

    标签: python machine-learning tensorflow lstm rnn


    【解决方案1】:

    建议的解决方案对我有用,但Layer.call 方法规范更通用,因此以下Wrapper 应该对 API 更改更健壮。你这个:

    class Wrapper(tf.nn.rnn_cell.RNNCell):
      def __init__(self, inner_cell):
         super(Wrapper, self).__init__()
         self._inner_cell = inner_cell
    
      @property
      def state_size(self):
         return self._inner_cell.state_size
    
      @property
      def output_size(self):
        return (self._inner_cell.state_size, self._inner_cell.output_size)
    
      def call(self, input, *args, **kwargs):
        output, next_state = self._inner_cell(input, *args, **kwargs)
        emit_output = (next_state, output)
        return emit_output, next_state
    

    这是测试:

    n_steps = 2
    n_inputs = 3
    n_neurons = 5
    
    X = tf.placeholder(dtype=tf.float32, shape=[None, n_steps, n_inputs])
    basic_cell = Wrapper(tf.nn.rnn_cell.LSTMCell(num_units=n_neurons, state_is_tuple=False))
    outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)
    print(outputs, states)
    
    X_batch = np.array([
      # t = 0      t = 1
      [[0, 1, 2], [9, 8, 7]], # instance 0
      [[3, 4, 5], [0, 0, 0]], # instance 1
      [[6, 7, 8], [6, 5, 4]], # instance 2
      [[9, 0, 1], [3, 2, 1]], # instance 3
    ])
    
    with tf.Session() as sess:
      sess.run(tf.global_variables_initializer())
      outputs_val = outputs[0].eval(feed_dict={X: X_batch})
      print(outputs_val)
    

    返回的outputs(?, 2, 10)(?, 2, 5)张量的元组,它们都是LSTM状态和输出。请注意,我使用的是LSTMCell 的“毕业”版本,来自tf.nn.rnn_cell 包,而不是tf.contrib.rnn。还要注意state_is_tuple=True,以避免与LSTMStateTuple打交道。

    【讨论】:

      【解决方案2】:

      基于Maxim的想法,我最终得到了以下解决方案:

      class StatefulLSTMCell(LSTMCell):
          def __init__(self, *args, **kwargs):
              super(StatefulLSTMCell, self).__init__(*args, **kwargs)
      
          @property
          def output_size(self):
              return (self.state_size, super(StatefulLSTMCell, self).output_size)
      
          def call(self, input, state):
              output, next_state = super(StatefulLSTMCell, self).call(input, state)
              emit_output = (next_state, output)
              return emit_output, next_state
      

      【讨论】:

        猜你喜欢
        • 2018-06-22
        • 1970-01-01
        • 1970-01-01
        • 2018-08-12
        • 2019-12-04
        • 2017-05-07
        • 2019-09-20
        • 2017-02-04
        • 2017-12-25
        相关资源
        最近更新 更多