LSTM 自动编码器,使用第一个 LSTM 输出作为解码器的目标 [英] LSTM Auto Encoder, use first LSTM output as the target for the decoder

查看:138
本文介绍了LSTM 自动编码器,使用第一个 LSTM 输出作为解码器的目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有 10 天的传感器事件序列和真/假标签,指定传感器是否在 10 天持续时间内触发警报:

Having a sequence of 10 days of sensors events, and a true / false label, specifying if the sensor triggered an alert within the 10 days duration:

<头>
sensor_id时间戳功能_1功能_210_days_alert_label
12020-12-20 01:00:34.5650.230.11
12020-12-20 01:03:13.8970.30.121
22020-12-20 01:00:34.5650.130.40
22020-12-20 01:03:13.8970.20.90

95% 的传感器不会触发警报,因此数据不平衡.我正在考虑使用 autoEncoder 模型来检测异常(触发警报的传感器).由于我对解码整个序列不感兴趣,只对 LSTM 学习的上下文向量感兴趣,我在想像下图这样的东西,其中解码器正在重建编码器输出:

95% of the sensors do not trigger an alert, therefore the data is imbalanced. I was thinking of an autoEncoder model in order to detect the anomalies (Sensors that triggered an alarm). Since I'm not interested in decoding the entire sequence, just the LSTM learned context vector, I was thinking of something like the figure below, where the decoder is reconstructing the encoder output:

我在谷歌上搜索并找到了这个简单的 LSTM 自动编码器示例:

I've googled around and found this simple LSTM auto encoder example:

# lstm autoencoder recreate sequence
from numpy import array
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import RepeatVector
from tensorflow.keras.layers import TimeDistributed
from tensorflow.keras.utils import plot_model
# define input sequence
sequence = 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 model
model = 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 model
model.fit(sequence, sequence, epochs=300, verbose=0)
plot_model(model, show_shapes=True, to_file='reconstruct_lstm_autoencoder.png')
# demonstrate recreation
yhat = model.predict(sequence, verbose=0)
print(yhat[0,:,0])


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


I would like to modify the above example so the first LSTM output is used as the decoder target. Something like:

# lstm autoencoder recreate sequence
from numpy import array
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import RepeatVector
from tensorflow.keras.layers import TimeDistributed
from tensorflow.keras.utils import plot_model
# define input sequence
sequence = 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 model
model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(n_in,1)))

model.add(Dense(100, activation='relu')) # First LSTM output
model.add(Dense(32, activation='relu')) # Bottleneck 
model.add(Dense(100, activation='sigmoid')) # Decoded vector

model.compile(optimizer='adam', loss='mse')

# fit model
model.fit(sequence, FIRST_LSTM_OUTPUT, epochs=300, verbose=0) # <--- ???

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

Q: Can I use the first LSTM output vector as a target?

推荐答案

您可以使用 model.add_loss 来完成.在 add_loss 中,我们指定了我们的兴趣损失(在我们的例子中:mse)并设置用于计算它的层(在我们的例子中:LSTM 输出和模型预测)

You can do it using model.add_loss. In add_loss we specify the loss of our interest (in our case: mse) and set the layers used to compute it (in our case: the LSTM output and model predictions)

下面是一个虚拟示例:

n_sample, timesteps = 100, 9
X = 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)

这里运行代码

这篇关于LSTM 自动编码器,使用第一个 LSTM 输出作为解码器的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆