Fleet  0.0.9
Inference in the LOT
DeterministicGrammarHypothesis.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 
14 template<typename this_t,
15  typename _HYP,
16  typename datum_t=HumanDatum<_HYP>,
17  typename data_t=std::vector<datum_t>,
18  typename _Predict_t=Vector2D<typename _HYP::output_t>>
19 class DeterministicGrammarHypothesis : public BaseGrammarHypothesis<this_t, _HYP, datum_t, data_t, _Predict_t> {
20 public:
21  using HYP = _HYP;
23  using Super::Super;
24  using LL_t = Super::LL_t;
26 
27  // NOTE: We need to define this constructor or else we call the wrong recompute_LL in the constructor
28  // because C++ is a little bit insane.
29  DeterministicGrammarHypothesis(std::vector<HYP>& hypotheses, const data_t* human_data) {
30  this->set_hypotheses_and_data(hypotheses, *human_data);
31  }
32 
33 
34  virtual void recompute_P(std::vector<HYP>& hypotheses, const data_t& human_data) override {
35  assert(this->which_data == std::addressof(human_data));
36 
37  this->P.reset(new Predict_t(hypotheses.size(), human_data.size()));
38  if(human_data.size() == 0) return ;
39 
40  #pragma omp parallel for
41  for(size_t h=0;h<hypotheses.size();h++) {
42 
43  // sometimes our predicted data is the same as our previous data
44  // so we are going to include that so we don't keep recomputing it
45  auto ret = hypotheses[h].call(*human_data[0].predict);
46 
47  for(size_t di=0;di<human_data.size();di++) {
48 
49  // only change ret if its different
50  if(di > 0 and (*human_data[di].predict != *human_data[di-1].predict)) {
51  ret = hypotheses[h].call(*human_data[di].predict);
52  }
53 
54  #pragma omp critical
55  this->P->set(h,di,ret); // NOTE: Cannot move here because we might use "ret" again
56  }
57  }
58  }
59 
60 
61  virtual std::map<typename HYP::output_t, double> compute_model_predictions(const data_t& human_data, const size_t i, const Matrix& hposterior) const override {
62 
63  std::map<typename HYP::output_t, double> model_predictions;
64 
65  for(int h=0;h<hposterior.rows();h++) {
66  if(hposterior(h,i) < 1e-6) continue; // skip very low probability for speed
67 
68  model_predictions[this->P->get(h,i)] += hposterior(h,i);
69  }
70 
71  return model_predictions;
72  }
73 
74 
75 };
76 
77 
78 
Definition: DeterministicGrammarHypothesis.h:19
virtual void set_hypotheses_and_data(std::vector< HYP > &hypotheses, const data_t &human_data)
This is the primary function for setting hypothese and data on construction.
Definition: BaseGrammarHypothesis.h:172
_Predict_t Predict_t
Definition: BaseGrammarHypothesis.h:58
Eigen::MatrixXf Matrix
Definition: EigenLib.h:18
Definition: BaseGrammarHypothesis.h:48
we don&#39;t need inputs/outputs for out MyHypothesis
Definition: MyHypothesis.h:6
virtual void recompute_P(std::vector< HYP > &hypotheses, const data_t &human_data) override
Recompute the predictions for the hypotheses and data.
Definition: DeterministicGrammarHypothesis.h:34
Definition: HumanDatum.h:19
virtual std::map< typename HYP::output_t, double > compute_model_predictions(const data_t &human_data, const size_t i, const Matrix &hposterior) const override
This uses hposterior (computed via this->compute_normalized_posterior()) to compute the model predict...
Definition: DeterministicGrammarHypothesis.h:61
std::shared_ptr< Predict_t > P
Definition: BaseGrammarHypothesis.h:92
Just a little wrapper to allow vectors to be handled as 2D arrays, which simplifie some stuff in Gram...
Definition: Vector2D.h:14
DeterministicGrammarHypothesis(std::vector< HYP > &hypotheses, const data_t *human_data)
Definition: DeterministicGrammarHypothesis.h:29
const data_t * which_data
Definition: BaseGrammarHypothesis.h:99
std::unordered_map< typename HumanDatum< MyHypothesis > ::std::vector< HumanDatum< MyHypothesis > > *, std::vector< Vector > > LL_t
Definition: BaseGrammarHypothesis.h:61
Super::Predict_t Predict_t
Definition: DeterministicGrammarHypothesis.h:25
This class does grammar inference with some collection of HumanData and fixed set of hypotheses...