-
Notifications
You must be signed in to change notification settings - Fork 4
/
05_6_TS_RNN_LSTM_Memory.py
147 lines (124 loc) · 4.15 KB
/
05_6_TS_RNN_LSTM_Memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#%% [markdown]
# # LSTM for Regression with Memory between batches
# LSTM has memory which is capable of remembering across long sequences.
# Normally, the state within the network is reset after each training batch when fitting, predict or evaluating.
#
# It requires training data not be shuffled when fitting the network
#
# It also requires explicit reseetting of the network state after each exposure to the epoch (model.reset_states())
# * LSTM stateful = True
# * batch_input_shape=(batch_size, time_steps, features)
# * predict(.., batch_size)
#%%
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import math
#%%
np.random.seed(7)
df = pd.read_csv('data/airline_passenger.csv',
usecols=[1], engine='python', skipfooter=3)
ds = df.values
ds = ds.astype('float32')
#%% [markdown]
# ## Data Processing
# ### Rescaling
# LSTM are sensitive to the scale of the input data, specifically sigmoid or tanh.
# Hence, rescale to 0..1
#%%
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
#%%
scaler = MinMaxScaler(feature_range=(0, 1))
ds = scaler.fit_transform(ds)
#%% [markdown]
# ### Split the train and test
train_size = int(len(ds) * 0.67)
test_size = len(ds) - train_size
train, test = ds[0:train_size,:], ds[train_size:len(ds),:]
# reshape into X=t and y=t+1
look_back = 3
#%%
def create_dataset(dataset, look_back = 1):
data_X, data_y = [], []
for i in range(len(dataset) - look_back -1):
a = dataset[i:(i+look_back), 0]
data_X.append(a)
data_y.append(dataset[i + look_back, 0])
return np.array(data_X), np.array(data_y)
#%%
train_X, train_y = create_dataset(train, look_back)
test_X, test_y = create_dataset(test, look_back)
#%% [markdown]
# ## Reshaping
# LSTM expects input to be in 3-order tuple [samples, time steps, features]
#%% [markdown]
# Set the columns to be the time steps dimensions
# change the features dimension back to 1
#%%
# reshape input to [samples, time steps, features]
train_X = np.reshape(train_X, (train_X.shape[0], train_X.shape[1], 1))
test_X = np.reshape(test_X, (test_X.shape[0], test_X.shape[1], 1))
#%% [markdown]
# ## Building the LSTM model
#%%
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
#%% [markdown]
# ## Defining the model
# * 1 input
# * 1 hidden layer with 4 LSTM blocks
# * 1 output layer
#
# Instead of specifying input_shape, we should specify stateful as true, batch_input_shape.
#%%
batch_size= 1
#%%
model = Sequential()
model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True)) # note that batch_input_shape
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()
#%%
for i in range(100):
print("************* Outer Batch %d **********" % i)
model.fit(train_X, train_y, epochs=100, batch_size=1, verbose=2)
model.reset_states()
#%%
# specify batch_size
train_predict = model.predict(train_X, batch_size=batch_size)
model.reset_states()
test_predict = model.predict(test_X, batch_size=batch_size)
model.reset_states()
#%%
# invert predictions
train_predict = scaler.inverse_transform(train_predict)
train_y = scaler.inverse_transform([train_y])
test_predict = scaler.inverse_transform(test_predict)
test_y = scaler.inverse_transform([test_y])
#%%
train_score = math.sqrt(mean_squared_error(train_y[0], train_predict[:, 0]))
test_score = math.sqrt(mean_squared_error(test_y[0], test_predict[:, 0]))
#%%
print('Train Score: %.2f RMSE' % (train_score))
print('Test Score: %.2f RMSE' % (test_score))
#%%
# shift train predictions for plotting
train_predict_plot = np.empty_like(ds)
train_predict_plot[:, :] = np.nan
train_predict_plot[look_back:len(train_predict)+look_back, :] = train_predict
#%%
# shift test predictions for plotting
test_predict_plot = np.empty_like(ds)
test_predict_plot[:, :] = np.nan
test_predict_plot[len(train_predict)+(look_back*2)+1:len(ds)-1, :] = test_predict
#%%
# plot baseline and predictions
plt.plot(scaler.inverse_transform(ds))
plt.plot(train_predict_plot)
plt.plot(test_predict_plot)
plt.figure(figsize=(300, 200))
plt.show()
#%% [markdown]
# Better than some, worse than others. Requires more epocs to train.