Beispiel #1
0
    def histogram(self, tag, values, bins, step=None):
        """Saves histogram of values.

    Args:
      tag: str: label for this data
      values: ndarray: will be flattened by this routine
      bins: number of bins in histogram, or array of bins for onp.histogram
      step: int: training step
    """
        if step is None:
            step = self._step
        else:
            self._step = step
        values = onp.array(values)
        bins = onp.array(bins)
        values = onp.reshape(values, -1)
        counts, limits = onp.histogram(values, bins=bins)
        # boundary logic
        cum_counts = onp.cumsum(onp.greater(counts, 0, dtype=onp.int32))
        start, end = onp.searchsorted(cum_counts, [0, cum_counts[-1] - 1],
                                      side='right')
        start, end = int(start), int(end) + 1
        counts = (counts[start - 1:end]
                  if start > 0 else onp.concatenate([[0], counts[:end]]))
        limits = limits[start:end + 1]
        sum_sq = values.dot(values)
        histo = HistogramProto(min=values.min(),
                               max=values.max(),
                               num=len(values),
                               sum=values.sum(),
                               sum_squares=sum_sq,
                               bucket_limit=limits.tolist(),
                               bucket=counts.tolist())
        summary = Summary(value=[Summary.Value(tag=tag, histo=histo)])
        self.add_summary(summary, step)
Beispiel #2
0
def log_histogram(tag, values, step, bins=1000):
    """
    log_histogram
    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 = 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 Summary(value=[Summary.Value(tag=tag, histo=hist)])
Beispiel #3
0
def make_histogram(values, bins=g_default_bins, max_bins=None, debug_name=''):
    """Convert values into a histogram proto using logic from histogram.cc."""
    if values.size == 0:
        raise ValueError(
            '[make_histogram, {}] The input has no element.'.format(
                debug_name))
    values = values.reshape(-1)
    counts, limits = np.histogram(values, bins=bins)
    num_bins = len(counts)
    if max_bins is not None and num_bins > max_bins:
        subsampling = num_bins // max_bins
        subsampling_remainder = num_bins % subsampling
        if subsampling_remainder != 0:
            counts = np.pad(
                counts,
                pad_width=[[0, subsampling - subsampling_remainder]],
                mode="constant",
                constant_values=0)
        counts = counts.reshape(-1, subsampling).sum(axis=-1)
        new_limits = np.empty((counts.size + 1, ), limits.dtype)
        new_limits[:-1] = limits[:-1:subsampling]
        new_limits[-1] = limits[-1]
        limits = new_limits

    # Find the first and the last bin defining the support of the histogram:
    cum_counts = np.cumsum(np.greater(counts, 0, dtype=np.int32))
    start, end = np.searchsorted(cum_counts, [0, cum_counts[-1] - 1],
                                 side="right")
    start = int(start)
    end = int(end) + 1
    del cum_counts

    # TensorBoard only includes the right bin limits. To still have the leftmost limit
    # included, we include an empty bin left.
    # If start == 0, we need to add an empty one left, otherwise we can just include the bin left to the
    # first nonzero-count bin:
    counts = counts[start - 1:end] if start > 0 else np.concatenate(
        [[0], counts[:end]])
    limits = limits[start:end + 1]

    if counts.size == 0 or limits.size == 0:
        raise ValueError(
            '[make_histogram, {}] The histogram is empty, please file a bug report.'
            .format(debug_name))

    sum_sq = values.dot(values)
    from tensorflow import HistogramProto
    return HistogramProto(min=values.min(),
                          max=values.max(),
                          num=len(values),
                          sum=values.sum(),
                          sum_squares=sum_sq,
                          bucket_limit=limits.tolist(),
                          bucket=counts.tolist())
Beispiel #4
0
def makeHistProto(dist: Counter, bins, keys=None) -> HistogramProto:
    if keys == None:
        keys = sorted(dist.keys())
    hist = HistogramProto()
    hist.min = -0.5
    hist.max = bins - 0.5
    hist.num = sum(dist.values())

    hist.sum = sum(i * dist[key] for i, key in enumerate(keys))
    hist.sum_squares = sum((i * dist[key])**2 for i, key in enumerate(keys))
    for i in range(bins):
        hist.bucket_limit.append(i + 0.5)
    for key in keys:
        hist.bucket.append(dist[key] * (30 / bins))
    for _ in range(bins - len(keys)):
        hist.bucket.append(0)

    return hist
Beispiel #5
0
def log_vector(tag, values):
    """
    log_histogram
    Logs a vector of values.
    """
    values = np.array(values).flatten()

    # Fill fields of histogram proto
    hist = HistogramProto()
    hist.min = 0
    hist.max = len(values) - 1
    hist.num = len(values)
    hist.sum = float(np.sum(np.arange(hist.num)))
    hist.sum_squares = float(np.sum(np.arange(hist.num)**2))

    for idx, c in enumerate(values):
        hist.bucket_limit.append(idx)
        hist.bucket.append(c)

    return Summary(value=[Summary.Value(tag=tag, histo=hist)])