-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorboardLR.py
43 lines (36 loc) · 1.44 KB
/
TensorboardLR.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
from Utils import *
from tensorflow.python.keras.callbacks import Callback
from tensorflow.python.keras import backend as k
class TensorboardLR(Callback):
def __init__(self,
log_dir='./log',
write_graph=True):
self.write_graph = write_graph
self.log_dir = log_dir
def set_model(self, model):
self.model = model
self.sess = k.get_session()
if self.write_graph:
self.writer = tf.summary.FileWriter(self.log_dir, self.sess.graph)
else:
self.writer = tf.summary.FileWriter(self.log_dir)
# def on_epoch_end(self, epoch, logs={}):
def on_train_batch_end(self, batch, logs={}):
logs.update({'learning_rate': float(k.get_value(self.model.optimizer.lr))})
index = tf.keras.backend.eval(self.model.optimizer.iterations)
self._write_logs(logs, index)
def _write_logs(self, logs, index):
for name, value in logs.items():
if name in ['batch', 'size']:
continue
summary = tf.Summary()
summary_value = summary.value.add()
if isinstance(value, np.ndarray):
summary_value.simple_value = value.item()
else:
summary_value.simple_value = value
summary_value.tag = name
self.writer.add_summary(summary, index)
self.writer.flush()
def on_train_end(self, _):
self.writer.close()