我'm currently trying to implement an LSTM autoencoder to be used in order allow compression of transactions timeseries (Berka dataset) into a smaller encoded vector. The data I' m看起来像this(这是一个帐户的累计余额) .
我决定使用Keras,我尝试在this教程之后创建一个简单的自动编码器 . 该模型不起作用 .
我的代码是这样的:
import keras
from keras import Input, Model
from keras.layers import Lambda, LSTM, RepeatVector
from matplotlib import pyplot as plt
from scipy import io
from sklearn.preprocessing import MinMaxScaler
import numpy as np
class ResultPlotter(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
plt.subplots(2, 2, figsize=(10, 3))
indexes = np.random.randint(datapoints, size=4)
for i in range(4):
plt.subplot(2, 2, i+1)
plt.plot(sparse_balances[indexes[i]])
result = sequence_autoencoder.predict(sparse_balances[0:1])
plt.plot(result.T)
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.show()
return
result_plotter = ResultPlotter()
sparse_balances = io.mmread("my_path_to_sparse_balances.mtx")
sparse_balances = sparse_balances.todense()
scaler = MinMaxScaler(feature_range=(0, 1))
sparse_balances = scaler.fit_transform(sparse_balances)
N = sparse_balances.shape[0]
D = sparse_balances.shape[1]
batch_num = 32
timesteps = 500
latent_dim = 32
datapoints = N
model_inputs = Input(shape=(timesteps,))
inputs = Lambda(lambda x: keras.backend.expand_dims(x, -1))(model_inputs)
encoded = LSTM(latent_dim)(inputs)
decoded = RepeatVector(timesteps)(encoded)
decoded = LSTM(1, return_sequences=True)(decoded)
decoded = Lambda(lambda x: keras.backend.squeeze(x, -1))(decoded)
sequence_autoencoder = Model(model_inputs, decoded)
encoder = Model(model_inputs, encoded)
earlyStopping = keras.callbacks.EarlyStopping(monitor='loss', patience=5, verbose=0, mode='auto')
sequence_autoencoder.compile(loss='mean_squared_error', optimizer='adam')
sequence_autoencoder.fit(sparse_balances[:datapoints], sparse_balances[:datapoints],
batch_size=batch_num, epochs=100,
callbacks=[earlyStopping, result_plotter])
我没有添加生成sparse_balanced.mtx的代码以保持一切清晰,随意请求它,我会发布它 .
问题是自动编码器似乎停留在预测single line上,而不是返回紧跟输入趋势的输出,但经过广泛的研究后,我仍然需要找到解决方案 . 我做了一些实验,使用密集层作为模型的潜在输出部分,并且它能够返回much better results .
那么问题是:鉴于通过使用LSTM->密集或密集 - >密集自动编码器,我能够获得不错的结果,并且使用密集 - > LSTM和LSTM-> LSTM导致相同的错误预测,是我的模型,概念或其他地方的问题?
非常感谢每一条评论,谢谢 .