-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.py
45 lines (31 loc) · 1.59 KB
/
logger.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
import csv
from os.path import exists
class TrainEpisodeLogger2048:
"""
TrainEpisodeLogger2048 is used to show the
NN training results (max tile reached, average reward, etc.) and to save the results in a CSV file.
"""
def __init__(self):
self.train_idx = 0
self.filePath_train = 'models/logs/train_history.csv'
self.filePath_eps = 'models/logs/ep_history.csv'
self.csv_writer_ep = self.getCSVWriter(self.filePath_eps,
headers=['episode', 'score', 'max_reward', 'max_tile'])
self.csv_writer_train = self.getCSVWriter(self.filePath_train,
headers=['idx', 'episode', 'loss', 'mean_abs_error'])
def getCSVWriter(self, filePath, headers):
if exists(filePath):
csv_file = open(filePath, "a") # a = append
csv_writer = csv.writer(csv_file, delimiter=',')
else:
csv_file = open(filePath, "w") # w = write (clear and restart)
csv_writer = csv.writer(csv_file, delimiter=',')
csv_writer.writerow(headers)
return csv_writer
def log_episode(self, episode, score, max_reward, max_tile):
self.csv_writer_ep.writerow((episode + 1, score, max_reward, max_tile))
def log_train(self, episode, history):
self.train_idx += 1
loss = history.history['loss'][0]
mean_abs_err = history.history['mean_absolute_error'][0]
self.csv_writer_train.writerow((self.train_idx, episode + 1, loss, mean_abs_err))