I need to use the Tensorflow profiler to profile some code that is running slowly for some reason. Unfortunately, the code in question uses tf.Estimator, so I can't figure out how to inject the run metadata object into the session run() call in order to get the information that the profiler needs.
What should I do?
It is recommended using pre-made Estimators when just getting started. To write a TensorFlow program based on pre-made Estimators, you must perform the following tasks: Create one or more input functions. Define the model's feature columns.
The TensorFlow Profiler (or the Profiler) provides a set of tools that you can use to measure the training performance and resource consumption of your TensorFlow models. This new version of the Profiler is integrated into TensorBoard, and builds upon existing capabilities such as the Trace Viewer.
tf.estimator
use tf.train.ProfilerHook
works!
just add a ProfilerHook
in TrainSpec
hooks!
hook = tf.train.ProfilerHook(
save_steps=20,
output_dir=os.path.join(args.model_dir, "tracing"),
show_dataflow=True,
show_memory=True)
hooks = [hook]
train_spec = tf.estimator.TrainSpec(
hooks=hooks,
input_fn=lambda: input_fn())
then, you could get the tracing file like timeline-{}.json
in model_dir/tracing
, and open chrome chrome://tracing
to visual!
refs: https://stackoverflow.com/a/48278275/6494418
with tf.contrib.tfprof.ProfileContext('/tmp/train_dir', dump_steps=[10]) as pctx:
estimator.train() # any thing you want to profile
Then you will get a file at /tmp/train_dir/profile_10
Arguments are defined in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/profiler/profile_context.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With