-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathex5_2_lstm_airplane.py
124 lines (97 loc) · 3.39 KB
/
ex5_2_lstm_airplane.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
# %%
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import model_selection
from keras import models, layers
import seaborn as sns
from keraspp import skeras
# %%
def main():
machine = Machine()
machine.run(epochs=400)
class Machine():
def __init__(self):
self.data = Dataset()
shape = self.data.X.shape[1:]
self.model = rnn_model(shape)
def run(self, epochs=400):
d = self.data
X_train, X_test, y_train, y_test = d.X_train, d.X_test, d.y_train, d.y_test
X, y = d.X, d.y
m = self.model
h = m.fit(X_train, y_train, epochs=epochs, validation_data=[X_test, y_test], verbose=0)
skeras.plot_loss(h)
plt.title('History of training')
plt.show()
yp = m.predict(X_test)
print('Loss:', m.evaluate(X_test, y_test))
plt.plot(yp, label='Origial')
plt.plot(y_test, label='Prediction')
plt.legend(loc=0)
plt.title('Validation Results')
plt.show()
yp = m.predict(X_test).reshape(-1)
print('Loss:', m.evaluate(X_test, y_test))
print(yp.shape, y_test.shape)
df = pd.DataFrame()
df['Sample'] = list(range(len(y_test))) * 2
df['Normalized #Passengers'] = np.concatenate([y_test, yp], axis=0)
df['Type'] = ['Original'] * len(y_test) + ['Prediction'] * len(yp)
plt.figure(figsize=(7, 5))
sns.barplot(x="Sample", y="Normalized #Passengers",
hue="Type", data=df)
plt.ylabel('Normalized #Passengers')
plt.show()
yp = m.predict(X)
plt.plot(yp, label='Origial')
plt.plot(y, label='Prediction')
plt.legend(loc=0)
plt.title('All Results')
plt.show()
def rnn_model(shape):
m_x = layers.Input(shape=shape) #X.shape[1:]
m_h = layers.LSTM(10)(m_x)
m_y = layers.Dense(1)(m_h)
m = models.Model(m_x, m_y)
m.compile('adam', 'mean_squared_error')
m.summary()
return m
class Dataset:
def __init__(self, fname='international-airline-passengers.csv', D=12):
data_dn = load_data(fname=fname)
X, y = get_Xy(data_dn, D=D)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=42)
self.X, self.y = X, y
self.X_train, self.X_test, self.y_train, self.y_test = X_train, X_test, y_train, y_test
def load_data(fname='international-airline-passengers.csv'):
dataset = pd.read_csv(fname, usecols=[1], engine='python', skipfooter=3)
data = dataset.values.reshape(-1)
plt.plot(data)
plt.xlabel('Time'); plt.ylabel('#Passengers')
plt.title('Original Data')
plt.show()
# data normalize
data_dn = (data - np.mean(data)) / np.std(data) / 5
plt.plot(data_dn)
plt.xlabel('Time'); plt.ylabel('Normalized #Passengers')
plt.title('Normalized data by $E[]$ and $5\sigma$')
plt.show()
return data_dn
def get_Xy(data, D=12):
# make X and y
X_l = []
y_l = []
N = len(data)
assert N > D, "N should be larger than D, where N is len(data)"
for ii in range(N-D-1):
X_l.append(data[ii:ii+D])
y_l.append(data[ii+D])
X = np.array(X_l)
X = X.reshape(X.shape[0], X.shape[1], 1)
y = np.array(y_l)
print(X.shape, y.shape)
return X, y
if __name__ == '__main__':
main()
# %%