我想制作一个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
答案 0 :(得分:1)
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单元。
答案 1 :(得分:0)
您可以参考博客,并在推理过程中了解它是如何完成的。
https://machinelearningmastery.com/develop-encoder-decoder-model-sequence-sequence-prediction-keras/
答案 2 :(得分:0)
在训练期间,我们以一次性方式提供数据。我想你明白那一部分。
但在推理期间,我们不能这样做。我们必须在每个时间步骤给出数据然后返回单元格状态,隐藏状态和循环应该继续直到生成最后一个单词