我们从Python开源项目中,提取了以下22个代码示例,用于说明如何使用tensorflow.python.client.timeline.Timeline()。
def run(self, fetches, feed_dict=None, options=None, run_metadata=None): # Make sure there is no disagreement doing this. if options is not None: if options.trace_level != self.profiler_options.trace_level: # pragma: no cover raise ValueError( 'In profiler session. Inconsistent trace ' 'level from run call') # pragma: no cover self.profiler_options.update(options) # pragma: no cover self.local_run_metadata = tf.RunMetadata() output = super(TracerSession, self).run( fetches, feed_dict=feed_dict, options=self.profiler_options, run_metadata=self.local_run_metadata) trace_time = timeline.Timeline(self.local_run_metadata.step_stats) ctf = trace_time.generate_chrome_trace_format() with open(self._trace_filename(), 'w') as trace_file: trace_file.write(ctf) if self.each_time: self.counter += 1 return output
def run(self, fetches, feed_dict=None): """like Session.run, but return a Timeline object in Chrome trace format (JSON). Save the json to a file, go to chrome://tracing, and open the file. Args: fetches feed_dict Returns: dict: a JSON dict """ options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() super(ProfiledSession, self).run(fetches, feed_dict, options=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() return json.loads(ctf)
def step(self, sess, batch, eval_op=None): """ One step for a batch Either sgd training by setting `eval_op` to `self.update_op` or only evaluate the loss by leaving it to be `None` :param sess: a tensorflow session :param batch: a Batch object :param eval_op: an operator in tensorflow :return: vals: dict containing the values evaluated by `sess.run()` """ feed_dict = self.feed(batch) fetch_dict = self.fetch(eval_op) # run sess vals = sess.run(fetch_dict, feed_dict, options=self.config.run_options, run_metadata=self.config.run_metadata) # trace time consumption # very slow and requires large memory if self.config.time_trace: tl = timeline.Timeline(self.config.run_metadata.step_stats) ctf = tl.generate_chrome_trace_format() with open(self.config.trace_filename, 'w') as f: f.write(ctf) print("time tracing output to " + self.config.trace_filename) return vals
def traced_run(fetches): """Runs fetches, dumps timeline files in current directory.""" global sess assert sess global timeline_counter run_metadata = tf.RunMetadata() root = os.getcwd()+"/data" from tensorflow.python.client import timeline results = sess.run(fetches, options=run_options, run_metadata=run_metadata); tl = timeline.Timeline(step_stats=run_metadata.step_stats) ctf = tl.generate_chrome_trace_format(show_memory=True, show_dataflow=False) open(root+"/timeline_%d.json"%(timeline_counter,), "w").write(ctf) open(root+"/stepstats_%d.pbtxt"%(timeline_counter,), "w").write(str( run_metadata.step_stats)) timeline_counter+=1 return results
def sessrun(*args, **kwargs): sess = u.get_default_session() if not GLOBAL_PROFILE: return sess.run(*args, **kwargs) run_metadata = tf.RunMetadata() kwargs['options'] = full_trace_options kwargs['run_metadata'] = run_metadata result = sess.run(*args, **kwargs) first_entry = args[0] if isinstance(first_entry, list): if len(first_entry) == 0 and len(args) == 1: return None first_entry = first_entry[0] name = first_entry.name name = name.replace('/', '-') tl = timeline.Timeline(run_metadata.step_stats) ctf = tl.generate_chrome_trace_format() with open('timelines/%s.json'%(name,), 'w') as f: f.write(ctf) with open('timelines/%s.pbtxt'%(name,), 'w') as f: f.write(str(run_metadata)) return result
def traced_run(fetches): """Runs fetches, dumps timeline files in current directory.""" from tensorflow.python.client import timeline global timeline_counter run_metadata = tf.RunMetadata() results = sess.run(fetches, options=run_options, run_metadata=run_metadata); tl = timeline.Timeline(step_stats=run_metadata.step_stats) ctf = tl.generate_chrome_trace_format(show_memory=True, show_dataflow=False) open("timeline_%d.json"%(timeline_counter,), "w").write(ctf) open("stepstats_%d.pbtxt"%(timeline_counter,), "w").write(str( run_metadata.step_stats)) timeline_counter+=1 return results
def train_it(sess, step=1): _pat_chars_i, _pat_lens = get_batch(__batch_size) inputs = { pat_chars_i: _pat_chars_i, pat_lens: _pat_lens} # Run optimization op (backprop) #run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) #run_metadata = tf.RunMetadata() #sess.run(optimizer, feed_dict=inputs, options=run_options, run_metadata=run_metadata) sess.run(optimizer, feed_dict=inputs) #with open('timeline.json', 'w') as f: # f.write( # timeline.Timeline(run_metadata.step_stats) # .generate_chrome_trace_format()) if step % display_step == 0: # Calculate batch loss cost_f = sess.run(cost, feed_dict=inputs) print ("Iter {}, cost= {:.6f}".format( str(step*__batch_size), cost_f))
def after_run(self, _run_context, run_values): if not self.is_chief or self._done: return step_done = run_values.results if self._active: log.info("Captured full trace at step %s", step_done) # Create output directory gfile.MakeDirs(self._output_dir) # Save run metadata trace_path = os.path.join(self._output_dir, "run_meta") with gfile.GFile(trace_path, "wb") as trace_file: trace_file.write(run_values.run_metadata.SerializeToString()) log.info("Saved run_metadata to %s", trace_path) # Save timeline timeline_path = os.path.join(self._output_dir, "timeline.json") with gfile.GFile(timeline_path, "w") as timeline_file: tl_info = timeline.Timeline(run_values.run_metadata.step_stats) tl_chrome = tl_info.generate_chrome_trace_format( show_memory=True) timeline_file.write(tl_chrome) log.info("Saved timeline to %s", timeline_path) # Save tfprof op log tf.profiler.write_op_log( graph=tf.get_default_graph(), log_dir=self._output_dir, run_meta=run_values.run_metadata) log.info("Saved op log to %s", self._output_dir) self._active = False self._done = True self._active = (step_done >= self.params["step"])
def traced_run(fetches): """Runs fetches, dumps timeline files in current directory.""" global timeline_counter run_metadata = tf.RunMetadata() config = load_config() log_fn = "%s-%s-%s"%(config.task_type, config.task_id, timeline_counter) sess = tf.get_default_session() root = os.getcwd()+"/data" os.system('mkdir -p '+root) from tensorflow.python.client import timeline results = sess.run(fetches, options=run_options, run_metadata=run_metadata); tl = timeline.Timeline(step_stats=run_metadata.step_stats) ctf = tl.generate_chrome_trace_format(show_memory=True, show_dataflow=False) open(root+"/timeline_%s.json"%(log_fn,), "w").write(ctf) open(root+"/stepstats_%s.pbtxt"%(log_fn,), "w").write(str( run_metadata.step_stats)) timeline_counter+=1 return results
def run_shit(): sess = tf.Session() run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() sess.run(tf.initialize_all_variables()) train_step_ = sess.run([train_step], options=run_options, run_metadata=run_metadata, )#feed_dict={x: [[2,3],[5,1]]}) tl = timeline.Timeline(run_metadata.step_stats) ctf = tl.generate_chrome_trace_format() with open('o_100.json', 'w') as f: f.write(ctf)
def train_network(num_epochs, num_steps, state_size=4): sess.run(tf.initialize_all_variables()) # print("--- min for graph building ---",(time.time() - start_time)/60.0) # start_time = time.time() training_losses = [] # X_test, Y_test = genTestData(num_steps, num_test_runs, num_classes) X_test, Y_test = getTestData() for idx, (X_epoch,Y_epoch) in enumerate(genEpochs(num_epochs, num_batches, num_steps, batch_size, num_classes, copy_len)): training_loss = 0 acc = 0 training_state = [np.zeros((batch_size, state_size)) for i in range(num_stacked)] print("EPOCH %d" % idx) for batch in tqdm(range(len(X_epoch))): X = X_epoch[batch] Y = Y_epoch[batch] (train_step_, loss_, train_summary_) = sess.run([train_step, loss, train_summary], feed_dict={x:X, y:Y}, options=run_options, run_metadata=run_metadata) training_loss += loss_ train_writer.add_summary(train_summary_, idx) (test_loss, test_summary_, accuracy_) = sess.run( [loss, test_summary, accuracy], feed_dict={x:X_test, y:Y_test}, options=run_options, run_metadata=run_metadata) train_writer.add_summary(test_summary_, idx) training_loss = training_loss/num_batches print("train loss:", training_loss, "test loss:", test_loss, "test accuracy:", accuracy_) training_loss = 0 tl = timeline.Timeline(run_metadata.step_stats) ctf = tl.generate_chrome_trace_format() with open('timeline_add.json', 'w') as f: f.write(ctf)
def basic_train(loss_op, update_op, profile=0, save_dir='asset/unamed', **kwargs): profile_state = _ShouldProfile(profile) @stf.sg_train_func def train_func(sess, arg): profile_state.increment() if profile_state.should_profile(): options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() else: options = None run_metadata = None loss = sess.run([loss_op] + update_op, options=options, run_metadata=run_metadata)[0] if profile_state.should_profile(): tl = tf_timeline.Timeline(run_metadata.step_stats) ctf = tl.generate_chrome_trace_format() with open(path.join(save_dir, 'timeline.json'), 'w') as fd: print(ctf, file=fd) return loss # run train function train_func(save_dir=save_dir, **kwargs)
def run_training_batch(self, session, batch): """ A batch contains input tensors for words, pos, lemmas, preds, preds_idx, and labels (in that order) Runs the model on the batch (through train_op if train=True) Returns the loss """ feed_dict = self.batch_to_feed(batch) feed_dict[self.use_dropout_placeholder] = 1.0 fetches = [self.loss, self.train_op] # options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) # run_metadata = tf.RunMetadata() loss, _ = session.run(fetches, feed_dict=feed_dict) # loss, _ = session.run(fetches, # feed_dict=feed_dict, # options=options, # run_metadata=run_metadata) # fetched_timeline = timeline.Timeline(run_metadata.step_stats) # chrome_trace = fetched_timeline.generate_chrome_trace_format() # with open('timeline.json', 'w') as f: # f.write(chrome_trace) return loss
def trace(config, sess, model, train_data): run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() X, Q, Y = random_batch(*train_data, config.batch_size) model.batch_fit(X, Q, Y, learning_rate, run_options, run_metadata) train_writer.add_run_metadata(run_metadata, 'step%d' % step) from tensorflow.python.client import timeline tl = timeline.Timeline(run_metadata.step_stats) ctf = tl.generate_chrome_trace_format() with open('timeline.json', 'w') as f: f.write(ctf) return
def benchmark_one_step(sess, fetches, step, batch_size, step_train_times, trace_filename, image_producer, params, summary_op=None): """Advance one step of benchmarking.""" if trace_filename and step == -1: run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() else: run_options = None run_metadata = None summary_str = None start_time = time.time() if summary_op is None: results = sess.run(fetches, options=run_options, run_metadata=run_metadata) else: (results, summary_str) = sess.run( [fetches, summary_op], options=run_options, run_metadata=run_metadata) if not params.forward_only: lossval = results['total_loss'] else: lossval = 0. image_producer.notify_image_consumption() train_time = time.time() - start_time step_train_times.append(train_time) if step >= 0 and (step == 0 or (step + 1) % params.display_every == 0): log_str = '%i\t%s\t%.3f' % ( step + 1, get_perf_timing_str(batch_size, step_train_times), lossval) if 'top_1_accuracy' in results: log_str += '\t%.3f\t%.3f' % (results['top_1_accuracy'], results['top_5_accuracy']) log_fn(log_str) if trace_filename and step == -1: log_fn('Dumping trace to %s' % trace_filename) trace = timeline.Timeline(step_stats=run_metadata.step_stats) with gfile.Open(trace_filename, 'w') as trace_file: trace_file.write(trace.generate_chrome_trace_format(show_memory=True)) return summary_str
def benchmark_one_step(sess, fetches, step, batch_size, step_train_times, trace_filename, image_producer, params, summary_op=None): """Advance one step of benchmarking.""" if trace_filename is not None and step == -1: run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() else: run_options = None run_metadata = None summary_str = None start_time = time.time() if summary_op is None: results = sess.run(fetches, options=run_options, run_metadata=run_metadata) else: (results, summary_str) = sess.run( [fetches, summary_op], options=run_options, run_metadata=run_metadata) if not params.forward_only: lossval = results['total_loss'] else: lossval = 0. image_producer.notify_image_consumption() train_time = time.time() - start_time step_train_times.append(train_time) if step >= 0 and (step == 0 or (step + 1) % params.display_every == 0): log_str = '%i\t%s\t%.3f' % ( step + 1, get_perf_timing_str(batch_size, step_train_times), lossval) if 'top_1_accuracy' in results: log_str += '\t%.3f\t%.3f' % (results['top_1_accuracy'], results['top_5_accuracy']) log_fn(log_str) if trace_filename is not None and step == -1: log_fn('Dumping trace to %s' % trace_filename) trace = timeline.Timeline(step_stats=run_metadata.step_stats) with gfile.Open(trace_filename, 'w') as trace_file: trace_file.write(trace.generate_chrome_trace_format(show_memory=True)) return summary_str
def train_network(num_epochs, num_steps, state_size=4): sess.run(tf.initialize_all_variables()) # print("--- min for graph building ---",(time.time() - start_time)/60.0) # start_time = time.time() training_losses = [] # X_test, Y_test = genTestData(num_steps, num_test_runs, num_classes) X_test, Y_test = getTestData() for idx, (X_epoch,Y_epoch) in enumerate(genEpochs(num_epochs, num_data_points, num_steps, batch_size, num_classes)): training_loss = 0 acc = 0 num_batches = 0 training_state = [np.zeros((batch_size, state_size)) for i in range(num_stacked)] print("EPOCH %d" % idx) for batch in range(len(X_epoch)): X = X_epoch[batch] Y = Y_epoch[batch] (train_step_, loss_, train_summary_) = sess.run([train_step, loss, train_summary], feed_dict={x:X, y:Y}, options=run_options, run_metadata=run_metadata) training_loss += loss_ train_writer.add_summary(train_summary_, idx) num_batches += 1 (test_loss, test_summary_, accuracy_) = sess.run( [loss, test_summary, accuracy], feed_dict={x:X_test, y:Y_test}, options=run_options, run_metadata=run_metadata) train_writer.add_summary(test_summary_, idx) training_loss = training_loss/num_batches print("train loss:", training_loss, "test loss:", test_loss, "test accuracy:", accuracy_) training_loss = 0 tl = timeline.Timeline(run_metadata.step_stats) ctf = tl.generate_chrome_trace_format() with open('timeline_add.json', 'w') as f: f.write(ctf)
def train_network(num_epochs, num_steps, state_size=4): # with tf.Session() as sess: sess.run(tf.initialize_all_variables()) # print("--- min for graph building ---",(time.time() - start_time)/60.0) # start_time = time.time() training_losses = [] # (test_X_epoch,test_Y_epoch) = genData(num_data_points, num_steps, batch_size) test_X_epoch,test_Y_epoch = getTestData() for idx, (X_epoch,Y_epoch) in enumerate(genEpochs(num_epochs, num_data_points, num_steps, batch_size)): training_loss = 0 num_batches = 0 print("EPOCH %d" % idx) for batch in range(len(X_epoch)): X = X_epoch[batch] Y = Y_epoch[batch] (train_step_, loss_, summary_, prediction_) = sess.run([train_step, loss, summary, prediction], feed_dict={x:X, y:Y}, options=run_options, run_metadata=run_metadata) training_loss += loss_ train_writer.add_summary(summary_, idx) num_batches += 1 test_loss = 0 test_num_batches = 0 for test_batch in range(len(test_X_epoch)): X_test = test_X_epoch[test_batch] Y_test = test_Y_epoch[test_batch] (test_loss_, test_loss_summary_) = sess.run([loss, test_loss_summary], feed_dict={x:X_test, y:Y_test}, options=run_options, run_metadata=run_metadata) test_loss += test_loss_ train_writer.add_summary(test_loss_summary_, idx) test_num_batches += 1 test_loss = test_loss/test_num_batches training_loss = training_loss/num_batches print("train loss:", training_loss, "test loss", test_loss) training_loss = 0 test_loss = 0 # tl = timeline.Timeline(run_metadata.step_stats) # ctf = tl.generate_chrome_trace_format() # with open('./timelines/additionV2.json', 'w') as f: # f.write(ctf)
def load_data(self, sess, inputs, full_trace=False): """Bulk loads the specified inputs into device memory. The shape of the inputs must conform to the shapes of the input placeholders this optimizer was constructed with. The data is split equally across all the devices. If the data is not evenly divisible by the batch size, excess data will be discarded. Args: sess: TensorFlow session. inputs: List of Tensors matching the input placeholders specified at construction time of this optimizer. full_trace: Whether to profile data loading. Returns: The number of tuples loaded per device. """ feed_dict = {} assert len(self.input_placeholders) == len(inputs) for ph, arr in zip(self.input_placeholders, inputs): truncated_arr = make_divisible_by(arr, self.batch_size) feed_dict[ph] = truncated_arr truncated_len = len(truncated_arr) if full_trace: run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) else: run_options = tf.RunOptions(trace_level=tf.RunOptions.NO_TRACE) run_metadata = tf.RunMetadata() sess.run( [t.init_op for t in self._towers], feed_dict=feed_dict, options=run_options, run_metadata=run_metadata) if full_trace: trace = timeline.Timeline(step_stats=run_metadata.step_stats) trace_file = open(os.path.join(self.logdir, "timeline-load.json"), "w") trace_file.write(trace.generate_chrome_trace_format()) tuples_per_device = truncated_len / len(self.devices) assert tuples_per_device > 0, \ "Too few tuples per batch, trying increasing the training " \ "batch size or decreasing the sgd batch size. Tried to split up " \ "{} rows {}-ways in batches of {} (total across devices).".format( len(arr), len(self.devices), self.batch_size) assert tuples_per_device % self.per_device_batch_size == 0 return tuples_per_device
def optimize(self, sess, batch_index, extra_ops=[], extra_feed_dict={}, file_writer=None): """Run a single step of SGD. Runs a SGD step over a slice of the preloaded batch with size given by self.per_device_batch_size and offset given by the batch_index argument. Updates shared model weights based on the averaged per-device gradients. Args: sess: TensorFlow session. batch_index: Offset into the preloaded data. This value must be between `0` and `tuples_per_device`. The amount of data to process is always fixed to `per_device_batch_size`. extra_ops: Extra ops to run with this step (e.g. for metrics). extra_feed_dict: Extra args to feed into this session run. file_writer: If specified, tf metrics will be written out using this. Returns: The outputs of extra_ops evaluated over the batch. """ if file_writer: run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) else: run_options = tf.RunOptions(trace_level=tf.RunOptions.NO_TRACE) run_metadata = tf.RunMetadata() feed_dict = {self._batch_index: batch_index} feed_dict.update(extra_feed_dict) outs = sess.run( [self._train_op] + extra_ops, feed_dict=feed_dict, options=run_options, run_metadata=run_metadata) if file_writer: trace = timeline.Timeline(step_stats=run_metadata.step_stats) trace_file = open(os.path.join(self.logdir, "timeline-sgd.json"), "w") trace_file.write(trace.generate_chrome_trace_format()) file_writer.add_run_metadata( run_metadata, "sgd_train_{}".format(batch_index)) return outs[1:]
def train_step(sess, train_op, global_step, train_step_kwargs): """Function that takes a gradient step and specifies whether to stop. Args: sess: The current session. train_op: A dictionary of `Operation` that evaluates the gradients and returns the total loss (for first) in case of iter_size > 1. global_step: A `Tensor` representing the global training step. train_step_kwargs: A dictionary of keyword arguments. Returns: The total loss and a boolean indicating whether or not to stop training. """ start_time = time.time() if FLAGS.iter_size == 1: # for debugging specific endpoint values, # set the train file to one image and use # pdb here # import pdb # pdb.set_trace() if FLAGS.profile_iterations: run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() total_loss, np_global_step = sess.run([train_op, global_step], options=run_options, run_metadata=run_metadata) tl = timeline.Timeline(run_metadata.step_stats) ctf = tl.generate_chrome_trace_format() with open(os.path.join(FLAGS.train_dir, 'timeline_%08d.json' % np_global_step), 'w') as f: f.write(ctf) else: total_loss, np_global_step = sess.run([train_op, global_step]) else: for j in range(FLAGS.iter_size-1): sess.run([train_op[j]]) total_loss, np_global_step = sess.run( [train_op[FLAGS.iter_size-1], global_step]) time_elapsed = time.time() - start_time if 'should_log' in train_step_kwargs: if sess.run(train_step_kwargs['should_log']): logging.info('%s: global step %d: loss = %.4f (%.2f sec)', datetime.now(), np_global_step, total_loss, time_elapsed) if 'should_stop' in train_step_kwargs: should_stop = sess.run(train_step_kwargs['should_stop']) else: should_stop = False return total_loss, should_stop