Fleet  0.0.9
Inference in the LOT
MyHypothesis.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "CachedCallHypothesis.h"
5 
6 class InnerHypothesis : public DeterministicLOTHypothesis<InnerHypothesis,Utterance,TruthValue,MyGrammar,&grammar>,
7  public CachedCallHypothesis<InnerHypothesis,Utterance,TruthValue> {
8 public:
10  using Super::Super; // inherit the constructors
12 
13  InnerHypothesis(const InnerHypothesis& c) : Super(c), CCH(c) {}
14  InnerHypothesis(const InnerHypothesis&& c) : Super(c), CCH(c) { }
15 
18  CachedCallHypothesis::operator=(c);
19  return *this;
20  }
23  CachedCallHypothesis::operator=(c);
24  return *this;
25  }
26 
27  void set_value(Node& v) {
30  }
31  void set_value(Node&& v) {
34  }
35 
36  virtual double compute_prior() override {
38  }
39 
40  [[nodiscard]] virtual ProposalType propose() const override {
41 
42  ProposalType p;
43 
44  if(flip(0.85)) p = Proposals::regenerate(&grammar, value);
46  else if(flip(0.5)) p = Proposals::swap_args(&grammar, value);
47  else if(flip()) p = Proposals::insert_tree(&grammar, value);
49 
50  return p;
51  }
52 
57  double get_weight_fromcache() const {
58  if(cache.size() == 0) { return 1.0; } // for empty cache
59 
60  int numtrue = 0;
61  for(auto& v : cache) {
62  numtrue += (v == TruthValue::True);
63  }
64 
65  // use the actual weight formula
66  return 1.0 / (0.1 + double(numtrue) / cache.size());
67  }
68 
69 
70  virtual TruthValue cached_call_wrapper(const Utterance& di) override {
71  // override how cached_call accesses the data
72  return this->call(di, TruthValue::Undefined);
73  }
74 
75 
76 };
77 
78 #include "Lexicon.h"
79 
80 struct ignore_t {};
81 class MyHypothesis : public Lexicon<MyHypothesis, std::string, InnerHypothesis, ignore_t,ignore_t, Utterance> {
82  // Takes a node (in a bigger tree) and a word
84  using Super::Super; // inherit the constructors
85 public:
86 
87  void clear_cache() {
88  for(auto& [k,f] : factors) {
89  f.clear_cache();
90  }
91  }
92 
93 
94  virtual double compute_likelihood(const data_t& data, double breakout=-infinity) override {
95 
96  // need to set this; probably not necessary?
97  for(auto& [k,f] : factors)
98  f.program.loader = this;
99 
100  // get the cached version
101  for(auto& [k,f] : factors) {
102  f.cached_call(data);
103 
104  if(f.got_error)
105  return likelihood = -infinity;
106  }
107 
108  // first compute the weights -- these will depend on how how often each is true
109  std::map<key_t,double> weights;
110  double W = 0.0; // total weight
111  for(auto& [k, f] : factors) {
112  weights[k] = f.get_weight_fromcache();
113  W += weights[k];
114  }
115 
116 
117  likelihood = 0.0;
118  for(size_t i=0;i<data.size();i++) {
119  const Utterance& utterance = data[i];
120 
121  // Check all the words that are true and select
122  bool wtrue = false; // was w true?
123  bool wpresup = false; // was w presuppositionally valid?
124  double Wpt = 0.0; // total weight of those that are true and presup
125  double Wp = 0.0; // total weight of those that are true and presup
126 
127  // must loop over each word and see when it is true
128  for(auto& [k,f] : factors) {
129  // we just call the right factor on utterance -- note that the "word" in u is
130  // ignored within the function call
131  auto tv = f.cache.at(i);
132 
133  if(tv != TruthValue::Undefined) {
134  Wp += weights[k]; // presup met
135  if(tv == TruthValue::True) {
136  Wpt += weights[k];
137  }
138  }
139 
140  if(utterance.word == k) {
141  wtrue = (tv == TruthValue::True);
142  wpresup = (tv != TruthValue::Undefined);
143  }
144  }
145 
147 
148  // compute p -- equation (13) in the paper
149  double w = weights.at(utterance.word);
150  double p = (wtrue and wpresup ? alpha_p*alpha_t*w/Wpt : 0) +
151  (wpresup ? alpha_p*(1-alpha_t)*w/Wp : 0) +
152  (1.0-alpha_p)*w/W;
153  likelihood += log(p);
154  }
155 
156  return likelihood;
157  }
158 
159  virtual std::string string(std::string prefix="") const override {
160  extern MyHypothesis target;
161 
162  std::string out = prefix;
163  for(auto& [k,f] : factors) {
164 
165  // TODO: get precision and recall relative to the target for presup and for
166  bool conservative = true;
167  int agr = 0;
168  for(size_t i=0;i<PRDATA;i++) {
169  auto& di = prdata[i];
170 
171  auto o = const_cast<InnerHypothesis*>(&f)->call(di); // we have to const_cast here since call is not const,
172  auto to = target.at(k).call(di);
173  agr += (to == o);
174 
175  // we compute whether we give the same answer on <A,intersection(A,B)> as <A,B>
176  auto conservative_di = di; // copy the dat
177  conservative_di.color = DSL::intersection(conservative_di.shape, conservative_di.color);
178  auto co = const_cast<InnerHypothesis*>(&f)->call(conservative_di);
179  if(co != o) {
180  conservative = false;
181  }
182  }
183 
184  out += str("\t", k,
185  f.get_weight_fromcache(),
186  target.at(k).get_weight_fromcache(),
187  double(agr)/PRDATA,
188  conservative,
189  QQ(f.string())) + "\n";
190  }
191  assert(out.size() > 0); // better not factors here
192  out.erase(out.size()-1); //remove last newline
193 
194  return out;
195  }
196 
197  virtual void show(std::string prefix="") override {
202  extern MyHypothesis target;
203  extern MyHypothesis bunny_spread;
205 
206  print(":", prefix, this->posterior, this->prior, this->likelihood, target.likelihood, bunny_spread.likelihood, classical_spread.likelihood);
207  print(this->string());
208  }
209 };
A lexicon stores an association of numbers (in a vector) to some other kind of hypotheses (typically ...
MyGrammar grammar
std::optional< std::pair< Node, double > > sample_function_leaving_args(GrammarType *grammar, const Node &from)
This samples functions f(a,b) -> g(a,b) (e.g. without destroying what&#39;s below). This uses a little tr...
Definition: Proposers.h:331
std::string QQ(const std::string &x)
Definition: Strings.h:190
double likelihood
Definition: Bayesable.h:43
void set_value(Node &v)
Definition: MyHypothesis.h:27
Definition: Node.h:22
LOTHypothesis< InnerHypothesis, BindingTree *, bool, MyGrammar, grammar, defaultdatum_t< BindingTree *, bool >, std::vector< defaultdatum_t< BindingTree *, bool > >, typename MyGrammar ::VirtualMachineState_t > Super
Definition: DeterministicLOTHypothesis.h:17
virtual double compute_prior() override
Compute the prior – defaultly just the PCFG (grammar) prior.
Definition: MyHypothesis.h:36
TruthValue
Definition: Main.cpp:32
virtual double compute_likelihood(const data_t &data, double breakout=-infinity) override
Compute the likelihood of a collection of data, by calling compute_single_likelihood on each...
Definition: MyHypothesis.h:94
DeterministicLOTHypothesis< InnerHypothesis, BindingTree *, bool, MyGrammar,&grammar > Super
Definition: InnerHypothesis.h:14
MyHypothesis bunny_spread
Definition: Main.cpp:56
Definition: InnerHypothesis.h:9
double prior
Definition: Bayesable.h:42
virtual output_t call(const input_t x, const output_t &err=output_t{})
A variant of call that assumes no stochasticity and therefore outputs only a single value...
Definition: DeterministicLOTHypothesis.h:32
const int PRDATA
Definition: Main.cpp:27
InnerHypothesis & operator=(const InnerHypothesis &c)
Definition: MyHypothesis.h:16
bool flip(float p=0.5)
Definition: Random.h:25
void clear_cache()
Definition: MyHypothesis.h:87
std::string str(BindingTree *t)
Definition: BindingTree.h:195
double posterior
Definition: Bayesable.h:44
virtual ProposalType propose() const override
Default proposal is rational-rules style regeneration.
Definition: MyHypothesis.h:40
Definition: CachedCallHypothesis.h:14
we don&#39;t need inputs/outputs for out MyHypothesis
Definition: MyHypothesis.h:6
virtual void show(std::string prefix="") override
Definition: MyHypothesis.h:197
const auto intersection
Definition: DSL.h:32
void print(FIRST f, ARGS... args)
Lock output_lock and print to std:cout.
Definition: IO.h:53
std::optional< std::pair< Node, double > > delete_tree(GrammarType *grammar, const Node &from)
Definition: Proposers.h:275
void set_value(Node &&v)
Definition: MyHypothesis.h:31
void clear_cache()
Definition: CachedCallHypothesis.h:23
constexpr double infinity
Definition: Numerics.h:20
Definition: Main.cpp:40
Definition: DeterministicLOTHypothesis.h:14
MyHypothesis classical_spread
Definition: Main.cpp:57
MyHypothesis::data_t prdata
Definition: Main.cpp:42
virtual TruthValue cached_call_wrapper(const Utterance &di) override
This is how we access the data before calling – needed to say how this interfaces with the data...
Definition: MyHypothesis.h:70
InnerHypothesis & operator=(const InnerHypothesis &&c)
Definition: MyHypothesis.h:21
Definition: MyHypothesis.h:80
std::optional< std::pair< Node, double > > regenerate(GrammarType *grammar, const Node &from)
A little helper function that resamples everything below when we can. If we can&#39;t, then we&#39;ll recurse.
Definition: Proposers.h:107
MyHypothesis target
Definition: Main.cpp:55
Definition: Lexicon.h:27
double get_weight_fromcache() const
This computes the weight of this factor from its cached values.
Definition: MyHypothesis.h:57
virtual std::string string(std::string prefix="") const override
Definition: MyHypothesis.h:159
InnerHypothesis(const InnerHypothesis &&c)
Definition: MyHypothesis.h:14
This is a hypothesis that allows you to cache an entire call on data. NOTE: This only catches std::ex...
InnerHypothesis(const InnerHypothesis &c)
Definition: MyHypothesis.h:13
std::optional< std::pair< Node, double > > insert_tree(GrammarType *grammar, const Node &from)
Definition: Proposers.h:192
const double MAX_NODES
Definition: InnerHypothesis.h:20
std::string word
Definition: Main.cpp:44
std::vector< Args... > data_t
Definition: Bayesable.h:39
std::optional< std::pair< Node, double > > swap_args(GrammarType *grammar, const Node &from)
This propose swaps around arguments of the same type.
Definition: Proposers.h:389
virtual size_t count() const
How many nodes total are below me?
Definition: BaseNode.h:358
INNER & at(const key_t &k)
Definition: Lexicon.h:60
Definition: LRUCache.h:19