from LOTlib import break_ctrlc from LOTlib.Inference.GrammarInference.FullGrammarHypothesis import FullGrammarHypothesis from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler h0 = FullGrammarHypothesis(counts, L, GroupLength, prior_offset, NYes, NTrials, Output) mhs = MHSampler(h0, [], 100000, skip=0) for s, h in break_ctrlc(enumerate(mhs)): print mhs.acceptance_ratio(), h.prior, h.likelihood,\ h.value['alpha'].value[0], h.value['beta'].value[0],\ h.value['prior_temperature'].value, h.value['likelihood_temperature'].value,\ 'RULES',\ ' '.join([str(x) for x in h.value['rulep']['BOOL'].value ]),\ ' '.join([str(x) for x in h.value['rulep']['PREDICATE'].value ]),\ ' '.join([str(x) for x in h.value['rulep']['START'].value ]),\ ' '.join([str(x) for x in h.value['rulep']['SET'].value ]) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Run gradient ascent # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # from LOTlib import break_ctrlc # # from LOTlib.Inference.GrammarInference.FullGrammarHypothesis import FullGrammarHypothesis # # # from LOTlib.Inference.GrammarInference.GradientGrammarInference import GrammarGradient # # h0 = FullGrammarHypothesis(counts, L, GroupLength, prior_offset, NYes, NTrials, Output)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ from LOTlib import break_ctrlc from LOTlib.Inference.GrammarInference.FullGrammarHypothesis import FullGrammarHypothesis from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler h0 = FullGrammarHypothesis(counts, L, GroupLength, prior_offset, NYes, NTrials, Output) mhs = MHSampler(h0, [], 100000, skip=0) for s, h in break_ctrlc(enumerate(mhs)): print mhs.acceptance_ratio(), h.prior, h.likelihood,\ h.value['alpha'].value[0], h.value['beta'].value[0],\ h.value['prior_temperature'].value, h.value['likelihood_temperature'].value,\ 'RULES',\ ' '.join([str(x) for x in h.value['rulep']['BOOL'].value ]),\ ' '.join([str(x) for x in h.value['rulep']['PREDICATE'].value ]),\ ' '.join([str(x) for x in h.value['rulep']['START'].value ]),\ ' '.join([str(x) for x in h.value['rulep']['SET'].value ]) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Run gradient ascent # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # from LOTlib import break_ctrlc # # from LOTlib.Inference.GrammarInference.FullGrammarHypothesis import FullGrammarHypothesis # # # from LOTlib.Inference.GrammarInference.GradientGrammarInference import GrammarGradient #
print "# Computed counts for each hypothesis & nonterminal" from LOTlib.Inference.GrammarInference.SimpleGrammarHypothesis import SimpleGrammarHypothesis from LOTlib.Inference.GrammarInference.FullGrammarHypothesis import FullGrammarHypothesis from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler h0 = SimpleGrammarHypothesis(counts, L, GroupLength, prior_offset, NYes, NTrials, Output) # h0 = FullGrammarHypothesis(counts, L, GroupLength, prior_offset, NYes, NTrials, Output) writ = [] mhs = MHSampler(h0, [], 100, skip=500) for s, h in break_ctrlc(enumerate(mhs)): if isinstance(h, SimpleGrammarHypothesis): a = str(mhs.acceptance_ratio()) + ',' + str(h.prior) + ',' + str(h.likelihood) + ',BOOLS,' +\ ','.join([str(x) for x in h.value['BOOL'].value ]) + ',PREDS,' + ','.join([str(x) for x in h.value['PREDICATE'].value ]) else: assert isinstance(h, FullGrammarHypothesis) a = str(mhs.acceptance_ratio()) + ',' + str(h.prior) + ',' + str(h.likelihood) + ',' + \ str(h.value['alpha'].value[0]) + ',' + str(h.value['beta'].value[0]) + ',' + \ str(h.value['prior_temperature']) + ',' + str(h.value['likelihood_temperature']) + ',RULES,' +\ ','.join([str(x) for x in h.value['rulep']['PREDICATE'].value ]) print a writ.append(a) with open('Viz/' +MODEL + '.csv', 'w') as f: f.writelines('\n'.join(writ))
print "# Computed counts for each hypothesis & nonterminal" from LOTlib.Inference.GrammarInference.SimpleGrammarHypothesis import SimpleGrammarHypothesis from LOTlib.Inference.GrammarInference.FullGrammarHypothesis import FullGrammarHypothesis from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler h0 = SimpleGrammarHypothesis(counts, L, GroupLength, prior_offset, NYes, NTrials, Output) # h0 = FullGrammarHypothesis(counts, L, GroupLength, prior_offset, NYes, NTrials, Output) writ = [] mhs = MHSampler(h0, [], 100, skip=500) for s, h in break_ctrlc(enumerate(mhs)): if isinstance(h, SimpleGrammarHypothesis): a = str(mhs.acceptance_ratio()) + ',' + str(h.prior) + ',' + str(h.likelihood) + ',BOOLS,' +\ ','.join([str(x) for x in h.value['BOOL'].value ]) + ',PREDS,' + ','.join([str(x) for x in h.value['PREDICATE'].value ]) else: assert isinstance(h, FullGrammarHypothesis) a = str(mhs.acceptance_ratio()) + ',' + str(h.prior) + ',' + str(h.likelihood) + ',' + \ str(h.value['alpha'].value[0]) + ',' + str(h.value['beta'].value[0]) + ',' + \ str(h.value['prior_temperature']) + ',' + str(h.value['likelihood_temperature']) + ',RULES,' +\ ','.join([str(x) for x in h.value['rulep']['PREDICATE'].value ]) print a writ.append(a) with open('Viz/' + MODEL + '.csv', 'w') as f: f.writelines('\n'.join(writ))