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)
def histogram_summary(self, tag, values, step, bins=1000): if self.use_tensorboard: """ 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 # Therefore 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)
def get_histogram(values): hist = tf_v1.HistogramProto() # Fill attributes in the histogram Proto counts, bin_edges = np.histogram(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)) hist.bucket_limit = bin_edges[1:] hist.bucket = counts return hist
def make_tf_summary_histogram(values, num_bins=10): """Constructs a tf Summary of type histogram from a np array of values. Args: values: list or np.array. num_bins: int. Number of histogram bins. Returns: tf.HistogramProto. """ values = np.reshape(values, [-1]) counts, limits = np.histogram(values, bins=num_bins) return tf.HistogramProto(min=np.amin(values), max=np.amax(values), num=values.size, sum=np.sum(values), sum_squares=np.sum(values**2), bucket_limit=limits.tolist()[1:], bucket=counts.tolist())
def make_histogram_proto(data, bins_count=None): # number of elements in the array elem_count = np.prod(data.shape) # Make sure the number of bins is defined and # doesn't exceed the nume of element if bins_count is None: bins_count = elem_count else: bins_count = np.min([bins_count, elem_count]).astype(np.int) # compute histogram using numpy occurrences, bin_edges = np.histogram(data, bins=bins_count) return tf.HistogramProto( min=data.min().astype(np.float), max=data.min().astype(np.float), num=elem_count.astype(np.int), sum=np.sum(data).astype(np.float), sum_squares=np.sum([datum * datum for datum in data]).astype(np.float), bucket_limit=bin_edges[1:].tolist(), bucket=occurrences.tolist())