-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtf_lstm_climate_timeseries.py
162 lines (125 loc) · 4.19 KB
/
tf_lstm_climate_timeseries.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Import dependencies
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# Pre-process data
# seed random number generators
np.random.seed(1)
tf.set_random_seed(1)
# Paramaters
seq_len = 50 # Sequence Length
train_test_ratio = 0.7
time_series = [] # float values in ppm
time_stamps = [] # string corresponding to year-month
# read from data from csv file
with open('datasets/co2-ppm-mauna-loa-19651980.csv') as f:
skipped_line = False
for line in f.readlines():
if not skipped_line:
skipped_line = True
continue
else:
try:
line = line.strip().split(',')
time_series.append([float(line[1])])
time_stamps.append(line[0].strip('"'))
except Exception as e:
break
# Scale data
ts_min = np.min(time_series)
ts_max = np.max(time_series)
time_series = (time_series - ts_min) / (ts_max - ts_min)
# Split data into train and test
train_time_series = time_series[:int(len(time_series) * train_test_ratio)]
test_time_series = time_series[int(len(time_series) * train_test_ratio) - 1:]
# Creates sequences from data
def create_dataset(data, len_seq):
features = []
labels = []
for i in range(len(data) - len_seq):
features.append(data[i:i + len_seq])
labels.append(data[i + len_seq])
return features, labels
trainX, trainY = create_dataset(train_time_series, seq_len)
testX, testY = create_dataset(
np.concatenate((trainX[-1], test_time_series)), seq_len)
# Neural Network
# hyper-parameters
n_rnn_neurons = 100
n_input_neurons = 1
n_output_neurons = 1
learn_rate = 0.006
n_epoch = 1000
# input/output placeholders
X = tf.placeholder(tf.float32, [None, seq_len, n_input_neurons])
Y = tf.placeholder(tf.float32, [None, n_output_neurons])
# Weights and biases for final fully connected layer
layer_op = {
'weight':
tf.Variable(tf.random_normal([n_rnn_neurons, n_output_neurons], stddev=1)),
'bias':
tf.Variable(tf.random_normal([n_output_neurons], stddev=1))
}
# Model
# lstm + droput layer
cell = tf.contrib.rnn.BasicLSTMCell(n_rnn_neurons)
cell = tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=0.75)
lstm_op, _ = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
# Just connect last output of hidden layer to fully connected layer
lstm_op = tf.squeeze(lstm_op[:, -1:], axis=1)
final_op = tf.nn.sigmoid(
tf.matmul(lstm_op, layer_op['weight']) + layer_op['bias'])
# Error and Optimizer
# mean-squared error
error = tf.reduce_mean(0.5 * tf.square(final_op - Y))
# adam-optimizer
optimizer = tf.train.AdamOptimizer(learn_rate).minimize(error)
# Start Session
with tf.Session() as sess:
tf.global_variables_initializer().run()
print("*********** Train ***********")
for epoch in range(n_epoch):
_, err = sess.run([optimizer, error], feed_dict={X: trainX, Y: trainY})
if epoch % 100 == 0:
print("Epoch : %d Error = %f" % (epoch, err))
print("\n*********** Test ***********")
err, resultt = sess.run([error, final_op], feed_dict={X: testX, Y: testY})
print("Testing Error : %f" % err)
# Predict futur values with continuous data
inp = trainX[-1].flatten().tolist()
resultp = []
for i in range(len(test_time_series)):
op = final_op.eval({X: np.reshape(inp, [1, -1, 1])})
inp.append(op[0][0])
resultp.append(op[0][0])
del inp[0]
# Plot test and prediction output
plt.figure(figsize=(12, 8))
plt.plot(
train_time_series * (ts_max - ts_min) + ts_min,
'b',
label='training data')
plt.plot(
np.arange(len(train_time_series) - 1, len(time_series)),
test_time_series * (ts_max - ts_min) + ts_min,
'c',
label='expected data')
plt.plot(
np.arange(len(train_time_series) - 1, len(time_series)),
resultt * (ts_max - ts_min) + ts_min,
'm',
label='test output')
plt.plot(
np.arange(len(train_time_series) - 1, len(time_series)),
np.array(resultp) * (ts_max - ts_min) + ts_min,
'r',
label='continous prediction')
plt.xticks(
np.arange(0, len(time_series), 12),
time_stamps[::12],
rotation=70,
fontsize=7)
plt.xlabel('Month')
plt.ylabel('CO2 (ppm)')
plt.legend(loc='upper left')
plt.show()