首页 > 解决方案 > lstm 自动编码器预测总是从零开始

问题描述

我正在尝试构建一个 LSTM 自动编码器来查找一组信号中的异常。自动编码器似乎运行良好,除了在曲线的开头。在他开始时,所有重建的曲线都从零开始。当我标准化它们时,实际上这里的零是每条曲线的平均值。看这张图

原始曲线与重建曲线的示例

它发生在所有曲线上。训练曲线是这样的

训练曲线

我在这里想念什么?

自动编码器是使用 Keras 构建的,如下所示

input_dim = 1
latent_dim = 50
array_length = lstm_df.shape[1]

inputs = Input(shape=(array_length, input_dim))
# encoding
encoded = LSTM(latent_dim, return_sequences=True )(inputs)
decoded = LSTM(input_dim, return_sequences=True, activation='linear'(encoded)
# I also tried the default activation (tanh) here

sequence_autoencoder = Model(inputs, decoded)
sequence_autoencoder.compile(optimizer='rmsprop', loss='mean_squared_error')
# I also tried 'adam' optimizer

# And trained as follows

epochs = 80
history = sequence_autoencoder.fit(lstm_df.values.reshape(lstm_df.shape[0], array_length, input_dim), 
                                   lstm_df.values.reshape(lstm_df.shape[0], array_length, input_dim),
                                   verbose=True,
                                   epochs=epochs,
                                   batch_size=32,
                                   shuffle=True)
# Prediction
prediction = sequence_autoencoder.predict(test.values.reshape(test.shape[0], array_length, input_dim))
mse = np.power(test.values - prediction.reshape(test.shape), 2).mean(1)
error_df = pd.DataFrame({'reconstruction error': -mse.ravel()}, index=test.index)
# I am inverting the mse to stick to the convention, the smaller the more outlier 

outliers_fraction = .2

no_of_anomalies = int(len(error_df) * outliers_fraction)
anomolus_ids = error_df.sort_values('reconstruction error').head(no_of_anomalies).index

训练和测试数据从这里下载 https://github.com/h2oai/h2o-2/tree/master/smalldata/anomaly

ecg_discord_train.csv 和 ecg_discord_test.csv

标签: pythonkeraslstmautoencoderanomaly-detection

解决方案


推荐阅读