Guest User

Untitled

a guest
May 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. import os
  4. import tensorflow as tf
  5. import numpy as np
  6. import tempfile
  7. from tensorflow.python import debug as tfdbg
  8.  
  9. def watch_session(dump_root_dir, train_op, times):
  10. ''' Dump tensors to temperary directory for analysis'''
  11. watch_opt = tfdbg.WatchOptions(
  12. debug_ops="DebugIdentity", node_name_regex_whitelist=r".*")
  13.  
  14. def my_watch_fn(fetches, feeds):
  15. return watch_opt
  16.  
  17. with tf.Session() as sess:
  18. sess.run(tf.global_variables_initializer())
  19. sess = tfdbg.DumpingDebugWrapperSession(sess, watch_fn=my_watch_fn,
  20. session_root=dump_root_dir)
  21. for _ in range(times):
  22. sess.run(train_op)
  23.  
  24. def next_nan_or_inf(dump_root_dir):
  25. for folder in sorted(os.listdir(dump_root_dir)):
  26. mydir = tfdbg.DebugDumpDir(os.path.join(dump_root_dir, folder))
  27. for data in sorted(mydir.dumped_tensor_data,
  28. key=lambda x: x.timestamp):
  29. try:
  30. tensor = data.get_tensor()
  31. if 'float' in tensor.dtype.name:
  32. if np.isnan(tensor).any() or np.isinf(tensor).any():
  33. yield data
  34. except AttributeError:
  35. # Some tensors may be uninitialized, we skip those
  36. pass
  37.  
  38.  
  39. if __name__ == __main__:
  40. NUM_RUNS = 3
  41. NUM_PRINT_TENSORS = 10
  42. train_op = tf.constant([float('NaN')])
  43.  
  44. dump_root_dir = tempfile.mkdtemp()
  45. print('dump_root_dir: %s' % dump_root_dir)
  46. watch_session(dump_root_dir, train_op, NUM_RUNS)
  47. count = 0
  48. for data in next_nan_or_inf(dump_root_dir):
  49. print('-'*40)
  50. print('[*] Round %s: %s' % (
  51. data.file_path.split('/')[3][-1], data.tensor_name))
  52. count += 1
  53. if count > NUM_PRINT_TENSORS:
  54. break
Add Comment
Please, Sign In to add comment