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
评论列表
文章目录