Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import os
  2. import tempfile
  3. import json
  4.  
  5. import tensorflow as tf
  6. from tensorflow.contrib.layers import fully_connected as fc
  7. from tensorflow.examples.tutorials.mnist import input_data
  8. from tensorflow.python.client import timeline
  9.  
  10.  
  11. class TimeLiner:
  12. _timeline_dict = None
  13.  
  14. def update_timeline(self, chrome_trace):
  15. # convert crome trace to python dict
  16. chrome_trace_dict = json.loads(chrome_trace)
  17. # for first run store full trace
  18. if self._timeline_dict is None:
  19. self._timeline_dict = chrome_trace_dict
  20. # for other - update only time consumption, not definitions
  21. else:
  22. for event in chrome_trace_dict['traceEvents']:
  23. # events time consumption started with 'ts' prefix
  24. if 'ts' in event:
  25. self._timeline_dict['traceEvents'].append(event)
  26.  
  27. def save(self, f_name):
  28. with open(f_name, 'w') as f:
  29. json.dump(self._timeline_dict, f)
  30.  
  31.  
  32. batch_size = 100
  33.  
  34. inputs = tf.placeholder(tf.float32, [batch_size, 784])
  35. targets = tf.placeholder(tf.float32, [batch_size, 10])
  36.  
  37. with tf.variable_scope("layer_1"):
  38. fc_1_out = fc(inputs, num_outputs=500, activation_fn=tf.nn.sigmoid)
  39. with tf.variable_scope("layer_2"):
  40. fc_2_out = fc(fc_1_out, num_outputs=784, activation_fn=tf.nn.sigmoid)
  41. with tf.variable_scope("layer_3"):
  42. logits = fc(fc_2_out, num_outputs=10)
  43.  
  44. loss = tf.reduce_mean(
  45. tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=targets))
  46. train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
  47.  
  48. if __name__ == '__main__':
  49. mnist_save_dir = os.path.join(tempfile.gettempdir(), 'MNIST_data')
  50. mnist = input_data.read_data_sets(mnist_save_dir, one_hot=True)
  51.  
  52. config = tf.ConfigProto()
  53. config.gpu_options.allow_growth = True
  54. with tf.Session(config=config) as sess:
  55. sess.run(tf.global_variables_initializer())
  56.  
  57. options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
  58. run_metadata = tf.RunMetadata()
  59. many_runs_timeline = TimeLiner()
  60. runs = 5
  61. for i in range(runs):
  62. batch_input, batch_target = mnist.train.next_batch(batch_size)
  63. feed_dict = {inputs: batch_input,
  64. targets: batch_target}
  65.  
  66. sess.run(train_op,
  67. feed_dict=feed_dict,
  68. options=options,
  69. run_metadata=run_metadata)
  70.  
  71. fetched_timeline = timeline.Timeline(run_metadata.step_stats)
  72. chrome_trace = fetched_timeline.generate_chrome_trace_format()
  73. many_runs_timeline.update_timeline(chrome_trace)
  74. many_runs_timeline.save('timeline_03_merged_%d_runs.json' % runs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement