Skip to content

Instantly share code, notes, and snippets.

@ruslangrimov
Last active October 15, 2019 20:51
Show Gist options
  • Save ruslangrimov/c5c1a14343823a53e1eec972f591536d to your computer and use it in GitHub Desktop.
Save ruslangrimov/c5c1a14343823a53e1eec972f591536d to your computer and use it in GitHub Desktop.
Profiling a Keras-TensorFlow model
import tensorflow as tf
from tensorflow.python.client import timeline
from keras import backend as K
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
model = ... # A Keras model
fn = K.function(model.inputs, model.outputs, options=run_options, run_metadata=run_metadata)
for i in range(4):
x = K.variable(...)
fn([x])
tl = timeline.Timeline(run_metadata.step_stats)
ctf = tl.generate_chrome_trace_format()
with open('timeline_%d.json' % (i), 'w') as f:
f.write(ctf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment