-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.py
207 lines (188 loc) · 6.73 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
Main execution for WaveNet forecasting
"""
import os
import argparse
import logging
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
from src.util import load_data, split_data, smape, Normalizer
from src.model import WaveNet
def set_logging():
""" Set logging level and format """
fmt = "[%(levelname)s %(asctime)s] %(message)s"
logging.basicConfig(format=fmt, level=logging.INFO)
def parse_args():
""" Parse command line arguments """
parser = argparse.ArgumentParser()
parser.add_argument(
'--date',
type=lambda s: datetime.strptime(s, '%Y-%m-%d'),
help='Date to run the model on.',
required=True
)
parser.add_argument(
'--data_dir',
type=str,
default='./data',
help='Location of CSV files for data input. Default is `./data`'
)
parser.add_argument(
'--test_periods',
type=int,
default=360,
help='Number of periods to use for testing. Default is 360sec (6 hours).'
)
parser.add_argument(
'--num_filters',
type=int,
default=1,
help='Convolutional filters to use. Default is 1.'
)
parser.add_argument(
'--num_layers',
type=int,
default=7,
help='Number of layers to use. Default is 7.',
)
parser.add_argument(
'--learning_rate',
type=float,
default=1e-3,
help='Optimizer learning rate. Default is 1e-3.',
)
parser.add_argument(
'--regularization',
type=float,
default=1e-2,
help='L2 regularization coefficient. Default is 1e-2.',
)
parser.add_argument(
'--num_iters',
type=int,
default=8000,
help='Number of training iterations to run. Default is 8000.',
)
parser.add_argument(
'--log_dir',
type=str,
default='./logs',
help='Directory to store logs and TensorBoard checkpoints. Default is ./logs'
)
parser.add_argument(
'--seed',
type=int,
default=0,
help='Random seed to use for weight initialization. Default is 0'
)
parser.add_argument(
'--plot',
action='store_true',
help='Store a plot of the resulting prediction'
)
parser.add_argument(
'--to_csv',
action='store_true',
help='Store a CSV file of the predictions.'
)
args = parser.parse_args()
return args
if __name__ == '__main__':
set_logging()
args = parse_args()
# Load in data
train_features, train_targets, test_targets = (
load_data(args.date, args.data_dir)
.pipe(lambda df: split_data(df, args.test_periods))
)
logging.info("Train periods: %s, test periods: %s",
train_features.shape[0], test_targets.shape[0])
# Normalize data
normalizer = Normalizer()
train_features = normalizer.fit_transform(train_features)
train_targets = normalizer.transform(train_targets)
test_targets = normalizer.transform(test_targets)
# Format model training input
columns = train_features.columns.tolist()
features = dict()
targets = dict()
for column in columns:
features[column] = train_features[column].values.reshape(1, -1)
targets[column] = train_targets[column].values.reshape(1, -1)
# Model parameters
params = {
'time_steps': train_features.shape[0],
'num_filters': args.num_filters,
'num_layers': args.num_layers,
'learning_rate': args.learning_rate,
'regularization': args.regularization,
'num_iters': args.num_iters,
'log_dir': args.log_dir,
'columns': columns,
'seed': args.seed
}
logging.info("Training with parameters: %s", params)
# Run model
with WaveNet(**params) as model:
# Train
train_pred = model.train(targets, features)
# Generate
num_steps = test_targets.shape[0]
test_pred = model.generate(num_steps, features)
for column in columns:
train_smape = smape(train_pred[column], train_targets[column], normalizer)
test_smape = smape(test_pred[column], test_targets[column], normalizer)
logging.info("%s in-sample sMAPE: %f", column, train_smape)
logging.info("%s, out-of-sample sMAPE: %f", column, test_smape)
# Maybe plot
if args.plot:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.style.use('seaborn-darkgrid')
# Each column gets it's own plot
for column in columns:
# Combine in-sample and out-of-sample in a single series
actual = pd.Series(
np.hstack([train_targets[column].values, test_targets[column].values]),
index=np.hstack([train_targets.index, test_targets.index]))
forecast = pd.Series(
np.hstack([train_pred[column].reshape(-1), test_pred[column].reshape(-1)]),
index=np.hstack([train_targets.index, test_targets.index]))
# Inverse transform
actual = normalizer.inverse_transform(actual)
forecast = normalizer.inverse_transform(forecast)
fig = plt.figure(figsize=(12, 8))
actual.plot(label='Actual', ls='--')
forecast.plot(label='Forecast', alpha=.75)
plt.title("{:s} on {:%Y-%m-%d}".format(column, args.date), fontsize=14)
train_cutoff = args.date + timedelta(days=1) - timedelta(seconds=60 * args.test_periods)
plt.axvline(train_cutoff, ls='--', alpha=.6)
plt.xlabel("Timestamp", fontsize=12)
plt.ylabel("Price USD", fontsize=12)
plt.legend(fontsize=12)
figname = os.path.join(args.log_dir, "{:s}_{:%Y_%m_%d}.png".format(column, args.date))
plt.savefig(figname)
logging.info("Plot saved to %s", figname)
# Maybe save csv
if args.to_csv:
actual = (
pd.DataFrame({
col: np.hstack([train_targets[col].values, test_targets[col].values])
for col in columns
}, index=np.hstack([train_targets.index, test_targets.index]))
.pipe(normalizer.inverse_transform)
)
forecast = (
pd.DataFrame({
col + '_pred': np.hstack([train_pred[col].reshape(-1), test_pred[col].reshape(-1)])
for col in columns
}, index=np.hstack([train_targets.index, test_targets.index]))
.pipe(normalizer.inverse_transform)
)
filename = os.path.join(args.log_dir, "{:%Y_%m_%d}.csv".format(args.date))
df = actual.join(forecast)
df.index.name = 'Timestamp'
df.to_csv(filename)
logging.info("Output saved to %s", filename)