【发布时间】:2018-06-10 14:08:05
【问题描述】:
我想制作一个用于重建目的的 Seq2Seq 模型。我想要一个经过训练的模型来重建正常的时间序列,并且假设这样的模型在重建训练期间没有看到它们的异常时间序列方面做得很糟糕。
我的代码和理解上都有一些差距。我把这个作为一个方向,到目前为止做了: traindata:input_data.shape(1000,60,1) 和 target_data.shape(1000,50,1),其中目标数据是相同的训练数据,只是按照论文 here 中的建议相反的顺序。 推理:我想用形状为 (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
【问题讨论】:
-
我有一个序列到序列预测的类似问题。我总共有 9000 个形状为 (9000, 200, 1) 的样本。在分成训练集和测试集后,例如形状 (6000,200,1) 和 (3000, 200, 1)。您是否在 while 循环中做了任何额外的事情,以及您如何在代码中调用 predict_sequence?
-
@annulstudent93 我在 stackoverflow 上发布了一个类似的问题。 stackoverflow.com/questions/58491120/… 但我没有得到任何答案。您能帮我在代码中如何以及在何处调用 predict_sequence 函数。你在 while 循环中还做了什么?
标签: python tensorflow keras time-series lstm