Keras 中的有状态自动编码器

stateful autoencoder in Keras

我正在尝试创建一个有状态自动编码器模型。目标是使自动编码器对于每个时间序列都是有状态的。数据由10个时间序列组成,每个时间序列长度为567。

timeseries#1: 451, 318, 404, 199, 225, 158, 357, 298, 339, 155, 135, 239, 306, ....
timeseries#2: 304, 274, 150, 143, 391, 357, 278, 557, 98, 106, 305, 288, 325, ....
...
timeseries#10: 208, 138, 201, 342, 280, 282, 280, 140, 124, 261, 193, .....

我的回顾窗口是 28。所以我生成了以下具有 28 个时间步长的序列:

[451, 318, 404, 199, 225, 158, 357, 298, 339, 155, 135, 239, 306, .... ]
[318, 404, 199, 225, 158, 357, 298, 339, 155, 135, 239, 306, 56, ....]
[404, 199, 225, 158, 357, 298, 339, 155, 135, 239, 306, 56, 890, ....]
...
[304, 274, 150, 143, 391, 357, 278, 557, 98, 106, 305, 288, 325, ....]
[274, 150, 143, 391, 357, 278, 557, 98, 106, 305, 288, 325, 127, ....]
[150, 143, 391, 357, 278, 557, 98, 106, 305, 288, 325, 127, 798, ....]
...
[208, 138, 201, 342, 280, 282, 280, 140, 124, 261, 193, .....]
[138, 201, 342, 280, 282, 280, 140, 124, 261, 193, 854, .....]

每个时间序列都有 539 个序列。我需要做的是让 LSTM 对于每个时间序列都是有状态的,并在看到时间序列中的所有序列后重置状态。这是我的代码:

batch_size = 35  #(total Number of samples is 5390, and it is dividable by 35)
timesteps = 28
n_features = 1
hunits = 14
RepeatVector(timesteps/hunits = 2)
epochs = 1000


inputEncoder = Input(batch_shape=(35, 28, 1), name='inputEncoder')
outEncoder, c, h = LSTM(14, stateful=True, return_state=True, name='outputEncoder')(inputEncoder)
encoder_model = Model(inputEncoder, outEncoder)

context = RepeatVector(2, name='inputDecoder')(outEncoder)
context_reshaped = Reshape(28, 1), name='ReshapeLayer')(context)

outDecoder = LSTM(1, return_sequences=True, stateful=True, name='decoderLSTM')(context_reshaped)

autoencoder = Model(inputEncoder, outDecoder)

autoencoder.compile(loss='mse', optimizer='rmsprop')

for i in range(epochs):
       history = autoencoder.fit(data, data,
                          validation_split=config['validation_split_ratio'],
                          shuffle=False,
                          batch_size=35,
                          epochs=1,
                         )   
       autoencoder.reset_states()

2 个问题:

1- 第一个纪元结束后出现此错误,我想知道这是怎么回事:

ValueError: Cannot feed value of shape (6, 28, 1) for Tensor u'inputEncoder:0', which has shape '(35, 28, 1)'

2- 我不认为该模型可以正常工作。在这里它将在所有批次(一个纪元)之后重置状态,这意味着在处理完所有时间序列之后。我应该如何将其更改为在时间序列之间有状态?

问题出在 validation_split 率!!它设置为 0.33%,当拆分发生时,它会尝试训练 3611 个数据样本,这些样本不能被我的 batch_size=35 整除。基于这个 post 我可以找到正确的数字,从 post:

复制
def quantize_validation_split(validation_split, sample_count, batch_size):
    batch_count = sample_count / batch_size
    return float(int(batch_count * validation_split)) / batch_count

then you can call model.fit(..., validation_split=fix_validation_split(0.05, len(X), batch_size)). but it would be cool if keras did this for you inside fit().

此外,关于使自动编码器以我需要的方式有状态:每个纪元的末尾不应有 reset_state