Skip to content
Snippets Groups Projects
Commit acff6865 authored by lukas leufen's avatar lukas leufen
Browse files

include time tracking

parents f4d5ac23 e936d100
Branches
Tags
2 merge requests!12Experiment Setup finished,!11finish run script creation, /close #11
...@@ -9,6 +9,7 @@ import math ...@@ -9,6 +9,7 @@ import math
from typing import Union from typing import Union
import numpy as np import numpy as np
import os import os
import time
def to_list(arg): def to_list(arg):
...@@ -84,3 +85,41 @@ class LearningRateDecay(keras.callbacks.History): ...@@ -84,3 +85,41 @@ class LearningRateDecay(keras.callbacks.History):
self.lr['lr'].append(current_lr) self.lr['lr'].append(current_lr)
logging.info(f"Set learning rate to {current_lr}") logging.info(f"Set learning rate to {current_lr}")
return K.get_value(self.model.optimizer.lr) return K.get_value(self.model.optimizer.lr)
class TimeTracking(object):
"""
Track time to measure execution time. Time tracking automatically starts on initialisation and ends by calling stop
method. Duration can always be shown by printing the time tracking object or calling get_current_duration.
"""
def __init__(self, start=True):
self.start = None
self.end = None
if start:
self._start()
def _start(self):
self.start = time.time()
def _end(self):
self.end = time.time()
def _duration(self):
if self.end:
return self.end - self.start
else:
return time.time() - self.start
def __repr__(self):
return f"{round(self._duration(), 2)}s"
def run(self):
self._start()
def stop(self):
self.end = time.time()
return self._duration()
def duration(self):
return self._duration()
import pytest import pytest
from src.helpers import to_list, check_path_and_create, l_p_loss, LearningRateDecay from src.helpers import *
import logging import logging
import os import os
import keras import keras
...@@ -78,3 +78,60 @@ class TestLearningRateDecay: ...@@ -78,3 +78,60 @@ class TestLearningRateDecay:
model.compile(optimizer=keras.optimizers.Adam(), loss=l_p_loss(2)) model.compile(optimizer=keras.optimizers.Adam(), loss=l_p_loss(2))
model.fit(np.array([1, 0, 2, 0.5]), np.array([1, 1, 0, 0.5]), epochs=5, callbacks=[lr_decay]) model.fit(np.array([1, 0, 2, 0.5]), np.array([1, 1, 0, 0.5]), epochs=5, callbacks=[lr_decay])
assert lr_decay.lr['lr'] == [0.02, 0.02, 0.02*0.95, 0.02*0.95, 0.02*0.95*0.95] assert lr_decay.lr['lr'] == [0.02, 0.02, 0.02*0.95, 0.02*0.95, 0.02*0.95*0.95]
class TestTimeTracking:
def test_init(self):
t = TimeTracking()
assert t.start is not None
assert t.start < time.time()
assert t.end is None
t2 = TimeTracking(start=False)
assert t2.start is None
def test__start(self):
t = TimeTracking(start=False)
t._start()
assert t.start < time.time()
def test__end(self):
t = TimeTracking()
t._end()
assert t.end > t.start
def test__duration(self):
t = TimeTracking()
d1 = t._duration()
assert d1 > 0
d2 = t._duration()
assert d2 > d1
t._end()
d3 = t._duration()
assert d3 > d2
assert d3 == t._duration()
def test_repr(self):
t = TimeTracking()
t._end()
duration = t._duration()
assert t.__repr__().rstrip() == f"{round(duration, 2)}s".rstrip()
def test_run(self):
t = TimeTracking(start=False)
assert t.start is None
t.run()
assert t.start is not None
def test_stop(self):
t = TimeTracking()
assert t.end is None
duration = t.stop()
assert duration == t._duration()
def test_duration(self):
t = TimeTracking()
duration = t
assert duration is not None
duration = t.stop()
assert duration == t.duration()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment