時間序列(單個時序+預測下一步)
阿新 • • 發佈:2018-12-24
""" Created on Wed Dec 19 21:02:24 2018 @author: 87671 """ ''' Univariate LSTM Models(只有一個時間序列) 參考連結:https://machinelearningmastery.com/how-to-develop-lstm-models-for-time-series-forecasting/ ''' # 定義切割函式 from numpy import array def split_sequence(sequence,n_steps): X,y = list(),list() for i in range(len(sequence)): end_ix = i + n_steps # 也就是y if end_ix > len(sequence)-1: break seq_x, seq_y = sequence[i:end_ix],sequence[end_ix] X.append(seq_x) y.append(seq_y) return array(X),array(y) #舉例 raw_seq=[10,20,30,40,50,60,70,80,90] n_steps = 3 X,y = split_sequence(raw_seq,n_steps) for i in range(len(X)): print(X[i],y[i]) ''' 結果如下,也就是想用前3步的資訊去預測後一步的資訊 [10 20 30] 40 [20 30 40] 50 [30 40 50] 60 [40 50 60] 70 [50 60 70] 80 [60 70 80] 90 ''' # 編譯 from keras.models import Sequential from keras.layers import LSTM from keras.layers import Dense ###########################################第一個沒有任何累加的LSTM n_features = 1 X = X.reshape((X.shape[0],X.shape[1],n_features)) # 轉換成(樣本量,時間步,資料維度) model = Sequential() model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features))) # n_features=1,因為是單變數 model.add(Dense(1)) model.compile(optimizer='adam',loss='mse') model.fit(X,y,epochs=200,verbose=0) # 預測 x_input = array([70,80,90]) x_input = x_input.reshape((1,n_steps,n_features)) yhat = model.predict(x_input,verbose=0) print(yhat) # 101.529 ##########################################累加一層的LSTM return_sequences=True model = Sequential() model.add(LSTM(50, activation='relu', return_sequences=True, input_shape=(n_steps, n_features))) model.add(LSTM(50, activation='relu')) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, y, epochs=200, verbose=0) # 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(yhat) # 103.504944 #########################################雙向LSTM(利用的前後的資訊去學習) from keras.layers import Bidirectional model = Sequential() model.add(Bidirectional(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=200, verbose=0) # 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(yhat) ##########################################CNN+LSTM from keras.layers import Flatten from keras.layers import TimeDistributed from keras.layers.convolutional import Conv1D from keras.layers.convolutional import MaxPooling1D raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90] n_steps = 4 X, y = split_sequence(raw_seq, n_steps) #因為這裡要用CNN所以要想像成圖片那種二維的格式當做輸入,這裡每個樣本變成了2*2的圖片 # reshape from [samples, timesteps] into [samples, subsequences, timesteps, features] n_features = 1 n_seq = 2 n_steps = 2 X = X.reshape((X.shape[0], n_seq, n_steps, n_features)) print(X.shape) #(5,2,2,1) # define model model = Sequential() model.add(TimeDistributed(Conv1D(filters=64, kernel_size=1, activation='relu'), input_shape=(None, n_steps, n_features))) model.add(TimeDistributed(MaxPooling1D(pool_size=2))) model.add(TimeDistributed(Flatten())) model.add(LSTM(50, activation='relu')) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, y, epochs=500, verbose=0) # demonstrate prediction x_input = array([60, 70, 80, 90]) x_input = x_input.reshape((1, n_seq, n_steps, n_features)) yhat = model.predict(x_input, verbose=0) print(yhat) #102.6211 ###########################################convLSTM # reshape from [samples, timesteps] into [samples, timesteps, rows, columns, features] n_features = 1 n_seq = 2 n_steps = 2 X = X.reshape((X.shape[0], n_seq, 1, n_steps, n_features)) # define model model = Sequential() model.add(ConvLSTM2D(filters=64, kernel_size=(1,2), activation='relu', input_shape=(n_seq, 1, n_steps, n_features))) model.add(Flatten()) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, y, epochs=500, verbose=0) # demonstrate prediction x_input = array([60, 70, 80, 90]) x_input = x_input.reshape((1, n_seq, 1, n_steps, n_features)) yhat = model.predict(x_input, verbose=0) print(yhat)