-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
histogram: make summary_v2.histogram_pb
TPU compatible
#5409
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,15 +14,22 @@ | |
# ============================================================================== | ||
"""Histogram summaries and TensorFlow operations to create them, V2 versions. | ||
|
||
A histogram summary stores a list of buckets. Each bucket is encoded as | ||
a triple `[left_edge, right_edge, count]`. Thus, a full histogram is | ||
encoded as a tensor of dimension `[k, 3]`. | ||
|
||
In general, the value of `k` (the number of buckets) will be a constant, | ||
like 30. There are two edge cases: if there is no data, then there are | ||
no buckets (the shape is `[0, 3]`); and if there is data but all points | ||
have the same value, then there is one bucket whose left and right | ||
endpoints are the same (the shape is `[1, 3]`). | ||
A histogram summary stores a list of buckets. Each bucket is encoded as a triple | ||
`[left_edge, right_edge, count]`. Thus, a full histogram is encoded as a tensor | ||
of dimension `[k, 3]`, where the first `k - 1` buckets are closed-open and the | ||
last bucket is closed-closed. | ||
|
||
In general, the value of `k` (the number of buckets) will be a constant, like 30. | ||
For V2 format, there are two edge cases: if there is no data, then there are no | ||
buckets (the shape is `[0, 3]`); and if there is data but all points have the | ||
same value, then there is one bucket whose left and right endpoints are the same | ||
(the shape is `[1, 3]`). | ||
|
||
For V3 format, the shape of the output histogram is always constant (`[k, 3]`). | ||
In the case of empty data, the output will be an all-zero histogram of shape | ||
`[k, 3]`, where all edges and counts are zeros. If there is data but all points | ||
have the same value, then all buckets' left and right edges are the same and only | ||
the last bucket has nonzero count. | ||
""" | ||
|
||
import contextlib | ||
|
@@ -257,11 +264,11 @@ def histogram_pb(tag, data, buckets=None, description=None): | |
tag: String tag for the summary. | ||
data: A `np.array` or array-like form of any shape. Must have type | ||
castable to `float`. | ||
buckets: Optional positive `int`. The output will have this | ||
many buckets, except in two edge cases. If there is no data, then | ||
there are no buckets. If there is data but all points have the | ||
same value, then there is one bucket whose left and right | ||
endpoints are the same. | ||
buckets: Optional positive `int`. The output shape will always be | ||
[buckets, 3]. If there is no data, then an all-zero array of shape | ||
[buckets, 3] will be returned. If there is data but all points have | ||
the same value, then all buckets' left and right endpoints are the | ||
same and only the last bucket has nonzero count. | ||
description: Optional long-form description for this summary, as a | ||
`str`. Markdown is supported. Defaults to empty. | ||
|
||
|
@@ -270,15 +277,18 @@ def histogram_pb(tag, data, buckets=None, description=None): | |
""" | ||
bucket_count = DEFAULT_BUCKET_COUNT if buckets is None else buckets | ||
data = np.array(data).flatten().astype(float) | ||
if data.size == 0: | ||
buckets = np.array([]).reshape((0, 3)) | ||
if bucket_count == 0 or data.size == 0: | ||
histogram_buckets = np.zeros((bucket_count, 3)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional, but this implementation also handles the 0-bucket-count case, so you could combine these two conditions into just if bucket_count == 0 or data.size == 0:
histogram_buckets = np.zeros((bucket_count, 3)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks! |
||
else: | ||
min_ = np.min(data) | ||
max_ = np.max(data) | ||
range_ = max_ - min_ | ||
if range_ == 0: | ||
center = min_ | ||
buckets = np.array([[center - 0.5, center + 0.5, float(data.size)]]) | ||
left_edges = right_edges = np.array([min_] * bucket_count) | ||
bucket_counts = np.array([0] * (bucket_count - 1) + [data.size]) | ||
histogram_buckets = np.array( | ||
[left_edges, right_edges, bucket_counts] | ||
).transpose() | ||
else: | ||
bucket_width = range_ / bucket_count | ||
offsets = data - min_ | ||
|
@@ -295,10 +305,10 @@ def histogram_pb(tag, data, buckets=None, description=None): | |
edges = np.linspace(min_, max_, bucket_count + 1) | ||
left_edges = edges[:-1] | ||
right_edges = edges[1:] | ||
buckets = np.array( | ||
histogram_buckets = np.array( | ||
[left_edges, right_edges, bucket_counts] | ||
).transpose() | ||
tensor = tensor_util.make_tensor_proto(buckets, dtype=np.float64) | ||
tensor = tensor_util.make_tensor_proto(histogram_buckets, dtype=np.float64) | ||
|
||
summary_metadata = metadata.create_summary_metadata( | ||
display_name=None, description=description | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add the other 3 test cases for v3 as well?
test_empty_input, test_empty_input_of_high_rank, test_zero_bucket_count
See also other comment about the empty input case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.