Fleet  0.0.9
Inference in the LOT
FullGrammarHypothesis.h
Go to the documentation of this file.
1 #pragma once
2 
4 
18 template<typename this_t,
19  typename _HYP,
20  typename datum_t=HumanDatum<_HYP>,
21  typename data_t=std::vector<datum_t>,
22  typename _Predict_t=Vector2D<typename _HYP::output_t>>
23 class FullGrammarHypothesis : public BaseGrammarHypothesis<this_t, _HYP, datum_t, data_t, _Predict_t> {
24 public:
25  using HYP = _HYP;
27  using Super::Super;
28  using LL_t = Super::LL_t;
30 
37  virtual void recompute_P(std::vector<HYP>& hypotheses, const data_t& human_data) {
38  assert(which_data == std::addressof(human_data));
39 
40  P.reset(new Predict_t(hypotheses.size(), human_data.size()));
41 
42  #pragma omp parallel for
43  for(size_t h=0;h<hypotheses.size();h++) {
44 
45  // sometimes our predicted data is the same as our previous data
46  // so we are going to include that so we don't keep recomputing it
47  auto ret = hypotheses[h].call(*human_data[0].predict);
48 
49  for(size_t di=0;di<human_data.size();di++) {
50 
51  // only change ret if its different
52  if(di > 0 and (*human_data[di].predict != *human_data[di-1].predict)) {
53  ret = hypotheses[h].call(*human_data[di].predict);
54  }
55 
56  #pragma omp critical
57  P->at(h,di) = ret; // cannot move because ret might be reused
58  }
59  }
60  }
61 
62 
70  virtual std::map<typename HYP::output_t, double> compute_model_predictions(const data_t& human_data, const size_t i, const Matrix& hposterior) const {
71 
72  std::map<typename HYP::output_t, double> model_predictions;
73 
74  // Note this is the non-log version. Here we avoid computing ang logs, logsumexp, or even exp in this loop
75  // because it is very slow.
76  // P(output | learner_data) = sum_h P(h | learner_data) * P(output | h):
77  // NOTE We do not use omp because this is called by something that does
78  for(int h=0;h<hposterior.rows();h++) {
79  if(hposterior(h,i) < 1e-6) continue; // skip very low probability for speed
80 
81  for(const auto& [outcome,outcomelp] : P->at(h,i)) {
82  model_predictions[outcome] += hposterior(h,i) * exp(outcomelp);
83  }
84  }
85 
86  return model_predictions;
87  }
88 
89 };
_Predict_t Predict_t
Definition: BaseGrammarHypothesis.h:58
_HYP HYP
Definition: BaseGrammarHypothesis.h:51
Eigen::MatrixXf Matrix
Definition: EigenLib.h:18
Definition: BaseGrammarHypothesis.h:48
Definition: HumanDatum.h:19
std::shared_ptr< Predict_t > P
Definition: BaseGrammarHypothesis.h:92
Super::Predict_t Predict_t
Definition: FullGrammarHypothesis.h:29
Just a little wrapper to allow vectors to be handled as 2D arrays, which simplifie some stuff in Gram...
Definition: Vector2D.h:14
virtual void recompute_P(std::vector< HYP > &hypotheses, const data_t &human_data)
Recompute the predictions for the hypotheses and data.
Definition: FullGrammarHypothesis.h:37
const data_t * which_data
Definition: BaseGrammarHypothesis.h:99
std::unordered_map< typename datum_t::data_t *, std::vector< Vector > > LL_t
Definition: BaseGrammarHypothesis.h:61
Definition: FullGrammarHypothesis.h:23
virtual std::map< typename HYP::output_t, double > compute_model_predictions(const data_t &human_data, const size_t i, const Matrix &hposterior) const
This uses hposterior (computed via this->compute_normalized_posterior()) to compute the model predict...
Definition: FullGrammarHypothesis.h:70
This class does grammar inference with some collection of HumanData and fixed set of hypotheses...