LSTM自编码器,使用第一个LSTM输出作为解码器的目标

拥有一个包含10天传感器事件的序列,以及一个真/假标签,用于指定传感器是否在10天内触发了警报:

sensor_id timestamp feature_1 feature_2 10_days_alert_label
1 2020-12-20 01:00:34.565 0.23 0.1 1
1 2020-12-20 01:03:13.897 0.3 0.12 1
2 2020-12-20 01:00:34.565 0.13 0.4 0
2 2020-12-20 01:03:13.897 0.2 0.9 0

95%的传感器不会触发警报,因此数据是不平衡的。我在考虑使用自编码器模型来检测异常(触发警报的传感器)。由于我对解码整个序列不感兴趣,只对LSTM学习到的上下文向量感兴趣,我考虑了如下的图示,其中解码器重构编码器的输出:

enter image description here

我通过谷歌搜索找到了这个简单的LSTM自编码器示例:

# lstm autoencoder recreate sequencefrom numpy import arrayfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import LSTMfrom tensorflow.keras.layers import Densefrom tensorflow.keras.layers import RepeatVectorfrom tensorflow.keras.layers import TimeDistributedfrom tensorflow.keras.utils import plot_model# define input sequencesequence = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])# reshape input into [samples, timesteps, features]n_in = len(sequence)sequence = sequence.reshape((1, n_in, 1))# define modelmodel = Sequential()model.add(LSTM(100, activation='relu', input_shape=(n_in,1)))model.add(RepeatVector(n_in))model.add(LSTM(100, activation='relu', return_sequences=True))model.add(TimeDistributed(Dense(1)))model.compile(optimizer='adam', loss='mse')# fit modelmodel.fit(sequence, sequence, epochs=300, verbose=0)plot_model(model, show_shapes=True, to_file='reconstruct_lstm_autoencoder.png')# demonstrate recreationyhat = model.predict(sequence, verbose=0)print(yhat[0,:,0])

我想修改上面的示例,以便将第一个LSTM的输出用作解码器的目标。类似于:

# lstm autoencoder recreate sequencefrom numpy import arrayfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import LSTMfrom tensorflow.keras.layers import Densefrom tensorflow.keras.layers import RepeatVectorfrom tensorflow.keras.layers import TimeDistributedfrom tensorflow.keras.utils import plot_model# define input sequencesequence = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])# reshape input into [samples, timesteps, features]n_in = len(sequence)sequence = sequence.reshape((1, n_in, 1))# define modelmodel = Sequential()model.add(LSTM(100, activation='relu', input_shape=(n_in,1)))model.add(Dense(100, activation='relu')) # First LSTM outputmodel.add(Dense(32, activation='relu')) # Bottleneck model.add(Dense(100, activation='sigmoid')) # Decoded vectormodel.compile(optimizer='adam', loss='mse')# fit modelmodel.fit(sequence, FIRST_LSTM_OUTPUT, epochs=300, verbose=0) # <--- ???

Q: 我可以使用第一个LSTM输出向量作为目标吗?


回答:

你可以使用model.add_loss来实现。在add_loss中我们指定我们感兴趣的损失(在我们的例子中是mse),并设置用于计算它的层(在我们的例子中是LSTM输出和模型预测)

下面是一个虚构的示例:

n_sample, timesteps = 100, 9X = np.random.uniform(0,1, (100, 9, 1))def mse(enc_output, pred):    return  tf.reduce_mean(tf.square(enc_output - pred))    inp = Input((timesteps,1,))enc = LSTM(100, activation='relu')(inp)x = Dense(100, activation='relu')(enc)x = Dense(32, activation='relu')(x)out = Dense(100, activation='sigmoid')(x)model = Model(inp, out)model.add_loss(mse(enc, out))model.compile(optimizer='adam', loss=None)model.fit(X, y=None, epochs=3)

这里是运行代码

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注