首页 > 解决方案 > 时间序列的 seq2seq 预测

问题描述

我想为重建目的制作一个 Seq2Seq 模型。我想要一个经过训练的模型来重建正常的时间序列,并且假设这样的模型在重建训练期间没有看到它们的异常时间序列方面会做得很糟糕。

我的代码和理解上都有一些差距。我把它作为一个方向,到目前为止:traindata: input_data.shape(1000,60,1) 和 target_data.shape(1000,50,1) 目标数据是相同的训练数据,只是按照相反的顺序,如纸在这里。推理:我想用形状为 (3000,60,1) 的训练模型预测另一个时间序列数据。T 现在有 2 个点是开放的:如何为我的训练模型指定输入数据以及如何使用停止条件构建推理部分?请纠正任何错误。

from keras.models import Model
from keras.layers import Input
from keras.layers import LSTM
from keras.layers import Dense

num_encoder_tokens = 1#number of features
num_decoder_tokens = 1#number of features
encoder_seq_length = None
decoder_seq_length = None
batch_size = 50
epochs = 40

# same data for training 
input_seqs=()#shape (1000,60,1) with sliding windows
target_seqs=()#shape(1000,60,1) with sliding windows but reversed
x= #what has x to be ?

#data for inference 
# how do I specify the input data for my other time series ?

# Define training model
encoder_inputs = Input(shape=(encoder_seq_length,
                          num_encoder_tokens))
encoder = LSTM(128, return_state=True, return_sequences=True)
encoder_outputs = encoder(encoder_inputs)
_, encoder_states = encoder_outputs[0], encoder_outputs[1:]

decoder_inputs = Input(shape=(decoder_seq_length,
                          num_decoder_tokens))
decoder = LSTM(128, return_sequences=True)
decoder_outputs = decoder(decoder_inputs, initial_state=encoder_states)
decoder_outputs = TimeDistributed(
Dense(num_decoder_tokens, activation='tanh'))(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

# Training
model.compile(optimizer='adam', loss='mse')
model.fit([input_seqs,x], target_seqs,
      batch_size=batch_size, epochs=epochs)


# Define sampling models for inference
encoder_model = Model(encoder_inputs, encoder_states)

decoder_state_input_h = Input(shape=(100,))
decoder_state_input_c = Input(shape=(100,))
decoder_states = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs = decoder(decoder_inputs,
                      initial_state=decoder_states)
decoder_model = Model([decoder_inputs] + decoder_states,
decoder_outputs)

# Sampling loop for a batch of sequences
states_values = encoder_model.predict(input_seqs)
stop_condition = False
while not stop_condition:
    output_tokens = decoder_model.predict([target_seqs] + states_values)
#what else do I need to include here ? 
    break

标签: pythontensorflowkerastime-serieslstm

解决方案


def predict_sequence(infenc, infdec, source, n_steps, cardinality):
    # encode
    state = infenc.predict(source)
    # start of sequence input
    target_seq = array([0.0 for _ in range(cardinality)]).reshape(1, 1, cardinality)
    # collect predictions
    output = list()
    for t in range(n_steps):
        # predict next char
        yhat, h, c = infdec.predict([target_seq] + state)
        # store prediction
        output.append(yhat[0,0,:])
        # update state
        state = [h, c]
        # update target sequence
        target_seq = yhat
    return array(output)

您可以看到每个时间步的输出都从外部反馈到 LSTM 单元。


推荐阅读