-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecast.py
59 lines (44 loc) · 1.88 KB
/
forecast.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
# -*- coding: utf-8 -*-
"""
Writters:
Aristi Papstavrou
Vissarion Moutafis
Time Series Forecasting using tensorflow lstm models.
usage:
~$ python forecast.py [-h] [-d DATASET_PATH] [-n N_SAMPLES]
"""
import pandas as pd
from src.ArgParser import *
from src.TimeSeriesForecastingFramework import *
from config.forecast_config import *
def main():
# create the needed parser
cmd_args = create_hyperparameter_parser(1)
n_samples = int(cmd_args.n_samples)
input_dataset_path = cmd_args.dataset_path
train_mode = cmd_args.train # set mode
MODEL_PATH = cmd_args.model_path + 'forecast' # set model path
# get the timeseries sample in a pandas dataframe
timeseries_df = pd.read_csv(input_dataset_path, sep="\t", index_col=0, header=None).astype(np.float32).sample(n_samples)
# get the indices in a list
TIME_SERIES_ID = timeseries_df.index.tolist()
if train_mode:
# create the timeseries prediction model
model = TimeSeriesForecastModel(
(LOOKBACK, 1), LSTM_LAYERS, dropout=DROPOUT_RATE, _loss=LOSS)
# initiate a timeseries forcasting framework
problem = TimeSeriesForecast(model, timeseries_df.to_numpy(), TIME_SERIES_ID)
# solve the problem
problem.solve(lookback=LOOKBACK, epochs=EPOCHS, batch_size=BATCH_SIZE)
# save the model for later use
model.save_solver(MODEL_PATH)
else:
model = TimeSeriesForecastModel((LOOKBACK, 1), LSTM_LAYERS, dropout=DROPOUT_RATE, _loss=LOSS, trained_model_path=MODEL_PATH)
# initiate a timeseries forcasting framework
problem = TimeSeriesForecast(model, timeseries_df.to_numpy(), TIME_SERIES_ID)
# preprocess dataset
problem.preprocess_dataset(LOOKBACK, trained=True)
# plot graphs based on index of timeseries (Comment out if you dont care)
problem.plot_graphs()
if __name__ == "__main__":
main()