Keras-为特定时间戳恢复LSTM隐藏状态 [英] Keras - Restore LSTM hidden state for a specific time stamp

查看:152
本文介绍了Keras-为特定时间戳恢复LSTM隐藏状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题仍在继续( LSTM-对部分序列进行预测).如上一个问题所述,我已经训练了一种有状态 LSTM模型,该模型用于批处理100个样本/标签的二进制分类,如下所示:

This question is in continue to (LSTM - Making predictions on partial sequence). As described in the previous question I've trained a stateful LSTM model for binary classification with batches of 100 samples/labels like so:

[Feature 1,Feature 2, .... ,Feature 3][Label 1]
[Feature 1,Feature 2, .... ,Feature 3][Label 2]
...
[Feature 1,Feature 2, .... ,Feature 3][Label 100]

型号代码:

def build_model(num_samples, num_features, is_training):
    model = Sequential()
    opt = optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0001)

    batch_size = None if is_training else 1
    stateful = False if is_training else True
    first_lstm = LSTM(32, batch_input_shape=(batch_size, num_samples, num_features),  return_sequences=True,
                      activation='tanh', stateful=stateful)

    model.add(first_lstm)
    model.add(LeakyReLU())
    model.add(Dropout(0.2))
    model.add(LSTM(16, return_sequences=True, activation='tanh', stateful=stateful))
    model.add(Dropout(0.2))
    model.add(LeakyReLU())
    model.add(LSTM(8, return_sequences=True, activation='tanh', stateful=stateful))
    model.add(LeakyReLU())
    model.add(Dense(1, activation='sigmoid'))

    if is_training:
        model.compile(loss='binary_crossentropy', optimizer=opt,
                      metrics=['accuracy', f1])
    return model

进行预测时,模型为无状态,批次大小为1,并且在每个样本之后都将分类概率取回,如下所示:

When predicting, the model is stateless, batch size is 1 and the classification probability is retrieved after each sample like so:

[Feature 1,Feature 2, .... ,Feature 10][Label 1] -> (model) -> probability

模型处理完100个样本后,

调用model.reset_states().该模型有效,效果很好.

calling model.reset_states() after the model finished processing a batch of 100 samples. The model works and the results are great.

注意:我的数据是来自多个来源的事件.

Note: My data are events coming from multiple sources.

我的问题:

在测试模型时,我可以控制样本的顺序,并且可以确保样本来自同一来源.也就是说,所有前100个样本均来自源1,然后调用model.reset_states()之后,接下来的100个样本均来自源2,依此类推.

When I test my model I have control over the order of the samples and I can make sure that the samples arrive from the same source. i.e all first 100 samples are from source 1, then after calling model.reset_states() the next 100 samples are from source 2 and so on.

但是,在我的生产环境中,示例是以异步方式到达的,例如:

On my production environment, however, the samples arrives in an async way for example:

首先从来源1提取3个样本,然后从来源2提取2个样本,等等"

First 3 samples from source 1 then 2 samples from source 2 etc'

说明:

我的问题:

如何在每个时间戳的特定时间戳上序列化模型状态,所以我可以在每个样本之后保存它,然后在新样本从相同来源到达时将其加载回去.

How can I serialize the model state at a certain timestamp for each source, so I can save it after each sample then load it back when a new sample arrives from the same source.

推荐答案

您可以像这样获取并设置内部状态:

You can get and set the internal states like so:

import keras.backend as K

def get_states(model):
    return [K.get_value(s) for s,_ in model.state_updates]

def set_states(model, states):
    for (d,_), s in zip(model.state_updates, states):
        K.set_value(d, s)

这篇关于Keras-为特定时间戳恢复LSTM隐藏状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆