python - 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
解决方案
推荐阅读
- swift - awakeFromNimb 不会在 TableView reloadData Swift 上被调用
- jboss - JBoss HTTPS 连接在一段时间后断开
- python - 如何让 Discord 机器人异步等待对多条消息的反应?
- java - 在响应json中将键的字符串数据类型转换为数组
- javascript - 在数据表中添加字段的总和
- c# - 无法“找到”浮动操作按钮
- ms-word - 在 Mendeley 中,如何获取 Word 文档中的引文以上传到我的图书馆?
- javascript - 尝试使用 iView 在我的 Vue 项目中使用主题时编译错误
- angular - RxJs:合并 concapMap 之后的所有值,以便它们在订阅“下一个”事件中一起发出?
- python - OpenCV 模板匹配将所有内容与单色参考图像匹配