使用seq2seq进行时间序列预测

时间:2019-06-20 22:29:58

标签: python machine-learning keras time-series seq2seq

我目前正在使用seq2seq模型对呼叫次数进行类似的时间序列预测。现在,我遇到一个问题,那就是当我想预测未来30天目标值时,结果将不会呈现任何系列趋势。

<pre>
# train model
latent_dim = 128 # LSTM hidden units
dropout = .20 

encoder_inputs = Input(shape=(None, n_inputs), 
                       name='encoder_inputs') 

encoder_lstm = LSTM(latent_dim, dropout=dropout, 
                    return_sequences=False, return_state=True, 
                    name='encoder_lstm')

_, state_h, state_c = encoder_lstm(encoder_inputs)

encoder_states = [state_h, state_c]


decoder_inputs = Input(shape=(None, n_features), 
                       name='decoder_inputs') 


decoder_lstm = LSTM(latent_dim, dropout=dropout, 
                    return_sequences=True, return_state=True, 
                    name='decoder_lstm')
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, 
                                     initial_state=encoder_states)

decoder_dense = (Dense(n_outputs, name='decoder_dense', 
                       activation='linear')) 
decoder_outputs = decoder_dense(decoder_outputs)

model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

model.compile(COCOB(), loss=ks.losses.mean_squared_error)

history = model.fit([encoder_input_data, decoder_input_data], 
                    decoder_target_data,
                    batch_size=16,  epochs=100,
                    validation_split=0.2, shuffle=False)

# define inference encoder
encoder_model = Model(encoder_inputs, encoder_states)

decoder_state_input_h = Input(shape=(latent_dim,))
decoder_state_input_c = Input(shape=(latent_dim,))
decoder_states_inputs = [decoder_state_input_h,       
                         decoder_state_input_c]  

decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, 
                                initial_state=decoder_states_inputs)

decoder_states = [state_h, state_c] 

decoder_outputs = decoder_dense(decoder_outputs) 
decoder_model = Model([decoder_inputs] + decoder_states_inputs,
                      [decoder_outputs] + decoder_states)

# decoded sequence 
n_test_samples = encoder_input_data_test.shape[0]
print('Total number of test smaples:', n_test_samples)

n_seq_out = 1  #length of days for predicting, objective is to 
# predict 3 months in the future

all_out = []

for i in range(n_test_samples):
    enc_outs = 
    encoder_model.predict(encoder_input_data_test[i:i+1,:,:])

    target_seq = np.zeros((1,1,n_features))

    target_seq[0,:,:] = encoder_input_data_test[i,-1:,:]

    decoded_seq = np.zeros((1,n_seq_out,n_outputs))

    for j in range(n_seq_out):
        output, h, c = decoder_model.predict([target_seq]+enc_outs)
        print('out', output)
        print('tar', target_seq)
        decoded_seq[0,j,0] = output[0,0,0] 

        target_seq = np.zeros((1,1,n_features))
        target_seq[0,0,0] = output[0,0,0]
        target_seq[0,0,1:] = decoder_input_data_test[j+1,0,1:]
        enc_outs = [h, c]  

    all_out.append(decoded_seq)

我的编码器数据输入的大小为(n_samples,步长= 84天,n_features),解码器输入的数据为大小(n_samples,步数= 1天,n_features),解码器目标数据的大小为(n_sample,步骤= 1,1)。编码器输入数据的每个样本都有一天的延迟,解码器输入数据用于实现教师强迫。

就像我只是在一天后进行预测一样,预测值是可以接受的,但不是很精确。但是,当我预测30天后,预测序列的值将无法阻止地增加或减少。

  1. 那么预测未来价值有什么问题吗?
  2. 我的功能包括目标的一列:呼叫次数,其他功能是外部功能(日,月,周日,假日..),将所有这些功能整合到模型中是个好主意吗?
  3. 在第三部分代码(解码顺序)中,我还需要将这些外部功能添加到教师强制中,对吗?

非常感谢!

0 个答案:

没有答案