拥有一个包含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学习到的上下文向量感兴趣,我考虑了如下的图示,其中解码器重构编码器的输出:
我通过谷歌搜索找到了这个简单的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)
这里是运行代码