gpt4 book ai didi

python - 时间序列的 seq2seq 预测

转载 作者:行者123 更新时间:2023-12-01 09:21:05 28 4
gpt4 key购买 nike

我想制作一个 Seq2Seq 模型用于重建目的。我想要一个经过训练的模型来重建正常时间序列,并且假设这样的模型在重建训练期间没有看到异常时间序列时效果会很差。

我的代码和理解上都存在一些差距。我以此为方向,到目前为止已经做到了:traindata:input_data.shape(1000,60,1)和target_data.shape(1000,50,1),目标数据是相同的训练数据,只是按照论文here中建议的相反顺序。用于推理:我想使用形状为 (3000,60,1) 的训练模型来预测另一个时间序列数据。现在有 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

最佳答案

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 单元。

关于python - 时间序列的 seq2seq 预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50784540/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com