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

Fix the calculation of the histogram buckets and writing to the tensor in summary_db_writer #18627

Merged
merged 2 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions tensorflow/contrib/tensorboard/db/summary_db_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1182,14 +1182,20 @@ class SummaryDbWriter : public SummaryWriterInterface {
// See tensorboard/plugins/histogram/summary.py and data_compat.py
Tensor t{DT_DOUBLE, {k, 3}};
auto data = t.flat<double>();
for (int i = 0; i < k; ++i) {
double left_edge = ((i - 1 >= 0) ? histo.bucket_limit(i - 1)
: std::numeric_limits<double>::min());
double right_edge = ((i + 1 < k) ? histo.bucket_limit(i + 1)
: std::numeric_limits<double>::max());
data(i + 0) = left_edge;
data(i + 1) = right_edge;
data(i + 2) = histo.bucket(i);
for (int i = 0, j = 0; i < k; ++i) {
// TODO(nickfelt): reconcile with TensorBoard's data_compat.py
// From summary.proto
// Parallel arrays encoding the bucket boundaries and the bucket values.
// bucket(i) is the count for the bucket i. The range for
// a bucket is:
// i == 0: -DBL_MAX .. bucket_limit(0)
// i != 0: bucket_limit(i-1) .. bucket_limit(i)
double left_edge = (i == 0) ? std::numeric_limits<double>::min()
: histo.bucket_limit(i - 1);

data(j++) = left_edge;
data(j++) = histo.bucket_limit(i);
data(j++) = histo.bucket(i);
}
int64 tag_id;
PatchPluginName(s->mutable_metadata(), kHistogramPluginName);
Expand Down
50 changes: 50 additions & 0 deletions tensorflow/contrib/tensorboard/db/summary_db_writer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,56 @@ class SummaryDbWriterTest : public ::testing::Test {
SummaryWriterInterface* writer_ = nullptr;
};

TEST_F(SummaryDbWriterTest, WriteHistogram_VerifyTensorValues) {
TF_ASSERT_OK(CreateSummaryDbWriter(db_, "histtest", "test1", "user1", &env_,
&writer_));
int step = 0;
std::unique_ptr<Event> e{new Event};
e->set_step(step);
e->set_wall_time(123);
Summary::Value* s = e->mutable_summary()->add_value();
s->set_tag("normal/myhisto");

double dummy_value = 10.123;
HistogramProto* proto = s->mutable_histo();
proto->Clear();
proto->set_min(dummy_value);
proto->set_max(dummy_value);
proto->set_num(dummy_value);
proto->set_sum(dummy_value);
proto->set_sum_squares(dummy_value);

int size = 3;
double bucket_limits[] = {-30.5, -10.5, -5.5};
double bucket[] = {-10, 10, 20};
for (int i = 0; i < size; i++) {
proto->add_bucket_limit(bucket_limits[i]);
proto->add_bucket(bucket[i]);
}
TF_ASSERT_OK(writer_->WriteEvent(std::move(e)));
TF_ASSERT_OK(writer_->Flush());
writer_->Unref();
writer_ = nullptr;

// TODO(nickfelt): implement QueryTensor() to encapsulate this
// Verify the data
string result = QueryString("SELECT data FROM Tensors");
const double* val = reinterpret_cast<const double*>(result.data());
double histarray[] = {std::numeric_limits<double>::min(),
-30.5,
-10,
-30.5,
-10.5,
10,
-10.5,
-5.5,
20};
int histarray_size = 9;
for (int i = 0; i < histarray_size; i++) {
EXPECT_EQ(histarray[i], val[i]);
}
}

TEST_F(SummaryDbWriterTest, NothingWritten_NoRowsCreated) {
TF_ASSERT_OK(CreateSummaryDbWriter(db_, "mad-science", "train", "jart", &env_,
&writer_));
Expand Down