Coverage for sacred/sacred/stflow/method_interception.py: 0%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from contextlib import ContextDecorator
2from .internal import ContextMethodDecorator
3import sacred.optional as opt
6if opt.has_tensorflow:
7 tf = opt.get_tensorflow()
8else:
9 tf = None
12class LogFileWriter(ContextDecorator, ContextMethodDecorator):
13 """
14 Intercept ``logdir`` each time a new ``FileWriter`` instance is created.
16 :param experiment: Tensorflow experiment.
18 The state of the experiment must be running when entering the annotated
19 function / the context manager.
21 When creating ``FileWriters`` in Tensorflow, you might want to
22 store the path to the produced log files in the sacred database.
24 In the scope of ``LogFileWriter``, the corresponding log directory path
25 is appended to a list in experiment.info["tensorflow"]["logdirs"].
27 ``LogFileWriter`` can be used both as a context manager or as
28 an annotation (decorator) on a function.
31 Example usage as decorator::
33 ex = Experiment("my experiment")
34 @LogFileWriter(ex)
35 def run_experiment(_run):
36 with tf.Session() as s:
37 swr = tf.summary.FileWriter("/tmp/1", s.graph)
38 # _run.info["tensorflow"]["logdirs"] == ["/tmp/1"]
39 swr2 tf.summary.FileWriter("./test", s.graph)
40 #_run.info["tensorflow"]["logdirs"] == ["/tmp/1", "./test"]
43 Example usage as context manager::
45 ex = Experiment("my experiment")
46 def run_experiment(_run):
47 with tf.Session() as s:
48 with LogFileWriter(ex):
49 swr = tf.summary.FileWriter("/tmp/1", s.graph)
50 # _run.info["tensorflow"]["logdirs"] == ["/tmp/1"]
51 swr3 = tf.summary.FileWriter("./test", s.graph)
52 #_run.info["tensorflow"]["logdirs"] == ["/tmp/1", "./test"]
53 # This is called outside the scope and won't be captured
54 swr3 = tf.summary.FileWriter("./nothing", s.graph)
55 # Nothing has changed:
56 #_run.info["tensorflow"]["logdirs"] == ["/tmp/1", "./test"]
58 """
60 def __init__(self, experiment):
61 self.experiment = experiment
63 def log_writer_decorator(
64 instance, original_method, original_args, original_kwargs
65 ):
66 result = original_method(instance, *original_args, **original_kwargs)
67 if "logdir" in original_kwargs:
68 logdir = original_kwargs["logdir"]
69 else:
70 logdir = original_args[0]
71 self.experiment.info.setdefault("tensorflow", {}).setdefault(
72 "logdirs", []
73 ).append(logdir)
74 return result
76 ContextMethodDecorator.__init__(
77 self, tf.summary.FileWriter, "__init__", log_writer_decorator
78 )