我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.Summary()。
def log_values(writer, itr, tags=None, values=None, dict=None): if dict is not None: assert tags is None and values is None tags = dict.keys() values = dict.values() else: if not nest.is_sequence(tags): tags, values = [tags], [values] elif len(tags) != len(values): raise ValueError('tag and value have different lenghts:' ' {} vs {}'.format(len(tags), len(values))) for t, v in zip(tags, values): summary = tf.Summary.Value(tag=t, simple_value=v) summary = tf.Summary(value=[summary]) writer.add_summary(summary, itr)
def image_summary(self, tag, images, step): """Log a list of images.""" img_summaries = [] for i, img in enumerate(images): # Write the image to a string try: s = StringIO() except: s = BytesIO() scipy.misc.toimage(img).save(s, format="png") # Create an Image object img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(), height=img.shape[0], width=img.shape[1]) # Create a Summary value img_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, i), image=img_sum)) # Create and write Summary summary = tf.Summary(value=img_summaries) self.writer.add_summary(summary, step)
def log_images(self, tag, images, step): """Logs a list of images.""" image_summaries = [] for image_num, image in enumerate(images): # Write the image to a string try: # Python 2.7 s = StringIO() toimage(image).save(s, format="png") except TypeError: # Python 3.X s = BytesIO() toimage(image).save(s, format="png") # Create an Image object img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(), height=image.shape[0], width=image.shape[1]) # Create a Summary value image_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, image_num), image=img_sum)) # Create and write Summary summary = tf.Summary(value=image_summaries) self.writer.add_summary(summary, step)
def log(self, rewards, v_l, p_l, e_l, g_n, v_n, mean_advantages_m): print(str(self.name), " episode_count", self.episode_count) summary = tf.Summary() summary.value.add(tag='Perf/Reward', simple_value=float(rewards)) # summary.value.add(tag='Perf/Length', simple_value=float(mean_length)) # summary.value.add(tag='Perf/Value', simple_value=float(mean_value)) summary.value.add(tag='Losses/Value Loss', simple_value=float(v_l)) summary.value.add(tag='Losses/Policy Loss', simple_value=float(p_l)) summary.value.add(tag='Losses/Entropy', simple_value=float(e_l)) summary.value.add(tag='Losses/Grad Norm', simple_value=float(g_n)) summary.value.add(tag='Losses/Var Norm', simple_value=float(v_n)) summary.value.add(tag='Losses/mean_advantages_m', simple_value=float(mean_advantages_m)) self.summary_writer.add_summary(summary, self.episode_count) self.summary_writer.flush() pass
def relpath(name, scope_name): m = scope_name + '/' end = ':0' if not (name.startswith(m) and name.endswith(end)): raise Exception("'{}' should start with '{}' and end with {}.".format(name, m, end)) return name[len(m):-len(end)] # def get_value(var): # return var.eval(session=fu.get_session()) # # # def set_value(var, value): # var.initializer.run({var.initial_value: value}, fu.get_session()) # class SummaryWriter: # def __init__(self, tf_summary_writer): # self.writer = tf_summary_writer # # def write_scalar(self, tag, val): # s = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=val)]) # self.writer.add_summary(s, self.t)
def _tensorboard_summary_writer(self, batch_total_loss): # if self.global_step == self.optimization_start: if self.create_list_for_total_losses: self.create_list_for_total_losses = False self._loss_list = [] self._mean_size = 0 self._loss_list.append(batch_total_loss) self._mean_size += 1 if self._mean_size % 10 == 0: mean_total_loss = np.mean(self._loss_list) summary = tf.Summary() summary.value.add(tag='Total Loss', simple_value=float(mean_total_loss)) self.summary_writer.add_summary(summary, self.global_step) self.summary_writer.flush() self._loss_list = [] self._mean_size = 0
def save_variables_and_metagraph(sess, saver, summary_writer, model_dir, model_name, step): # Save the model checkpoint print('Saving variables') start_time = time.time() checkpoint_path = os.path.join(model_dir, 'model-%s.ckpt' % model_name) saver.save(sess, checkpoint_path, global_step=step, write_meta_graph=False) save_time_variables = time.time() - start_time print('Variables saved in %.2f seconds' % save_time_variables) metagraph_filename = os.path.join(model_dir, 'model-%s.meta' % model_name) save_time_metagraph = 0 if not os.path.exists(metagraph_filename): print('Saving metagraph') start_time = time.time() saver.export_meta_graph(metagraph_filename) save_time_metagraph = time.time() - start_time print('Metagraph saved in %.2f seconds' % save_time_metagraph) summary = tf.Summary() #pylint: disable=maybe-no-member summary.value.add(tag='time/save_variables', simple_value=save_time_variables) summary.value.add(tag='time/save_metagraph', simple_value=save_time_metagraph) summary_writer.add_summary(summary, step)
def run_testing(sess,ep): acc = 0.0 loss = 0.0 pre_index = 0 add = 1000 for it in range(10): batch_x = test_x[pre_index:pre_index+add] batch_y = test_y[pre_index:pre_index+add] pre_index = pre_index + add loss_, acc_ = sess.run([cross_entropy,accuracy],feed_dict={x:batch_x, y_:batch_y, keep_prob: 1.0, train_flag: False}) loss += loss_ / 10.0 acc += acc_ / 10.0 summary = tf.Summary(value=[tf.Summary.Value(tag="test_loss", simple_value=loss), tf.Summary.Value(tag="test_accuracy", simple_value=acc)]) return acc, loss, summary # ========================================================== # # ?? main() # Training and Testing # Save train/teset loss and acc for visualization # Save Model in ./model # ========================================================== #
def write_summary(self, value, writer, step): # Check lengths are consistent names = self.define_names() value = np.squeeze(value) if len(value) != len(names): raise RuntimeError('Provided list of values is not consistent ' + ' with bins defined.') # Store each summary independently for i, n in enumerate(names): summ_name = self.interval_name(names[i]) summ_tag = '/'.join([self.get_label(), summ_name]) summ = tf.Summary( value=[tf.Summary.Value(tag=summ_tag, simple_value=float(value[i]))] ) writer.add_summary(summ, step)
def test_run(self, session, summary_ops, step, data_mode, log=True): """ Runs an evaluation step Args: session: Tensorflow session summary_ops: Summary operations for Tensorboard step: Step number to assign to the run data_mode: Evaluation mode (validation or testing) log: Whether to log step """ run = self._run(session=session, summary_ops=summary_ops, is_training=False) run.step = step if log: run.log('Evaluation {}'.format(str(data_mode))) return run
def add_summary_simple_value(writer, tag, step, simple_value): """Adds an event to the writer writer: tf.summary.FileWriter tag: str tag for the value step: int the global step simple_value: float or int A simple scalar float value """ s = tf.Summary() s.value.extend([tf.Summary.Value(tag=tag,simple_value=simple_value)]) writer.add_summary(s, global_step=step)
def tb_add_histogram(experiment, name, wall_time, step, histo): # Tensorflow does not support key being unicode histo_string = {} for k,v in histo.items(): histo_string[str(k)] = v histo = histo_string writer = tb_get_xp_writer(experiment) summary = tf.Summary(value=[ tf.Summary.Value(tag=name, histo=histo), ]) event = tf.Event(wall_time=wall_time, step=step, summary=summary) writer.add_event(event) writer.flush() tb_modified_xp(experiment, modified_type="histograms", wall_time=wall_time) # Perform requests to tensorboard http api
def save_scalar(step, name, value, writer): """Save a scalar value to tensorboard. Parameters ---------- step: int Training step (sets the position on x-axis of tensorboard graph. name: str Name of variable. Will be the name of the graph in tensorboard. value: float The value of the variable at this step. writer: tf.FileWriter The tensorboard FileWriter instance. """ summary = tf.Summary() summary_value = summary.value.add() summary_value.simple_value = float(value) summary_value.tag = name writer.add_summary(summary, step)
def log_images(self, tag, images, step): """Logs a list of images.""" im_summaries = [] for nr, img in enumerate(images): # Write the image to a string s = StringIO() plt.imsave(s, img, format='png') # Create an Image object img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(), height=img.shape[0], width=img.shape[1]) # Create a Summary value im_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, nr), image=img_sum)) # Create and write Summary summary = tf.Summary(value=im_summaries) self.writer.add_summary(summary, step)
def MakeSummary(name, value): """Creates a tf.Summary proto with the given name and value.""" summary = tf.Summary() val = summary.value.add() val.tag = str(name) val.simple_value = float(value) return summary
def write_summaries(self, X, y, label, step, summary_writer=None): if not X: return y_pred, loss = self.predict_proba_with_loss(X, y) metrics = classification_metrics(y, y_pred, self.threshold) metrics['loss'] = loss if summary_writer is not None: summary = tf.Summary() for key, value in metrics.items(): summary.value.add(tag="metrics/{}".format(key), simple_value=float(value)) if not self.summary_tensors: self.summary_tensors["positive_predictions_input"] = tf.placeholder( tf.float32, [None], "positive_predictions_input") self.summary_tensors["positive_predictions"] = tf.summary.histogram( "positive_predictions", self.summary_tensors["positive_predictions_input"]) self.summary_tensors["negative_predictions_input"] = tf.placeholder( tf.float32, [None], "negative_predictions_input") self.summary_tensors["negative_predictions"] = tf.summary.histogram( "negative_predictions", self.summary_tensors["negative_predictions_input"]) summary_writer.add_summary( self.summary_tensors["positive_predictions"].eval( feed_dict={self.summary_tensors["positive_predictions_input"]: y_pred[y]}), step) summary_writer.add_summary( self.summary_tensors["negative_predictions"].eval( feed_dict={self.summary_tensors["negative_predictions_input"]: y_pred[~y]}), step) summary_writer.add_summary(summary, step) summary_writer.flush()
def process(self, sess): """ process grabs a rollout that's been produced by the thread runner, and updates the parameters. The update is then sent to the parameter server. """ sess.run(self.sync) # copy weights from shared to local rollout = self.pull_batch_from_queue() batch = process_rollout(rollout, self.gamma, lambda_=1.0) should_compute_summary = self.task == 0 and self.local_steps % 11 == 0 if should_compute_summary: fetches = [self.summary_op, self.train_op, self.global_step] else: fetches = [self.train_op, self.global_step] feed_dict = { self.local_network.x: batch.si, self.ac: batch.a, self.adv: batch.adv, self.r: batch.r, } for k, v in zip(self.local_network.state_in, batch.features): feed_dict[k] = v fetched = sess.run(fetches, feed_dict=feed_dict) if should_compute_summary: self.summary_writer.add_summary(tf.Summary.FromString(fetched[0]), fetched[-1]) self.summary_writer.flush() self.local_steps += 1
def scalar_summary(self, tag, value, step): summary = tf.Summary(value = [tf.Summary.Value(tag = tag, simple_value = value)]) self.writer.add_summary(summary, step) self.writer.flush()
def histo_summary(self, tag, values, step, bins = 1000): hist = tf.HistogramProto() hist.min = float(np.min(values)) hist.max = float(np.max(values)) hist.num = int(np.prod(values.shape)) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values ** 2)) counts, edges = np.histogram(values, bins = bins) for edge in edges[1:]: hist.bucket_limit.append(edge) for count in counts: hist.bucket.append(count) summary = tf.Summary(value = [tf.Summary.Value(tag = tag, histo = hist)]) self.writer.add_summary(summary, step) self.writer.flush()
def make_summary(value_dict): """Make tf.Summary for tensorboard""" return tf.Summary(value=[tf.Summary.Value(tag=k, simple_value=v) for k, v in value_dict.items()])
def summary(value_dict, global_step, writer): """Make tf.Summary for tensorboard""" summary = tf.Summary(value=[tf.Summary.Value(tag=k, simple_value=v) for k, v in value_dict.items()]) writer.add_summary(summary, global_step) return None
def scalar_summary(self, tag, value, step): """Log a scalar variable.""" summary = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=value)]) self.writer.add_summary(summary, step)
def histo_summary(self, tag, values, step, bins=1000): """Log a histogram of the tensor of values.""" # Create a histogram using numpy counts, bin_edges = np.histogram(values, bins=bins) # Fill the fields of the histogram proto hist = tf.HistogramProto() hist.min = float(np.min(values)) hist.max = float(np.max(values)) hist.num = int(np.prod(values.shape)) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values**2)) # Drop the start of the first bin bin_edges = bin_edges[1:] # Add bin edges and counts for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) # Create and write Summary summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, step) self.writer.flush()
def log_scalar(self, tag, value, step): summary = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=value)]) self.writer.add_summary(summary, step)
def log_histogram(self, tag, values, step, bins=1000): # Create histogram using numpy values = np.array(values) counts, bin_edges = np.histogram(values, bins=bins) # Fill fields of histogram proto hist = tf.HistogramProto() hist.min = float(np.min(values)) hist.max = float(np.max(values)) hist.num = int(np.prod(np.shape(values))) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values ** 2)) # Requires equal number as bins, where the first goes from -DBL_MAX to bin_edges[1] # See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/summary.proto#L30 # Thus, we drop the start of the first bin bin_edges = bin_edges[1:] # Add bin edges and counts for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) # Create and write Summary summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, step) self.writer.flush()
def _evaluate_on_validation(self, get_val_instance_generator, batch_size, num_val_steps, session): val_batch_gen = DataManager.get_batch_generator( get_val_instance_generator, batch_size) # Calculate the mean of the validation metrics # over the validation set. val_accuracies = [] val_losses = [] for val_batch in tqdm(val_batch_gen, total=num_val_steps, desc="Validation Batches Completed", leave=False): feed_dict = self._get_validation_feed_dict(val_batch) val_batch_acc, val_batch_loss = session.run( [self.accuracy, self.loss], feed_dict=feed_dict) val_accuracies.append(val_batch_acc) val_losses.append(val_batch_loss) # Take the mean of the accuracies and losses. # TODO/FIXME this assumes each batch is same shape, which # is not necessarily true. mean_val_accuracy = np.mean(val_accuracies) mean_val_loss = np.mean(val_losses) # Create a new Summary object with mean_val accuracy # and mean_val_loss and add it to Tensorboard. val_summary = tf.Summary(value=[ tf.Summary.Value(tag="val_summaries/loss", simple_value=mean_val_loss), tf.Summary.Value(tag="val_summaries/accuracy", simple_value=mean_val_accuracy)]) return mean_val_accuracy, mean_val_loss, val_summary
def Evaluate(sess): test_acc = 0.0 test_loss = 0.0 test_pre_index = 0 add = 1000 for it in range(test_iteration): test_batch_x = test_x[test_pre_index: test_pre_index + add] test_batch_y = test_y[test_pre_index: test_pre_index + add] test_pre_index = test_pre_index + add test_feed_dict = { x: test_batch_x, label: test_batch_y, learning_rate: epoch_learning_rate, training_flag: False } loss_, acc_ = sess.run([cost, accuracy], feed_dict=test_feed_dict) test_loss += loss_ / 10.0 test_acc += acc_ / 10.0 summary = tf.Summary(value=[tf.Summary.Value(tag='test_loss', simple_value=test_loss), tf.Summary.Value(tag='test_accuracy', simple_value=test_acc)]) return test_acc, test_loss, summary
def histo_summary(self, tag, values, step, bins=1000): """Log a histogram of the tensor of values.""" # Create a histogram using numpy counts, bin_edges = np.histogram(values, bins=bins) # Fill the fields of the histogram proto hist = tf.HistogramProto() hist.min = float(np.min(values)) hist.max = float(np.max(values)) hist.num = int(np.prod(values.shape)) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values ** 2)) # Drop the start of the first bin bin_edges = bin_edges[1:] # Add bin edges and counts for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) # Create and write Summary summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, step) self.writer.flush()
def epoch_begin(self): self.epoch_id = self.gstep_val // self.train.nbatches summary_val = tf.Summary.Value(tag='control_flow/Epoch', simple_value=self.epoch_id + 1) summary = tf.Summary(value=[summary_val]) self.sv.summary_computed(self.sess, summary, global_step=self.epoch_id) self.pbar = tqdm(total=self.train.nbatches, bar_format='{n_fmt}/{total_fmt}{desc}' '{percentage:3.0f}%|{bar}| ' '[{elapsed}<{remaining},' '{rate_fmt}{postfix}]')
def test_finalize_stats_summaries(self): p = plan.Plan(None) p.save_summaries_secs = 42 p.losses['foo'] = tf.constant([1.0]) p.losses['bar'] = tf.constant([2.0, 3.0]) p.metrics['baz'] = tf.constant(4) p.metrics['qux'] = tf.constant([5.0, 6.0]) p.finalize_stats() with self.test_session(): self.assertEqual(6, p.loss_total.eval({p.batch_size_placeholder: 1})) summary = tf.Summary() summary.ParseFromString(p.summaries.eval({p.batch_size_placeholder: 1})) qux_string = tf.summary.histogram('qux', [5, 6]).eval() qux_proto = tf.Summary() qux_proto.ParseFromString(qux_string) qux_histogram = qux_proto.value[0].histo expected_values = [ tf.Summary.Value(tag='foo', simple_value=1), tf.Summary.Value(tag='bar', simple_value=5), tf.Summary.Value(tag='loss_total', simple_value=6), tf.Summary.Value(tag='baz', simple_value=4), tf.Summary.Value(tag='qux', histo=qux_histogram)] six.assertCountEqual(self, expected_values, summary.value) summary.ParseFromString(p.summaries.eval({p.batch_size_placeholder: 2})) expected_values = [ tf.Summary.Value(tag='foo', simple_value=0.5), tf.Summary.Value(tag='bar', simple_value=2.5), tf.Summary.Value(tag='loss_total', simple_value=3), tf.Summary.Value(tag='baz', simple_value=4), tf.Summary.Value(tag='qux', histo=qux_histogram)] six.assertCountEqual(self, expected_values, summary.value)
def emit_values(supervisor, session, step, values): summary = tf.Summary() for name, value in six.iteritems(values): summary_value = summary.value.add() summary_value.tag = name summary_value.simple_value = float(value) supervisor.summary_computed(session, summary, global_step=step)
def add_summary_value(writer, key, value, iteration): summary = tf.Summary(value=[tf.Summary.Value(tag=key, simple_value=value)]) writer.add_summary(summary, iteration)
def log_scalar(self, tag, value, step): """ Parameter ---------- tag : basestring Name of the scalar value step : int training iteration """ summary = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=value)]) self.writer.add_summary(summary, step)
def log_histogram(self, tag, values, step, bins=1000): """Logs the histogram of a list/vector of values.""" # Create histogram using numpy counts, bin_edges = np.histogram(values, bins=bins) # Fill fields of histogram proto hist = tf.HistogramProto() hist.min = float(np.min(values)) hist.max = float(np.max(values)) hist.num = int(np.prod(values.shape)) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values**2)) # Requires equal number as bins, where the first goes from -DBL_MAX to bin_edges[1] # See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/summary.proto#L30 # Thus, we drop the start of the first bin bin_edges = bin_edges[1:] # Add bin edges and counts for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) # Create and write Summary summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, step) self.writer.flush()