-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.py
112 lines (84 loc) · 3.38 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
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
"""
Written by Matteo Dunnhofer - 2018
Utility class to log training info to sstdout, to file, to tensorboard
"""
import os
import datetime as dt
from tensorboardX import SummaryWriter
class Logger(object):
def __init__(self, experiment_path, to_file=False, to_tensorboard=False):
super(Logger, self).__init__()
self.experiment_path = os.path.join(experiment_path, 'logs')
self.to_file = to_file
self.to_tensorboard = to_tensorboard
if self.to_file:
self.log_file = open(os.path.join(self.experiment_path, 'log.txt'), 'w+')
self.log_file.write(self.experiment_path + '\n\n')
if self.to_tensorboard:
self.tb_writer = SummaryWriter(os.path.join(experiment_path, 'tb_logs'))
def log_config(self, config, print_log=True):
"""
Log to file all the congfiguration data of the experiment
"""
if print_log:
print(str(config) + '\n')
if self.to_file:
self.log_file.write(str(config) + '\n')
def log_pytorch_model(self, model, print_log=True):
"""
Log to file the model architecture
"""
if print_log:
print(str(model) + '\n')
if self.to_file:
self.log_file.write(str(model) + '\n\n')
#if self.to_tensorboard:
# self.tb_writer.add_graph(model)
def log_loss(self, loss_type, step, value):
"""
Logging loss data
"""
log_str = '[{}] Step: {:08d} - Loss {:.05f}'.format(dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), step, value)
print(log_str)
if self.to_file:
self.log_file.write(log_str + '\n')
if self.to_tensorboard:
self.tb_writer.add_scalar(loss_type + '_loss', value, step)
def log_value(self, value_type, step, value, print_value=True, to_file=True, to_tensorboard=True):
"""
Logging generic data
"""
log_str = '[{}] Step: {:08d} - {} {:.05f}'.format(dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), step, value_type, value)
if print_value:
print(log_str)
if to_file:
self.log_file.write(log_str + '\n')
if to_tensorboard:
self.tb_writer.add_scalar(value_type, value, step)
def log_episode(self, worker_name, episode, reward):
"""
Logging episode data
"""
log_str = '[{}] Worker: {} --- Episode: {:07d} - Reward {:05f}'.format(dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), worker_name, episode, reward)
print(log_str)
if self.to_file:
self.log_file.write(log_str + '\n')
if self.to_tensorboard:
self.tb_writer.add_scalar('reward', reward, episode)
def log_test_episode(self, episode, reward):
"""
Logging test episode data
"""
log_str = '[{}] Test episode: {:05d} - Reward {:05f}'.format(dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), episode, reward)
print(log_str)
if self.to_file:
self.log_file.write(log_str + '\n')
#if self.to_tensorboard:
# TODO
def log_variables(self):
log_str = '[{}] Variables saved'.format(dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print(log_str)
if self.to_file:
self.log_file.write(log_str + '\n')
def close(self):
self.log_file.close()