Skip to content

Commit

Permalink
Merge branch 'inference_format' of https://github.com/HazyResearch/sa…
Browse files Browse the repository at this point in the history
…mpler into revert-to-standalone-only
  • Loading branch information
netj committed Jan 23, 2017
2 parents 3801e6c + 2c3d836 commit d88f52e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
35 changes: 23 additions & 12 deletions src/inference_result.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "inference_result.h"
#include "factor_graph.h"
#include <iostream>
#include <iomanip>
#include <memory>

namespace dd {
Expand Down Expand Up @@ -150,34 +151,44 @@ void InferenceResult::show_marginal_snippet(std::ostream &output) const {
output << " ..." << std::endl;
}

void InferenceResult::show_marginal_histogram(std::ostream &output) const {
void InferenceResult::show_marginal_histogram(std::ostream &output,
const size_t bins) const {
// show a histogram of inference results
output << "INFERENCE CALIBRATION (QUERY BINS):" << std::endl;
std::vector<size_t> abc;
for (int i = 0; i <= 10; ++i) {
abc.push_back(0);
}
std::vector<size_t> abc(bins + 1, 0);

size_t bad = 0;
for (size_t j = 0; j < nvars; ++j) {
const Variable &variable = fg.variables[j];
if (!opts.should_sample_evidence && variable.is_evid) {
continue;
}
for (size_t k = 0; k < variable.internal_cardinality(); ++k) {
int bin = (int)((double)sample_tallies[variable.var_val_base + k] /
agg_nsamples[variable.id] * 10);
if (bin >= 0 && bin <= 10) {
size_t bin = (size_t)((double)sample_tallies[variable.var_val_base + k] /
agg_nsamples[variable.id] * bins);
if (bin <= bins) {
++abc[bin];
} else {
++bad;
}
}
}
abc[9] += abc[10];
for (int i = 0; i < 10; ++i) {
output << "PROB BIN 0." << i << "~0." << (i + 1) << " --> # " << abc[i]
<< std::endl;
abc[bins - 1] += abc[bins];

const std::ios::fmtflags flags(output.flags()); // save ostream settings
output << std::fixed; // specify number of decimals (rather than sig figs)

// save precision and set new one
const size_t prec = output.precision(std::max((int)ceil(log10(bins)), 1));

for (size_t i = 0; i < bins; ++i) {
output << "PROB BIN " << (float)i / bins << "~" << (float)(i + 1) / bins
<< " --> # " << abc[i] << std::endl;
}

// restore ostream settings
output.flags(flags);
output << std::setprecision(prec);
}

void InferenceResult::dump_marginals_in_text(std::ostream &text_output) const {
Expand Down
3 changes: 2 additions & 1 deletion src/inference_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class InferenceResult {
void clear_variabletally();
void aggregate_marginals_from(const InferenceResult &other);
void show_marginal_snippet(std::ostream &output) const;
void show_marginal_histogram(std::ostream &output) const;
void show_marginal_histogram(std::ostream &output,
const size_t bins = 10) const;
void dump_marginals_in_text(std::ostream &text_output) const;

inline void update_weight(size_t wid, double stepsize, double gradient) {
Expand Down

0 comments on commit d88f52e

Please sign in to comment.