Last active
January 12, 2021 10:05
-
-
Save kretes/74a302a0abf879e5369f1ecd8d2a6d05 to your computer and use it in GitHub Desktop.
timing wrapper that wraps a funciton and logs its execution time to tensorboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
import itertools | |
def timing_tb(writer): | |
def wrapper_outer(func): | |
cont = itertools.count() | |
print(f"will log under {func.__name__}") | |
@wraps(func) | |
def wrapper(*arg, **kw): | |
t1 = time.time() | |
res = func(*arg, **kw) | |
t2 = time.time() | |
step = next(cont) | |
# print("log", func.__name__, t2-t1, step) | |
with writer.as_default(step=step): | |
if not tf.get_static_value(tf.summary.scalar(f'metric_{func.__name__}', data=t2-t1)): | |
print("save failed") | |
tf.summary.flush() | |
return res | |
return wrapper | |
return wrapper_outer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file_writer = tf.summary.create_file_writer(logdir) | |
@timing_tb(file_writer) | |
def a(): | |
return 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment