Skip to content
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

hparams: account for proto nondeterminism in tests #2235

Merged
merged 2 commits into from
May 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions tensorboard/plugins/hparams/summary_v2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ def test_pb_is_tensorboard_copy_of_proto(self):
if tf is not None:
self.assertNotIsInstance(result, tf.compat.v1.Summary)

def assert_hparams_summaries_equal(self, summary_1, summary_2):
def canonical(summary):
"""Return a canonical form for `summary`.

The result is such that `canonical(a) == canonical(b)` if and only
if `a` and `b` are logically equivalent.

Args:
summary: A `summary_pb2.Summary` containing hparams plugin data.
"""
new_summary = summary_pb2.Summary()
new_summary.MergeFrom(summary)
values = new_summary.value
self.assertEqual(len(values), 1, values)
value = values[0]
raw_content = value.metadata.plugin_data.content
value.metadata.plugin_data.content = b"<snipped>"
content = plugin_data_pb2.HParamsPluginData.FromString(raw_content)
return (new_summary, content)

self.assertEqual(canonical(summary_1), canonical(summary_2))

def test_consistency_across_string_key_and_object_key(self):
hparams_1 = {
hp.HParam("optimizer", hp.Discrete(["adam", "sgd"])): "adam",
Expand All @@ -158,7 +180,7 @@ def test_consistency_across_string_key_and_object_key(self):
"optimizer": "adam",
hp.HParam("learning_rate", hp.RealInterval(1e-2, 1e-1)): 0.02,
}
self.assertEqual(
self.assert_hparams_summaries_equal(
hp.hparams_pb(hparams_1, start_time_secs=self.start_time_secs),
hp.hparams_pb(hparams_2, start_time_secs=self.start_time_secs),
)
Expand Down Expand Up @@ -191,7 +213,7 @@ def test_invariant_under_permutation(self):
"learning_rate": 0.02,
"optimizer": "adam",
}
self.assertEqual(
self.assert_hparams_summaries_equal(
hp.hparams_pb(hparams_1, start_time_secs=self.start_time_secs),
hp.hparams_pb(hparams_2, start_time_secs=self.start_time_secs),
)
Expand Down