Skip to content

Instantly share code, notes, and snippets.

@ikhlestov
Created January 10, 2017 17:20
Show Gist options
  • Save ikhlestov/0eb1dae298d39b6df3c63509596b63f8 to your computer and use it in GitHub Desktop.
Save ikhlestov/0eb1dae298d39b6df3c63509596b63f8 to your computer and use it in GitHub Desktop.
import json
import tensorflow as tf
from tensorflow.python.client import timeline
x = tf.random_normal([1000, 1000])
y = tf.random_normal([1000, 1000])
res = tf.matmul(x, y)
# Run the graph with full trace option
total_result = None
with tf.Session() as sess:
for i in range(2):
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
sess.run(res, options=run_options, run_metadata=run_metadata)
# Create the Timeline object, and write it to a json
tl = timeline.Timeline(run_metadata.step_stats)
ctf = tl.generate_chrome_trace_format()
# convert crome trace to python dict
ctf_dict = json.loads(ctf)
# create one dict for all results
if total_result is None:
total_result = ctf_dict
else:
for event in ctf_dict['traceEvents']:
# add only events running times, not definitions
if 'ts' in event:
total_result['traceEvents'].append(event)
with open('timeline_total.json', 'w') as f:
json.dump(total_result, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment