def _create_HistogramProto(values, bins=50): # Convert to a numpy array values = np.array(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) return hist
def log_histogram(self, name, values, episode, bins=1000): """ Creates a histogram object for tensorboard (https://gist.github.com/gyglim/1f8dfb1b5c82627ae3efcfbbadb9f514#file-tensorboard_logging-py-L41) """ values = np.array(values) # values = values.eval(session=self.sess) # 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=name, histo=hist)]) self.writer.add_summary(summary, episode) self.writer.flush()
def add_histogram(self, tag, values, step, flush=False, bins=100, prefix=None): if prefix is not None: tag = prefix + '/' + tag if not isinstance(values, np.ndarray): values = np.array(values) counts, bin_edges = np.histogram(values, bins=bins) 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)) bin_edges = bin_edges[1:] for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.board.add_summary(summary, step) if flush: self.board.flush()
def log_histogram(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)) # 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, 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 custom_tensorflow_histogram(values, bins=100): import numpy as np import tensorflow as tf # Create histogram using numpy counts, bin_edges = np.histogram(values, bins=bins) # Fill fields of histogram proto hist = tf.HistogramProto() if len(values): 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)) else: hist.min = 0 hist.max = 0 hist.num = 0 hist.sum = 0 hist.sum_squares = 0 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(int(c)) return hist
def log_histogram(writer, tag, values, step, bins=1000, min=None, max=None, density=False): """Logs the histogram of a list/vector of values.""" values = np.array(values) # Fill fields of histogram proto hist = tf.HistogramProto() hist.min = float(np.min(values)) if min is None else float(min) hist.max = float(np.max(values)) if min is None else float(max) hist.num = int(np.prod(values.shape)) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values**2)) # Create histogram using numpy counts, bin_edges = np.histogram(values, bins=bins, range=(hist.min, hist.max), density=density) # 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)]) writer.add_summary(summary, step) writer.flush()
def histo_summary(self, tag, values, step, bins=1000): """Log a histogram of the tensor of values.""" # numpyでヒストグラムを作る counts, bin_edges = np.histogram(values, bins=bins) # ヒストグラムに基づき塗る 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)) # 最初のbinを捨てる bin_edges = bin_edges[1:] # エッジとカウントを追加 for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) # 要約を作成して書き込み summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, step) self.writer.flush()
def log_function(writer, tag, grid, grid_fn, step, agg='sum'): """Logs the histogram of a list/vector of values.""" values = np.array(grid_fn) if len(grid.shape)==1: grid.reshape(-1, 1) for d in range(grid.shape[1]): bin_edges = np.unique(grid[:, d]) counts = np.zeros(bin_edges.shape[0]) for it, edge in enumerate(bin_edges): if agg=='sum': counts[it] = np.sum(values[grid[:, d] == edge]) else: counts[it] = np.mean(values[grid[:, d] == edge]) # Fill fields of histogram proto hist = tf.HistogramProto() hist.min = float(np.min(grid[:, d])) hist.max = float(np.max(grid[:, d])) # 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="{}_{}".format(tag, d), histo=hist)]) writer.add_summary(summary, step)
def histo_summary(self, tag, values, step, bins=1000, increment_counter=True): """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 with self.writer.tf_writer.as_default(): tf.summary.histogram(tag, hist, buckets=hist.bucket)
def MakeHistSummary(name, values): """Logs the histogram of a list/vector of values.""" values = numpy.array(values) # Create histogram using numpy counts, bin_edges = numpy.histogram(values, bins=4716) # Fill fields of histogram proto hist = tf.HistogramProto() hist.min = float(numpy.min(values)) hist.max = float(numpy.max(values)) hist.num = int(numpy.prod(values.shape)) hist.sum = float(numpy.sum(values)) hist.sum_squares = float(numpy.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=name, histo=hist)]) return summary
def histogram_summary(self, tag, tensor, step): """ From the TF documentation: tf.summary.histogram takes an arbitrarily sized and shaped Tensor, and compresses it into a histogram data structure consisting of many bins with widths and counts. TensorFlow uses non-uniformly distributed bins, which is better than using numpy's uniform bins for activations and parameters which converge around zero, but we don't add that logic here. https://www.tensorflow.org/programmers_guide/tensorboard_histograms """ hist, edges = np.histogram(tensor, bins=200) tfhist = tf.HistogramProto( min=np.min(tensor), max=np.max(tensor), num=int(np.prod(tensor.shape)), sum=np.sum(tensor), sum_squares=np.sum(np.square(tensor)), ) # From the TF documentation: # Parallel arrays encoding the bucket boundaries and the bucket values. # bucket(i) is the count for the bucket i. The range for a bucket is: # i == 0: -DBL_MAX .. bucket_limit(0) # i != 0: bucket_limit(i-1) .. bucket_limit(i) tfhist.bucket_limit.extend(edges[1:]) tfhist.bucket.extend(hist) summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=tfhist)]) self.writers[0].add_summary(summary, step)
def _log_histogram(writer, tag, values, step, bins=1000): """ Logs the histogram of a list/vector of values. ref: https://gist.github.com/FireJohnny/1f82b5f7a3eabbdc7aacdb967fe1b557 """ # Convert to a numpy array values = np.array(values) values = np.sort(values)[:int(len(values) * 99 / 100)] # 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)]) writer.add_summary(summary, step)
def add_histogram(writer, tag, values, step, bins=1000): """ Logs the histogram of a list/vector of values. From: https://gist.github.com/gyglim/1f8dfb1b5c82627ae3efcfbbadb9f514 """ # 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)]) 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 make_histogram(values, bins=512): # From https://gist.github.com/gyglim/1f8dfb1b5c82627ae3efcfbbadb9f514 # License: BSD License 2.0 # Author Michael Gygli # Logs the histogram of a list/vector of values. # Convert to a numpy array values = np.array(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 return hist
def histogram(tag, values, bins=1000): """ Construct a histogram summary. :param tag: :param values: :param bins: """ values = np.array(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)]) return summary
def histo_summary(self, values, bins=100): """Helper function in train method. Log a histogram of the tensor of values for tensorboard. Creates a HistogramProto instance that can be fed into Tensorboard. Parameters --------- values : (np.array) histogram values bins : (int) how coarse the histogram is supposed to be """ # 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) return hist
def log_bar(self, tag, values, xs=None, step=None): if not step: step = self.step values = np.asarray(values).flatten() if not xs: axises = list(range(len(values))) else: axises = xs hist = tf.HistogramProto() hist.min = float(min(axises)) hist.max = float(max(axises)) hist.num = sum(values) hist.sum = sum([y * x for (x, y) in zip(axises, values)]) hist.sum_squares = sum([y * (x**2) for (x, y) in zip(axises, values)]) for edge in axises: hist.bucket_limit.append(edge - 1e-10) hist.bucket_limit.append(edge + 1e-10) for c in values: hist.bucket.append(0) hist.bucket.append(c) summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, self.step) self.writer.flush()
def histogram(self, data, global_step, bins=1000, tag='histogram'): """Logs the histogram of a list/vector of data.""" data = np.array(data) # Create histogram using numpy counts, bin_edges = np.histogram(data, bins=bins) # Fill fields of histogram proto hist = tf.HistogramProto() hist.min = float(np.min(data)) hist.max = float(np.max(data)) hist.num = int(np.prod(data.shape)) hist.sum = float(np.sum(data)) hist.sum_squares = float(np.sum(data**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.summary_writer.add_summary(summary, global_step) self.summary_writer.flush()
def _GenerateTestData(self): """Generates the test data directory. The test data has a single run named run1 which contains: - a histogram - an image at timestamp and step 0 - scalar events containing the value i at step 10 * i and wall time 100 * i, for i in [1, _SCALAR_COUNT). - a graph definition """ temp_dir = self.get_temp_dir() self.addCleanup(shutil.rmtree, temp_dir) run1_path = os.path.join(temp_dir, 'run1') os.makedirs(run1_path) writer = tf.train.SummaryWriter(run1_path) histogram_value = tf.HistogramProto(min=0, max=2, num=3, sum=6, sum_squares=5, bucket_limit=[0, 1, 2], bucket=[1, 1, 1]) # Add a simple graph event. graph_def = tf.GraphDef() node1 = graph_def.node.add() node1.name = 'a' node2 = graph_def.node.add() node2.name = 'b' node2.attr['very_large_attr'].s = b'a' * 2048 # 2 KB attribute writer.add_event(tf.Event(graph_def=graph_def.SerializeToString())) # 1x1 transparent GIF. encoded_image = base64.b64decode( 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7') image_value = tf.Summary.Image(height=1, width=1, colorspace=1, encoded_image_string=encoded_image) writer.add_event(tf.Event(wall_time=0, step=0, summary=tf.Summary(value=[tf.Summary.Value( tag='histogram', histo=histogram_value), tf.Summary.Value( tag='image', image=image_value)]))) # Write 100 simple values. for i in xrange(1, self._SCALAR_COUNT + 1): writer.add_event(tf.Event( # We use different values for wall time, step, and the value so we can # tell them apart. wall_time=100 * i, step=10 * i, summary=tf.Summary(value=[tf.Summary.Value(tag='simple_values', simple_value=i)]))) writer.flush() writer.close()
def AddHistogram(self, tag, wall_time=0, step=0, hmin=1, hmax=2, hnum=3, hsum=4, hsum_squares=5, hbucket_limit=None, hbucket=None): histo = tf.HistogramProto(min=hmin, max=hmax, num=hnum, sum=hsum, sum_squares=hsum_squares, bucket_limit=hbucket_limit, bucket=hbucket) event = tf.Event( wall_time=wall_time, step=step, summary=tf.Summary(value=[tf.Summary.Value(tag=tag, histo=histo)])) self.AddEvent(event)
def write_episode_summary(self, ms_sf, ms_aux, ms_option, r): if len(self.episode_rewards) != 0: last_reward = self.episode_rewards[-1] self.summary.value.add(tag='Perf/Reward', simple_value=float(last_reward)) if len(self.episode_lengths) != 0: last_length = self.episode_lengths[-1] self.summary.value.add(tag='Perf/Length', simple_value=float(last_length)) if len(self.episode_mean_values) != 0: last_mean_value = self.episode_mean_values[-1] self.summary.value.add(tag='Perf/Value', simple_value=float(last_mean_value)) if len(self.episode_mean_q_values) != 0: last_mean_q_value = self.episode_mean_q_values[-1] self.summary.value.add(tag='Perf/QValue', simple_value=float(last_mean_q_value)) if len(self.episode_mean_oterms) != 0: last_mean_oterm = self.episode_mean_oterms[-1] self.summary.value.add(tag='Perf/Oterm', simple_value=float(last_mean_oterm)) if len(self.episode_mean_options) != 0: last_frequent_option = self.episode_mean_options[-1] self.summary.value.add(tag='Perf/FreqOptions', simple_value=last_frequent_option) if len(self.episode_mean_options) != 0: last_frequent_action = self.episode_mean_actions[-1] self.summary.value.add(tag='Perf/FreqActions', simple_value=last_frequent_action) if len(self.episode_options) != 0: counts, bin_edges = np.histogram( self.episode_options, bins=list(range(self.config.nb_options)) + [self.config.nb_options]) hist = tf.HistogramProto(min=np.min(self.episode_options), max=np.max(self.episode_options), num=len(self.episode_options), sum=np.sum(self.episode_options), sum_squares=np.sum( [e**2 for e in self.episode_options])) 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) self.summary.value.add(tag='Perf/OptionsHist', histo=hist) self.summary_writer.add_summary(self.summary, self.total_steps) self.write_step_summary(ms_sf, ms_aux, ms_option, r) self.summary_writer.flush()
def train(self, n): """ train for n episodes """ rewards = [] c0 = np.zeros([1, N_H]) h0 = np.zeros([1, N_H]) i = 0 # episode index sig = StopRequest() sig.start() while not sig._stop: # TODO : hardcoded max_step net_reward, entry = self.train_1(c0, h0, GAME_STEPS-2) #if self._step > N_PRE: # self.update() if len(entry) >= N_TRACE: self._memory.add(np.asarray(entry)) rewards.append(net_reward) if (i % N_LOG) == 0 and i > 0: rw = rewards[-N_LOG:] r_mean = np.mean(rw) r_min = np.min(rw) r_max = np.max(rw) # Create histogram using numpy counts, bin_edges = np.histogram(rw, bins=16) bin_edges = bin_edges[1:] hist = tf.HistogramProto() hist.min = r_min hist.max = r_max hist.num = np.prod(np.shape(rw)) hist.sum = np.sum(rw) hist.sum_squares = np.sum(np.square(rw)) [hist.bucket_limit.append(e) for e in bin_edges] [hist.bucket.append(c) for c in counts] self._writer.add_summary(tf.Summary( value=[tf.Summary.Value(tag='reward/hist', histo=hist)] ), self._step) self.add_summary('reward/mean', r_mean) self.add_summary('reward/min', r_min) self.add_summary('reward/max', r_max) self.add_summary('logs/eps', self._eps) print "[%d:%d] r(mean,max) (%.2f,%.2f) | Eps: %f" % (i, self._step, r_mean, r_max, self._eps) i += 1 sig.stop() # i.e. stop handling signals return rewards
def testExample(self): bps = (0, 2500, 5000, 7500, 10000) proto = tf.HistogramProto(min=1, max=2, num=3, sum=4, sum_squares=5, bucket_limit=[1, 2, 3], bucket=[0, 3, 0]) self.assertEqual( _make_expected_value((0, 1.0), (2500, 1.25), (5000, 1.5), (7500, 1.75), (10000, 2.0)), compressor.CompressHistogram(proto, bps))
def testAnotherExample(self): bps = (0, 2500, 5000, 7500, 10000) proto = tf.HistogramProto(min=-2, max=3, num=4, sum=5, sum_squares=6, bucket_limit=[2, 3, 4], bucket=[1, 3, 0]) self.assertEqual( _make_expected_value((0, -2), (2500, 2), (5000, 2 + 1 / 3), (7500, 2 + 2 / 3), (10000, 3)), compressor.CompressHistogram(proto, bps))
def testEmpty(self): bps = (0, 2500, 5000, 7500, 10000) proto = tf.HistogramProto(min=None, max=None, num=0, sum=0, sum_squares=0, bucket_limit=[1, 2, 3], bucket=[0, 0, 0]) self.assertEqual( _make_expected_value((0, 0), (2500, 0), (5000, 0), (7500, 0), (10000, 0)), compressor.CompressHistogram(proto, bps))
def _histogram(self, metric, step=0): values = np.array(metric.raw_values) histo = tf.HistogramProto( min=np.amin(values), max=np.amax(values), num=len(values), sum=np.sum(values), sum_squares=np.sum(values**2), bucket_limit=metric.value['bin_edges'][1:].astype(np.float64), bucket=metric.value['bucket'].astype(np.float64)) summary = tf.Summary( value=[tf.Summary.Value(tag=metric.tag, histo=histo)]) self._add_event(summary, step)
def add_histogram(self, tag, values, step, bins=1000): counts, bin_edges = np.histogram(values, bins=bins) 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(np.square(values))) bin_edges = bin_edges[1:] for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.add_summary(summary, step)
def log_histogram(self, histogram, tag, global_step): tf_hist = tf.HistogramProto() bin_edges = histogram[1][1:] for edge in bin_edges: tf_hist.bucket_limit.append(edge) for count in histogram[0]: tf_hist.bucket.append(count) tf_hist.min = histogram[1][0] tf_hist.max = histogram[1][-1] summary = tf.Summary() summary.value.add(tag=tag, histo=tf_hist) self.writer.add_summary(summary, global_step=global_step) self.writer.flush()
def inferred_histo(summary, samples=1000): np.random.seed( hash(summary.std + summary.mean + summary.min + summary.max)) samples = np.random.randn(samples) * summary.std + summary.mean samples = np.clip(samples, a_min=summary.min, a_max=summary.max) (hist, edges) = np.histogram(samples) upper_edges = edges[1:] r = tf.HistogramProto(min=summary.min, max=summary.max, num=len(samples), sum=samples.sum(), sum_squares=(samples * samples).sum()) r.bucket_limit.extend(upper_edges) r.bucket.extend(hist) return r