-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
161 lines (132 loc) · 6.77 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
import os
import random
import argparse
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score, mean_absolute_percentage_error
from utils import make_dirs, load_data, standardization, train_validate_test_split, SequenceDataset, train_model, val_model, predict, inverse_transform, calculate_metrics
from models import RNN, LSTM, GRU
from sklearn.preprocessing import MinMaxScaler
from torch.utils.data import DataLoader
def main(args):
# Fix seed
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
# Prepare data
df = load_data(args.which_data)[args.feature]
df = df.set_index(['date'])
features = list(df.columns.difference([args.target]))
target_mean = df[args.target].mean()
target_stdev = df[args.target].std()
# Standardize data
df_ = standardization(df, args.target)
df = df_.reset_index(drop=False)
#Split data
df_train, df_val, df_test = train_validate_test_split(df, args.test_split)
df_train = df_train.set_index(['date'])
df_val = df_val.set_index(['date'])
df_test = df_test.set_index(['date'])
#display(df_train)
#display(df_val)
#display(df_test)
train_dataset = SequenceDataset(
df_train,
target=args.target,
features=features,
sequence_length=args.seq_length
)
val_dataset = SequenceDataset(
df_val,
target=args.target,
features=features,
sequence_length=args.seq_length
)
test_dataset = SequenceDataset(
df_test,
target=args.target,
features=features,
sequence_length=args.seq_length
)
train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=args.batch_size, shuffle=False)
test_loader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False)
num_inputs=len(features)
if args.model == 'rnn':
model = RNN(num_inputs, args.num_hidden_size, args.num_layers, args.output_size, args.dropout)
elif args.model == 'lstm':
model = LSTM(num_inputs, args.hidden_size, args.num_layers, args.output_size, args.dropout)
elif args.model == 'gru':
model = GRU(num_inputs, args.hidden_size, args.num_layers, args.output_size, args.dropout)
else:
raise NotImplementedError
loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
train_plot_losss = []
val_plot_losss = []
for ix_epoch in range(1, args.num_epochs+1):
print(f"Epoch {ix_epoch}\n---------")
train_model(train_loader, model, loss_function, optimizer=optimizer)
val_model(val_loader, model, loss_function)
print()
train_eval_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=False)
ystar_col = "model forecast"
df_train[ystar_col] = predict(train_eval_loader, model).numpy()
df_val[ystar_col] = predict(val_loader, model).numpy()
df_test[ystar_col] = predict(test_loader, model).numpy()
df_out = pd.concat((df_train, df_val, df_test))[[args.target, ystar_col]]
df_pred = inverse_transform(df_out, target_stdev, target_mean)
result_metrics = calculate_metrics(df_pred, args.target, ystar_col)
display(result_metrics)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--which_data', type=str, default='./data/data.xlsx', help='which data to use')
parser.add_argument('--seed', type=int, default=7777, help='seed for reproducibility')
parser.add_argument('--output_size', type=int, default=1, help='output_dim')
parser.add_argument('--seq_length', type=int, default=5, help='window size')
parser.add_argument('--batch_size', type=int, default=64, help='mini-batch size')
parser.add_argument('--target', type=str, default='electricity_price (PLN/MWh)', help='dependent feature')
parser.add_argument('--feature', type=str,
default=['date',
'electricity_price (PLN/MWh)',
'energy_demand (MW)',
'energy_from_wind_sources (MW)',
'is_holiday',
'code_of_the_day',
'electricity_price (PLN/MWh) lag24',
'electricity_price (PLN/MWh) lag48',
'electricity_price (PLN/MWh) lag72',
'electricity_price (PLN/MWh) lag96',
'electricity_price (PLN/MWh) lag120',
'electricity_price (PLN/MWh) lag144',
'electricity_price (PLN/MWh) lag168',
'electricity_price (PLN/MWh) lag336',
'energy_demand (MW) lag24',
'energy_demand (MW) lag48',
'energy_demand (MW) lag72',
'energy_demand (MW) lag96',
'energy_demand (MW) lag120',
'energy_demand (MW) lag144',
'energy_demand (MW) lag168',
'energy_demand (MW) lag336',
'energy_from_wind_sources (MW) lag24',
'energy_from_wind_sources (MW) lag48',
'energy_from_wind_sources (MW) lag72',
'energy_from_wind_sources (MW) lag96',
'energy_from_wind_sources (MW) lag120',
'energy_from_wind_sources (MW) lag144',
'energy_from_wind_sources (MW) lag168',
'energy_from_wind_sources (MW) lag336'], help='independent features')
parser.add_argument('--test_split', type=float, default=0.0824, help='test_split')
parser.add_argument('--lr', type=int, default=0.0001, help='learning rate')
parser.add_argument('--num_hidden_size', type=int, default=256, help='hidden units')
parser.add_argument('--num_epochs', type=int, default=11, help='num epochs')
parser.add_argument('--num_layers', type=int, default=2, help='num layer dim')
parser.add_argument('--dropout', type=int, default=0.6, help='dropout rate')
parser.add_argument('--model', type=str, default='rnn', choices=['rnn', 'lstm', 'gru'])
config = parser.parse_args()
torch.cuda.empty_cache()
main(config)