0

我正在应用 LSTM 自动编码器进行异常检测。由于异常数据与正常数据相比非常少,因此仅使用正常实例进行训练。测试数据由异常和正常实例组成。在训练期间,模型损失似乎不错。然而,在测试数据中,模型产生的准确性较差。即异常点和正常点没有很好地分开。
我的代码片段如下:

.............
.............
X_train = X_train.reshape(X_train.shape[0], lookback, n_features)
X_valid = X_valid.reshape(X_valid.shape[0], lookback, n_features)
X_test = X_test.reshape(X_test.shape[0], lookback, n_features)
.....................
......................
N = 1000
batch = 1000
lr = 0.0001
timesteps = 3
encoding_dim = int(n_features/2)
lstm_model = Sequential()
lstm_model.add(LSTM(N, activation='relu', input_shape=(timesteps, n_features), return_sequences=True))
lstm_model.add(LSTM(encoding_dim, activation='relu', return_sequences=False))
lstm_model.add(RepeatVector(timesteps))
# Decoder
lstm_model.add(LSTM(timesteps, activation='relu', return_sequences=True))
lstm_model.add(LSTM(encoding_dim, activation='relu', return_sequences=True))
lstm_model.add(TimeDistributed(Dense(n_features)))
lstm_model.summary()
adam = optimizers.Adam(lr)
lstm_model.compile(loss='mse', optimizer=adam)

cp = ModelCheckpoint(filepath="lstm_classifier.h5",
                     save_best_only=True,
                     verbose=0)

tb = TensorBoard(log_dir='./logs',
                 histogram_freq=0,
                 write_graph=True,
                 write_images=True)

lstm_model_history = lstm_model.fit(X_train, X_train,
                                    epochs=epochs,
                                    batch_size=batch,
                                    shuffle=False,
                                    verbose=1,
                                    validation_data=(X_valid, X_valid),
                                    callbacks=[cp, tb]).history


.........................
test_x_predictions = lstm_model.predict(X_test)
mse = np.mean(np.power(preprocess_data.flatten(X_test) - preprocess_data.flatten(test_x_predictions), 2), axis=1)

error_df = pd.DataFrame({'Reconstruction_error': mse,
                         'True_class': y_test})

# Confusion Matrix
pred_y = [1 if e > threshold else 0 for e in error_df.Reconstruction_error.values]
conf_matrix = confusion_matrix(error_df.True_class, pred_y)

plt.figure(figsize=(5, 5))
sns.heatmap(conf_matrix, xticklabels=LABELS, yticklabels=LABELS, annot=True, fmt="d")
plt.title("Confusion matrix")
plt.ylabel('True class')
plt.xlabel('Predicted class')
plt.show()

请建议在模型中可以做些什么来提高准确性。

4

1 回答 1

1

如果您的模型在测试集上表现不佳,我会确保检查某些内容;

  • 训练集没有被异常或来自测试集的任何信息污染。如果您使用缩放,请确保您没有将缩放器适合训练和测试集的组合。
  • 根据我的经验;如果自动编码器不能很好地区分测试数据但训练损失很低,前提是你的训练集是纯的,这意味着自动编码器确实了解了训练集的底层细节,但没有了解一般化的想法。
  • 您的阈值可能已关闭,您可能需要提出更好的阈值处理程序。一个例子可以在这里找到:https ://dl.acm.org/citation.cfm?doid=3219819.3219845

如果问题是第二个,解决方案是增加泛化。对于自动编码器,最有效的泛化工具之一是瓶颈的维度。再次基于我在飞行雷达数据异常检测方面的经验;降低瓶颈维度显着提高了我的多类分类精度。我使用了 encoding_dim 为 7 的 14 个功能,但 encoding_dim 为 4 提供了更好的结果。在我的情况下,训练损失的值并不重要,因为我只是比较重建错误,但是由于您正在使用 RE 的阈值进行分类,因此可以使用更稳健的阈值来提高准确性,就像在论文中一样我已经分享过了。

于 2019-11-08T10:39:27.640 回答