Beispiel #1
0
def run_one(iteration, model, model2data, probs):
    if LOTlib.SIG_INTERRUPTED: # do this so we don't create (big) hypotheses
        return

    # Take model and load the function to create hypotheses
    # Data is passed in to be constant across runs
    if re.search(r":", model):
        m, d = re.split(r":", model)
        make_hypothesis, _ = load_example(m)
    else:
        make_hypothesis, _ = load_example(model)

    htmp = make_hypothesis() # just use this to get the grammar

    # Make a new class to wrap our mixture in
    class WrappedClass(MixtureProposer, type(htmp)):
        pass

    # define a wrapper to set this proposal
    def wrapped_make_hypothesis(**kwargs):
        h = WrappedClass(**kwargs)
        print ">>", htmp, model,  h, kwargs
        h.set_proposal_probabilities(probs)
        return h

    sampler = MultipleChainMCMC(wrapped_make_hypothesis,  model2data[model], steps=options.SAMPLES, nchains=options.CHAINS)

    with open(options.OUT+"/aggregate.%s" % get_rank(), 'a') as out_aggregate:
        evaluate_sampler(sampler, trace=False, prefix="\t".join(map(str, [model, iteration, q(str(probs)) ])),
                         out_aggregate=out_aggregate, print_every=options.PRINTEVERY)
Beispiel #2
0
def run_one(iteration, model, model2data, sampler_type):
    """
    Run one iteration of a sampling method
    """

    if LOTlib.SIG_INTERRUPTED: return

    # Take model and load the function to create hypotheses
    # Data is passed in to be constant across runs
    if re.search(r":", model):
        m, d = re.split(r":", model)
        make_hypothesis, _ = load_example(m)
    else:
        make_hypothesis, _ = load_example(model)


    h0 = make_hypothesis()
    grammar = h0.grammar
    data = model2data[model]

    # Create a sampler
    if sampler_type == 'mh_sample_A':               sampler = MHSampler(h0, data, options.SAMPLES,  likelihood_temperature=1.0)
    # elif sampler_type == 'mh_sample_B':             sampler = MHSampler(h0, data, options.SAMPLES,  likelihood_temperature=1.1)
    # elif sampler_type == 'mh_sample_C':             sampler = MHSampler(h0, data, options.SAMPLES,  likelihood_temperature=1.25)
    # elif sampler_type == 'mh_sample_D':             sampler = MHSampler(h0, data, options.SAMPLES,  likelihood_temperature=2.0 )
    # elif sampler_type == 'mh_sample_E':             sampler = MHSampler(h0, data, options.SAMPLES,  likelihood_temperature=5.0 )
    elif sampler_type == 'particle_swarm_A':        sampler = ParticleSwarm(make_hypothesis, data, steps=options.SAMPLES, within_steps=10)
    elif sampler_type == 'particle_swarm_B':        sampler = ParticleSwarm(make_hypothesis, data, steps=options.SAMPLES, within_steps=100)
    elif sampler_type == 'particle_swarm_C':        sampler = ParticleSwarm(make_hypothesis, data, steps=options.SAMPLES, within_steps=200)
    elif sampler_type == 'particle_swarm_prior_sample_A':        sampler = ParticleSwarmPriorResample(make_hypothesis, data, steps=options.SAMPLES, within_steps=10)
    elif sampler_type == 'particle_swarm_prior_sample_B':        sampler = ParticleSwarmPriorResample(make_hypothesis, data, steps=options.SAMPLES, within_steps=100)
    elif sampler_type == 'particle_swarm_prior_sample_C':        sampler = ParticleSwarmPriorResample(make_hypothesis, data, steps=options.SAMPLES, within_steps=200)
    elif sampler_type == 'multiple_chains_A':       sampler = MultipleChainMCMC(make_hypothesis, data, steps=options.SAMPLES, nchains=10)
    elif sampler_type == 'multiple_chains_B':       sampler = MultipleChainMCMC(make_hypothesis, data, steps=options.SAMPLES, nchains=100)
    elif sampler_type == 'multiple_chains_C':       sampler = MultipleChainMCMC(make_hypothesis, data, steps=options.SAMPLES, nchains=1000)
    elif sampler_type == 'parallel_tempering_A':    sampler = ParallelTemperingSampler(make_hypothesis, data, steps=options.SAMPLES, within_steps=10, temperatures=[1.0, 1.025, 1.05], swaps=1, yield_only_t0=False)
    elif sampler_type == 'parallel_tempering_B':    sampler = ParallelTemperingSampler(make_hypothesis, data, steps=options.SAMPLES, within_steps=10, temperatures=[1.0, 1.25, 1.5], swaps=1, yield_only_t0=False)
    elif sampler_type == 'parallel_tempering_C':    sampler = ParallelTemperingSampler(make_hypothesis, data, steps=options.SAMPLES, within_steps=10, temperatures=[1.0, 2.0, 5.0], swaps=1, yield_only_t0=False)
    elif sampler_type == 'taboo_A':                 sampler = TabooMCMC(h0, data, steps=options.SAMPLES, skip=0, penalty= 0.001)
    elif sampler_type == 'taboo_B':                 sampler = TabooMCMC(h0, data, steps=options.SAMPLES, skip=0, penalty= 0.010)
    elif sampler_type == 'taboo_C':                 sampler = TabooMCMC(h0, data, steps=options.SAMPLES, skip=0, penalty= 0.100)
    elif sampler_type == 'taboo_D':                 sampler = TabooMCMC(h0, data, steps=options.SAMPLES, skip=0, penalty= 1.000)
    elif sampler_type == 'taboo_E':                 sampler = TabooMCMC(h0, data, steps=options.SAMPLES, skip=0, penalty=10.000)
    # elif sampler_type == 'partitionMCMC_A':         sampler = PartitionMCMC(grammar, make_hypothesis, data, 10, steps=options.SAMPLES)
    # elif sampler_type == 'partitionMCMC_B':         sampler = PartitionMCMC(grammar, make_hypothesis, data, 100, steps=options.SAMPLES)
    # elif sampler_type == 'partitionMCMC_C':         sampler = PartitionMCMC(grammar, make_hypothesis, data, 1000, steps=options.SAMPLES)
    elif sampler_type == 'enumeration_A':           sampler = EnumerationInference(grammar, make_hypothesis, data, steps=options.SAMPLES)
    else: assert False, "Bad sampler type: %s" % sampler_type

    # And open our output and evaluate
    with open("output/out-aggregate.%s" % get_rank(), 'a') as out_aggregate:
        evaluate_sampler(sampler, trace=False, prefix="\t".join(map(str, [model, iteration, sampler_type])),
                         out_aggregate=out_aggregate, print_every=options.PRINTEVERY)
Beispiel #3
0
def run_one(iteration, model, model2data, llt):
    if LOTlib.SIG_INTERRUPTED: return

    # Take model and load the function to create hypotheses
    # Data is passed in to be constant across runs
    if re.search(r":", model):
        m, d = re.split(r":", model)
        make_hypothesis, _ = load_example(m)
    else:
        make_hypothesis, _ = load_example(model)

    with open(options.OUT+"/aggregate.%s" % get_rank(), 'a') as out_aggregate:
        evaluate_sampler(MultipleChainMCMC(make_hypothesis, model2data[model], steps=options.SAMPLES, nchains=1, likelihood_temperature=llt),
                         trace=False, prefix="\t".join(map(str, [model, iteration, llt])),
                         out_aggregate=out_aggregate, print_every=options.PRINTEVERY)
Beispiel #4
0
def run_one(iteration, model, model2data, probs):
    if LOTlib.SIG_INTERRUPTED:  # do this so we don't create (big) hypotheses
        return

    # Take model and load the function to create hypotheses
    # Data is passed in to be constant across runs
    if re.search(r":", model):
        m, d = re.split(r":", model)
        make_hypothesis, _ = load_example(m)
    else:
        make_hypothesis, _ = load_example(model)

    htmp = make_hypothesis()  # just use this to get the grammar

    # Make a new class to wrap our mixture in
    class WrappedClass(MixtureProposer, type(htmp)):
        pass

    # define a wrapper to set this proposal
    def wrapped_make_hypothesis(**kwargs):
        h = WrappedClass(**kwargs)
        print ">>", htmp, model, h, kwargs
        h.set_proposal_probabilities(probs)
        return h

    sampler = MultipleChainMCMC(wrapped_make_hypothesis,
                                model2data[model],
                                steps=options.SAMPLES,
                                nchains=options.CHAINS)

    with open(options.OUT + "/aggregate.%s" % get_rank(),
              'a') as out_aggregate:
        evaluate_sampler(
            sampler,
            trace=False,
            prefix="\t".join(map(
                str, [model, iteration, q(str(probs))])),
            out_aggregate=out_aggregate,
            print_every=options.PRINTEVERY)