-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
180 lines (172 loc) · 9.41 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
import os
import gin
import datetime
import tensorflow as tf
from data import dataset
from absl import app, logging
from rl import environment, rl_agent, training
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
logging.info(e)
def main(args):
# parse config file
gin.parse_config_file("config.gin")
run()
@gin.configurable
def run(path_to_train_data="", path_to_eval_data="", normalization=False, normalization_type="min_max",
setup="single_step", rl_algorithm="ddpg", env_implementation="tf", use_gpu=False, multi_task=False):
# logging
log_dir = "./logs/" + "log" + datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
file_writer = tf.summary.create_file_writer(log_dir)
logging.get_absl_handler().use_absl_log_file(program_name="log", log_dir=log_dir)
# load data set
if multi_task:
dataset_files = sorted(os.listdir(path_to_train_data), key=lambda index: int(index.split("-")[0]))
patients_train_datasets = []
patient_train_total_times = []
for f in dataset_files:
ts_train_data_sp, total_train_time_h = dataset.load_csv_dataset(os.path.join(path_to_train_data, f))
patients_train_datasets.append(ts_train_data_sp)
patient_train_total_times.append(total_train_time_h)
ts_train_data = patients_train_datasets
total_train_time_h = int((len(ts_train_data) * 5) / 60) + 1
else:
ts_train_data, total_train_time_h = dataset.load_csv_dataset(path_to_train_data)
if path_to_eval_data != "":
if multi_task:
dataset_files = sorted(os.listdir(path_to_eval_data), key=lambda index: int(index.split("-")[0]))
patients_eval_datasets = []
patient_eval_total_times = []
for f in dataset_files:
ts_eval_data_sp, total_eval_time_h = dataset.load_csv_dataset(os.path.join(path_to_eval_data, f))
patients_eval_datasets.append(ts_eval_data_sp)
patient_eval_total_times.append(total_eval_time_h)
ts_eval_data = patients_eval_datasets
total_eval_time_h = int((len(ts_eval_data) * 5) / 60) + 1
else:
ts_eval_data, total_eval_time_h = dataset.load_csv_dataset(path_to_eval_data)
else:
ts_eval_data = ts_train_data
total_eval_time_h = total_train_time_h
if normalization:
if multi_task:
ts_train_data, ts_eval_data, data_summary = dataset.data_normalization_multi_patient(
ts_train_data, ts_eval_data, normalization_type=normalization_type)
else:
ts_train_data, ts_eval_data, data_summary = dataset.data_normalization(
ts_train_data, ts_eval_data, normalization_type=normalization_type)
else:
data_summary = {}
# create environment
if setup == "single_step":
if env_implementation == "tf":
train_env = environment.TsForecastingSingleStepTFEnv(ts_train_data, rl_algorithm, data_summary)
train_env_eval = environment.TsForecastingSingleStepTFEnv(
ts_train_data, rl_algorithm, data_summary, evaluation=True, max_window_count=-1)
else:
train_env = environment.TsForecastingSingleStepEnv(ts_train_data, rl_algorithm=rl_algorithm)
train_env_eval = environment.TsForecastingSingleStepEnv(
ts_train_data, evaluation=True, max_window_count=-1, rl_algorithm=rl_algorithm)
if normalization:
# max_attribute_val = train_env.max_attribute_val * data_summary["max"],
max_attribute_val = dataset.undo_data_normalization_sample_wise(train_env.max_attribute_val, data_summary)
else:
max_attribute_val = train_env.max_attribute_val
if path_to_eval_data != "":
if env_implementation == "tf":
eval_env = environment.TsForecastingSingleStepTFEnv(
ts_eval_data, rl_algorithm, data_summary, evaluation=True, max_window_count=-1)
eval_env_train = environment.TsForecastingSingleStepTFEnv(ts_eval_data, rl_algorithm, data_summary)
else:
eval_env = environment.TsForecastingSingleStepEnv(
ts_eval_data, evaluation=True, rl_algorithm=rl_algorithm, max_window_count=-1)
eval_env_train = environment.TsForecastingSingleStepEnv(ts_eval_data, rl_algorithm=rl_algorithm)
else:
if env_implementation == "tf":
eval_env = environment.TsForecastingSingleStepTFEnv(
ts_train_data, rl_algorithm, data_summary, evaluation=True, max_window_count=-1)
eval_env_train = environment.TsForecastingSingleStepTFEnv(ts_train_data, rl_algorithm, data_summary)
else:
eval_env = environment.TsForecastingSingleStepEnv(
ts_train_data, evaluation=True, rl_algorithm=rl_algorithm, max_window_count=-1)
eval_env_train = environment.TsForecastingSingleStepEnv(ts_train_data, rl_algorithm)
forecasting_steps = 1
num_iter = train_env.max_window_count
elif setup == "multi_step":
if env_implementation == "tf":
train_env = environment.TsForecastingMultiStepTFEnv(
ts_train_data, rl_algorithm, data_summary, multi_task=multi_task)
train_env_eval = environment.TsForecastingMultiStepTFEnv(
ts_train_data, rl_algorithm, data_summary, multi_task=multi_task,
evaluation=True, max_window_count=-1)
else:
train_env = environment.TsForecastingMultiStepEnv(ts_train_data, rl_algorithm)
train_env_eval = environment.TsForecastingMultiStepEnv(
ts_train_data, rl_algorithm, evaluation=True, max_window_count=-1)
if normalization:
max_attribute_val = dataset.undo_data_normalization_sample_wise(train_env.max_attribute_val, data_summary)
else:
max_attribute_val = train_env.max_attribute_val
if path_to_eval_data != "":
if env_implementation == "tf":
eval_env = environment.TsForecastingMultiStepTFEnv(
ts_eval_data, rl_algorithm, data_summary, evaluation=True, max_window_count=-1,
multi_task=multi_task)
eval_env_train = environment.TsForecastingMultiStepTFEnv(
ts_eval_data, rl_algorithm, data_summary, multi_task=multi_task)
else:
eval_env = environment.TsForecastingMultiStepEnv(ts_eval_data, rl_algorithm,
evaluation=True, max_window_count=-1)
eval_env_train = environment.TsForecastingMultiStepEnv(ts_eval_data, rl_algorithm)
else:
if env_implementation == "tf":
eval_env = environment.TsForecastingMultiStepTFEnv(
ts_train_data, rl_algorithm, data_summary, evaluation=True, max_window_count=-1,
multi_task=multi_task)
eval_env_train = environment.TsForecastingMultiStepTFEnv(
ts_train_data, rl_algorithm, data_summary, multi_task=multi_task)
else:
eval_env = environment.TsForecastingMultiStepEnv(ts_train_data, rl_algorithm, evaluation=True,
max_window_count=-1)
eval_env_train = environment.TsForecastingMultiStepEnv(ts_train_data, rl_algorithm)
forecasting_steps = train_env.pred_horizon
num_iter = train_env.max_window_count
if env_implementation != "tf":
# get TF environment
tf_train_env = environment.get_tf_environment(train_env)
tf_train_env_eval = environment.get_tf_environment(train_env_eval)
tf_eval_env = environment.get_tf_environment(eval_env)
tf_eval_env_train = environment.get_tf_environment(eval_env_train)
else:
tf_train_env = train_env
tf_train_env_eval = train_env_eval
tf_eval_env = eval_env
tf_eval_env_train = eval_env_train
# set up RL agent
agent = rl_agent.get_rl_agent(tf_train_env, rl_algorithm, use_gpu)
# save gin's operative config to a file before training
config_txt_file = open(log_dir + "/gin_config.txt", "w+")
config_txt_file.write("Configuration options available before training \n")
config_txt_file.write("\n")
config_txt_file.write(gin.operative_config_str())
config_txt_file.close()
# train agent on environment
training.rl_training_loop(log_dir, tf_train_env, tf_train_env_eval, tf_eval_env, tf_eval_env_train, agent,
ts_train_data, ts_eval_data, file_writer, setup, forecasting_steps, rl_algorithm,
total_train_time_h, total_eval_time_h, max_attribute_val, num_iter, data_summary,
env_implementation, multi_task)
# save gin's operative config to a file after training
config_txt_file = open(log_dir + "/gin_config.txt", "a")
config_txt_file.write("\n")
config_txt_file.write("Configuration options available after training \n")
config_txt_file.write("\n")
config_txt_file.write(gin.operative_config_str())
config_txt_file.close()
if __name__ == '__main__':
app.run(main)