【问题标题】:Keras LSTM stateKeras LSTM 状态
【发布时间】:2016-07-15 19:50:02
【问题描述】:

我想在 Keras 中运行 LSTM 并获得输出和状态。 TF中的一些类似的东西

with tf.variable_scope("RNN"):
      for time_step in range(num_steps):
        if time_step > 0: tf.get_variable_scope().reuse_variables()
        (cell_output, state) = cell(inputs[:, time_step, :], state)
        outputs.append(cell_output)

有没有办法在 Keras 中做到这一点,当序列的长度很大时,我可以获得最后一个状态并将其提供给新的输入。我知道 stateful=True 但我也想在训练时访问状态。我知道它使用的是扫描而不是 for 循环,但基本上我想保存状态,然后在下次运行时,将它们设为 LSTM 的起始状态。简而言之,获取输出和状态。

【问题讨论】:

    标签: keras lstm


    【解决方案1】:

    由于LSTM是一个层,一个层在keras中只能有一个输出(如果我错了请纠正我),你不能在不修改源代码的情况下同时得到两个输出。

    最近我正在破解 keras 以实现一些高级结构,一些你可能不喜欢的想法确实有效。我所做的是覆盖 keras 层,以便我们可以访问表示隐藏状态的张量。

    首先,您可以查看keras/layers/recurrent.py 中的call() 函数,了解 keras 是如何工作的:

    def call(self, x, mask=None):
        # input shape: (nb_samples, time (padded with zeros), input_dim)
        # note that the .build() method of subclasses MUST define
        # self.input_spec with a complete input shape.
        input_shape = self.input_spec[0].shape
        if K._BACKEND == 'tensorflow':
            if not input_shape[1]:
                raise Exception('When using TensorFlow, you should define '
                                'explicitly the number of timesteps of '
                                'your sequences.\n'
                                'If your first layer is an Embedding, '
                                'make sure to pass it an "input_length" '
                                'argument. Otherwise, make sure '
                                'the first layer has '
                                'an "input_shape" or "batch_input_shape" '
                                'argument, including the time axis. '
                                'Found input shape at layer ' + self.name +
                                ': ' + str(input_shape))
        if self.stateful:
            initial_states = self.states
        else:
            initial_states = self.get_initial_states(x)
        constants = self.get_constants(x)
        preprocessed_input = self.preprocess_input(x)
    
        last_output, outputs, states = K.rnn(self.step, preprocessed_input,
                                             initial_states,
                                             go_backwards=self.go_backwards,
                                             mask=mask,
                                             constants=constants,
                                             unroll=self.unroll,
                                             input_length=input_shape[1])
        if self.stateful:
            self.updates = []
            for i in range(len(states)):
                self.updates.append((self.states[i], states[i]))
    
        if self.return_sequences:
            return outputs
        else:
            return last_output
    

    其次,我们应该覆盖我们的图层,这是一个简单的脚本:

    import keras.backend as K
    from keras.layers import Input, LSTM
    class MyLSTM(LSTM):
       def call(self, x, mask=None):
       # .... blablabla, right before return
    
       # we add this line to get access to states
       self.extra_output = states
    
       if self.return_sequences:
       # .... blablabla, to the end
    
       # you should copy **exactly the same code** from keras.layers.recurrent
    
    I = Input(shape=(...))
    lstm = MyLSTM(20)
    output = lstm(I) # by calling, we actually call the `call()` and create `lstm.extra_output`
    extra_output = lstm.extra_output # refer to the target
    
    calculate_function = K.function(inputs=[I], outputs=extra_output+[output]) # use function to calculate them **simultaneously**. 
    

    【讨论】:

    • 顺便说一下,自定义层在加载和保存时会导致一些错误,因为keras在加载时找不到MyLSTM。打电话给model_from_json() 时让custom_object={'MyLSTM':MyLSTM}。应该很简单。
    猜你喜欢
    • 2018-03-30
    • 2017-08-31
    • 2018-04-04
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2019-03-15
    • 1970-01-01
    相关资源
    最近更新 更多