https://blog.csdn.net/flying_sfeng/article/details/78852816
版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/Flying_sfeng/article/details/78852816
這篇文章將講解如何使用lstm進行時間序列方面的預測,重點講lstm的應用,原理部分可參考以下兩篇文章:
Understanding LSTM Networks LSTM學習筆記
編程環境:python3.5,tensorflow 1.0
本文所用的數據集來自於kesci平台,由雲腦機器學習實戰訓練營提供:真實業界數據的時間序列預測挑戰
數據集采用來自業界多組相關時間序列(約40組)與外部特征時間序列(約5組)。本文只使用其中一組數據進行建模。
加載常用的庫:
-
#加載數據分析常用庫
-
import pandas as pd
-
import numpy as np
-
import tensorflow as tf
-
from sklearn.metrics import mean_absolute_error,mean_squared_error
-
from sklearn.preprocessing import MinMaxScaler
-
import matplotlib.pyplot as plt
-
% matplotlib inline
-
import warnings
-
warnings.filterwarnings( 'ignore')
數據顯示:
-
path = '../input/industry/industry_timeseries/timeseries_train_data/11.csv'
-
data11 = pd.read_csv(path,names=[ '年','月','日','當日最高氣溫','當日最低氣溫','當日平均氣溫','當日平均濕度','輸出'])
-
data11.head()
年 | 月 | 日 | 當日最高氣溫 | 當日最低氣溫 | 當日平均氣溫 | 當日平均濕度 | 輸出 | |
---|---|---|---|---|---|---|---|---|
0 | 2015 | 2 | 1 | 1.9 | -0.4 | 0.7875 | 75.000 | 814.155800 |
1 | 2015 | 2 | 2 | 6.2 | -3.9 | 1.7625 | 77.250 | 704.251112 |
2 | 2015 | 2 | 3 | 7.8 | 2.0 | 4.2375 | 72.750 | 756.958978 |
3 | 2015 | 2 | 4 | 8.5 | -1.2 | 3.0375 | 65.875 | 640.645401 |
4 | 2015 | 2 | 5 | 7.9 | -3.6 | 1.8625 | 55.375 | 631.725130 |
加載數據:
-
##load data(本文以第一個表為例,其他表類似,不再贅述)
-
f=open( '../input/industry/industry_timeseries/timeseries_train_data/11.csv')
-
df=pd.read_csv(f) #讀入數據
-
data=df.iloc[:, 3:8].values #取第3-7列
定義常量並初始化權重:
-
#定義常量
-
rnn_unit= 10 #hidden layer units
-
input_size= 4
-
output_size= 1
-
lr= 0.0006 #學習率
-
tf.reset_default_graph()
-
#輸入層、輸出層權重、偏置
-
weights={
-
'in':tf.Variable(tf.random_normal([input_size,rnn_unit])),
-
'out':tf.Variable(tf.random_normal([rnn_unit,1]))
-
}
-
biases={
-
'in':tf.Variable(tf.constant(0.1,shape=[rnn_unit,])),
-
'out':tf.Variable(tf.constant(0.1,shape=[1,]))
-
}
分割數據集,將數據分為訓練集和驗證集(最后90天做驗證,其他做訓練):
-
def get_data(batch_size=60,time_step=20,train_begin=0,train_end=487):
-
batch_index=[]
-
-
scaler_for_x=MinMaxScaler(feature_range=( 0,1)) #按列做minmax縮放
-
scaler_for_y=MinMaxScaler(feature_range=( 0,1))
-
scaled_x_data=scaler_for_x.fit_transform(data[:,: -1])
-
scaled_y_data=scaler_for_y.fit_transform(data[:, -1])
-
-
label_train = scaled_y_data[train_begin:train_end]
-
label_test = scaled_y_data[train_end:]
-
normalized_train_data = scaled_x_data[train_begin:train_end]
-
normalized_test_data = scaled_x_data[train_end:]
-
-
train_x,train_y=[],[] #訓練集x和y初定義
-
for i in range(len(normalized_train_data)-time_step):
-
if i % batch_size==0:
-
batch_index.append(i)
-
x=normalized_train_data[i:i+time_step,: 4]
-
y=label_train[i:i+time_step,np.newaxis]
-
train_x.append(x.tolist())
-
train_y.append(y.tolist())
-
batch_index.append((len(normalized_train_data)-time_step))
-
-
size=(len(normalized_test_data)+time_step -1)//time_step #有size個sample
-
test_x,test_y=[],[]
-
for i in range(size-1):
-
x=normalized_test_data[i*time_step:(i+ 1)*time_step,:4]
-
y=label_test[i*time_step:(i+ 1)*time_step]
-
test_x.append(x.tolist())
-
test_y.extend(y)
-
test_x.append((normalized_test_data[(i+ 1)*time_step:,:4]).tolist())
-
test_y.extend((label_test[(i+ 1)*time_step:]).tolist())
-
-
return batch_index,train_x,train_y,test_x,test_y,scaler_for_y
定義LSTM的網絡結構:
-
#——————————————————定義神經網絡變量——————————————————
-
def lstm(X):
-
batch_size=tf.shape(X)[ 0]
-
time_step=tf.shape(X)[ 1]
-
w_in=weights[ 'in']
-
b_in=biases[ 'in']
-
input=tf.reshape(X,[ -1,input_size]) #需要將tensor轉成2維進行計算,計算后的結果作為隱藏層的輸入
-
input_rnn=tf.matmul(input,w_in)+b_in
-
input_rnn=tf.reshape(input_rnn,[ -1,time_step,rnn_unit]) #將tensor轉成3維,作為lstm cell的輸入
-
cell=tf.contrib.rnn.BasicLSTMCell(rnn_unit)
-
#cell=tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(rnn_unit)
-
init_state=cell.zero_state(batch_size,dtype=tf.float32)
-
output_rnn,final_states=tf.nn.dynamic_rnn(cell, input_rnn,initial_state=init_state, dtype=tf.float32) #output_rnn是記錄lstm每個輸出節點的結果,final_states是最后一個cell的結果
-
output=tf.reshape(output_rnn,[ -1,rnn_unit]) #作為輸出層的輸入
-
w_out=weights[ 'out']
-
b_out=biases[ 'out']
-
pred=tf.matmul(output,w_out)+b_out
-
return pred,final_states
模型訓練與預測:
-
#——————————————————訓練模型——————————————————
-
def train_lstm(batch_size=80,time_step=15,train_begin=0,train_end=487):
-
X=tf.placeholder(tf.float32, shape=[ None,time_step,input_size])
-
Y=tf.placeholder(tf.float32, shape=[ None,time_step,output_size])
-
batch_index,train_x,train_y,test_x,test_y,scaler_for_y = get_data(batch_size,time_step,train_begin,train_end)
-
pred,_=lstm(X)
-
#損失函數
-
loss=tf.reduce_mean(tf.square(tf.reshape(pred,[ -1])-tf.reshape(Y, [-1])))
-
train_op=tf.train.AdamOptimizer(lr).minimize(loss)
-
with tf.Session() as sess:
-
sess.run(tf.global_variables_initializer())
-
#重復訓練5000次
-
iter_time = 5000
-
for i in range(iter_time):
-
for step in range(len(batch_index)-1):
-
_,loss_=sess.run([train_op,loss],feed_dict={X:train_x[batch_index[step]:batch_index[step+ 1]],Y:train_y[batch_index[step]:batch_index[step+1]]})
-
if i % 100 == 0:
-
print( 'iter:',i,'loss:',loss_)
-
####predict####
-
test_predict=[]
-
for step in range(len(test_x)):
-
prob=sess.run(pred,feed_dict={X:[test_x[step]]})
-
predict=prob.reshape(( -1))
-
test_predict.extend(predict)
-
-
test_predict = scaler_for_y.inverse_transform(test_predict)
-
test_y = scaler_for_y.inverse_transform(test_y)
-
rmse=np.sqrt(mean_squared_error(test_predict,test_y))
-
mae = mean_absolute_error(y_pred=test_predict,y_true=test_y)
-
print ('mae:',mae,' rmse:',rmse)
-
return test_predict
調用train_lstm()函數,完成模型訓練與預測的過程,並統計驗證誤差(mae和rmse):
test_predict = train_lstm(batch_size=80,time_step=15,train_begin=0,train_end=487)
迭代5000次后的結果:
-
iter: 3900 loss: 0.000505382
-
iter: 4000 loss: 0.000502154
-
iter: 4100 loss: 0.000503413
-
iter: 4200 loss: 0.00140424
-
iter: 4300 loss: 0.000500015
-
iter: 4400 loss: 0.00050004
-
iter: 4500 loss: 0.000498159
-
iter: 4600 loss: 0.000500861
-
iter: 4700 loss: 0.000519379
-
iter: 4800 loss: 0.000499999
-
iter: 4900 loss: 0.000501265
-
mae: 121.183626208 rmse: 162.049017904
畫圖分析:
-
plt.figure(figsize=( 24,8))
-
plt.plot(data[:, -1])
-
plt.plot([ None for _ in range(487)] + [x for x in test_predict])
-
plt.show()
結果如下:

可以看到,lstm模型基本能預測出序列的趨勢。
為了簡化流程,本文在特征工程及參數調優方面並沒有下功夫,適合初學者探索lstm模型在時間序列問題上的應用。
ps:數據的歸一化很重要,必須保證把訓練集跟驗證集規范在同一個空間內,否則得到的效果會很差。(我以前做天池的降雨量預測問題時一開始用的就是lstm,就是這一步沒做好,導致最后得到的結果基本很相近,最后這個模型被我放棄了。我在做這個數據集的時候一開始也遇到這個問題,后來在歸一化時把樣本都設置在同個空間范疇,就解決問題了)。
數據集提供了大概45組數據,所以我們可以使用multi-task learning探索各組數據之間的關聯性,這部分我還沒具體了解,就不貽笑大方了。
本文建模的框架來自於:
Tensorflow實例:利用LSTM預測股票每日最高價(二)
--------------------- 本文來自 Flying_sfeng 的CSDN 博客 ,全文地址請點擊:https://blog.csdn.net/flying_sfeng/article/details/78852816?utm_source=copy