Keras LSTM自动编码器:简单的顺序输入到顺序输出

时间:2019-05-28 09:46:09

标签: python keras deep-learning lstm autoencoder

我有以下示例3d形状数据集

    X_train =  [[array([[0.32225183]]),
      array([[0.28631562]]),
      array([[0.30552849]]),
      array([[0.33142298]]),
      array([[0.28376102]])],
     [array([[0.28631562]]),
      array([[0.30552849]]),
      array([[0.33142298]]),
      array([[0.28376102]]),
      array([[0.20635083]])],
     [array([[0.30552849]]),
      array([[0.33142298]]),
      array([[0.28376102]]),
      array([[0.20635083]]),
      array([[0.26855856]])]]

以上数据的形状为5个时间戳/序列,1个特征和3个样本。

我正在尝试运行一个简单的LSTM Autoencoder用于时间序列异常检测,因此输入是一个序列,输出将是一个序列,重构错误可以告诉我有关特定时间的异常。

    import numpy as np 
    import pandas as pd 
    import matplotlib.pyplot as plt


    from numpy import array
    from keras.models import Sequential
    from keras.layers import LSTM, Dense, RepeatVector, TimeDistributed
    from keras.utils import plot_model
    from keras import optimizers
    from keras.callbacks import EarlyStopping


    lstm_autoencoder = Sequential()
##EncodingPart
    lstm_autoencoder.add(LSTM(units = 10, activation='relu', input_shape=(5, 1), return_sequences=True))

##DecodingPart

    lstm_autoencoder.add(LSTM(5, activation='relu', return_sequences=True))
    lstm_autoencoder.summary()

## Compile
    lstm_autoencoder.compile(loss='mse', optimizer='adam')
    early_stop = EarlyStopping(monitor='loss', patience=2, verbose=1) 
    lstm_autoencoder_history = lstm_autoencoder.fit(X_train, X_train, 
                                                epochs = 100, 
                                                batch_size = 10, 
                                                verbose=2, callbacks=[early_stop])

通常,我在fitnpredict之后对输出进行展平并比较重构错误。

但这给了我以下错误

ValueError: Error when checking target: expected lstm_8 to have shape (5, 5) but got array with shape (5, 1)

请帮助我获得此简单的顺序输出

0 个答案:

没有答案