赞
踩
已经写了很久啦,因为工作原因,后边可能不会来维护和回复评论了,谢谢。
这篇帖子只是提出了一点 lstm 的时间序列预测方法和疑问,抛砖引玉!
网上搜索吗,比如别人做过的比较典型的有:
1. https://blog.csdn.net/aliceyangxi1987/article/details/73420583
2. https://www.cnblogs.com/mtcnn/p/9411597.html
3. https://cloud.tencent.com/developer/article/1083338 等等
(因为按照他们这种做法,预测未来是没有这个所谓的 一个X 来喂给训练好的模型的?)
《《《-----------------------------滑动窗口的LSTM方法预测:----------------------------------------------------------------------------》》》
前面描述的都是比如一个时间序列的value为
Y :[10, 20, 30, 40, 50, 60, 70, 80, 90] 他们会为其构造一条X 按照逐个t-1时刻的X对应逐个t时刻Y值。
X:[0, 10, 20, 30, 40, ......] 具体参考上述链接做法。
然后再利用构造的X,Y训练lstm模型,达到预测的目地。
但是这个做法,无法预测未来。
经过搜索,以上方法是数据LSTM中sequence to sequence 的方法的。一对一,适用于中英文翻译方法!!
或者一对一预测等等,不是预测未来的方法~ 所以前面那些链接中,应该是对sequence to sequence有点误会!
因此,我们应该采用滑动窗口的数据预测未来一个数据,sequence to vector的,可以根据前面几个预测下一个,比如下面:
比如X[t-3,t-2,t-1] 预测X[t], 此时X[t]可以当作标签Y
针对上述数据我们可以构造 (左为X, 右为Y ):
[10,20,30], [40]
[20,30,40], [50]
[30,40,50], [60]
.......................
这样依赖,利用多个滑动窗口(此处为3)来预测下一个数据的目地。。。。
预测未来如何逐个添加? 答:唯有预测一个,往后添加一个,再加入预测。
缺点:误差会积累!
《《《-----------------------------滑动窗口的LSTM方法python实例:------------------------------------------------》》》
- from numpy import array
- from keras.models import Sequential
- from keras.layers import LSTM
- from keras.layers import Dense
-
-
- '''
- 下面的split_sequence()函数实现了这种行为,并将给定的单变量序列分成多个样本,其中每个样本具有指定的时间步长,输出是单个时间步。
- '''
- # split a univariate sequence into samples
- def split_sequence(sequence, n_steps):
- X, y = list(), list()
- for i in range(len(sequence)):
- # find the end of this pattern
- end_ix = i + n_steps
- # check if we are beyond the sequence
- if end_ix > len(sequence)-1:
- break
- # gather input and output parts of the pattern
- seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
- X.append(seq_x)
- y.append(seq_y)
- return array(X), array(y)
-
-
- if __name__ == '__main__':
-
- # define input sequence
- raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
- print raw_seq
- # choose a number of time steps
- n_steps = 3
- # split into samples
- X, y = split_sequence(raw_seq, n_steps)
- print X, y
- # reshape from [samples, timesteps] into [samples, timesteps, features]
- n_features = 1
- X = X.reshape((X.shape[0], X.shape[1], n_features))
- # define model
- model = Sequential()
- model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features))) # 隐藏层,输入,特征维
- model.add(Dense(1))
- model.compile(optimizer='adam', loss='mse')
- # fit model
- model.fit(X, y, epochs=300, batch_size=1, verbose=2) # 迭代次数,批次数,verbose决定是否显示每次迭代
- # demonstrate prediction
- x_input = array([70, 80, 90])
- x_input = x_input.reshape((1, n_steps, n_features))
- yhat = model.predict(x_input, verbose=0)
- print x_input, yhat
- print(yhat)

预测未来?
比如[70,80,90] 预测一个假如为[100]
可以继续构造[80,90,100], 预测下一个。。。。
等等....
本文方法灵感参考转载于:
https://machinelearningmastery.com/how-to-develop-lstm-models-for-time-series-forecasting/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。