Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 5
0
    # ========================================================================================================

    from optparse import OptionParser
    parser = OptionParser()

    parser.add_option("--model", dest="MODEL", type="string", default="Number",
                      help="Which model do we run? (e.g. 'Number', 'Magnetism.Simple', etc.")
    parser.add_option("--skip", dest="SKIP", type="int", default=0, help="Skip this many steps between samples")
    parser.add_option("--alsoprint", dest="ALSO_PRINT", type="string", default="None",
                      help="A function of a hypothesis we can also print at the start of a line to see things we "
                           "want. E.g. --alsoprint='lambda h: h.get_knower_pattern()' ")
    (options, args) = parser.parse_args()

    display_option_summary(options)

    # ========================================================================================================
    # Load the model specified on the command line
    # ========================================================================================================

    from LOTlib.Examples.ExampleLoader import load_example

    make_hypothesis, make_data = load_example(options.MODEL)

    # ========================================================================================================
    #  Run the example's standard sampler with these parameters
    # ========================================================================================================

    # This is just a wrapper that nicely prints information
    standard_sample(make_hypothesis, make_data, alsoprint=options.ALSO_PRINT, skip=options.SKIP)

Ejemplo n.º 6
0
                         out_aggregate=out_aggregate, print_every=options.PRINTEVERY)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if __name__ == "__main__":

    # Parse the models from the input
    models = re.split(r',', options.MODELS)

    # define the data for each model.
    # Right now, we do this so that every iteration and method uses THE SAME data in order to minimize the variance
    model2data = dict()
    for model in models:
        if re.search(r":", model): # split model string by : to handle different amounts of data
            m, d = re.split(r":", model)
            _, make_data = load_example(m)
            model2data[model] = make_data(int(d))
        else:
            _, make_data = load_example(model)
            model2data[model] = make_data()


    # For each process, create the list of parameters
    params = [list(g) for g in product(range(options.REPETITONS),\
                                        models,
                                        [model2data],
                                        ['multiple_chains_A', 'multiple_chains_B', 'multiple_chains_C',
                                         'taboo_A', 'taboo_B', 'taboo_C', 'taboo_D',
                                         'particle_swarm_A', 'particle_swarm_B', 'particle_swarm_C',
                                         'particle_swarm_prior_sample_A', 'particle_swarm_prior_sample_B', 'particle_swarm_prior_sample_C',
                                         'mh_sample_A',#, 'mh_sample_B', 'mh_sample_C', 'mh_sample_D', 'mh_sample_E',
Ejemplo n.º 7
0
           self.propose_swaps()

        if self.yield_only_t0 and self.chain_idx != 0:
            return self.next() # keep going until we're on the one we yield
            ## TODO: FIX THIS SINCE IT WILL BREAK FOR HUGE NUMBERS OF CHAINS due toi recursion depth
        else:
            return self.chains[self.chain_idx].next()


if __name__ == "__main__":

    from LOTlib import break_ctrlc
    from LOTlib.Miscellaneous import Infinity
    from LOTlib.Examples.ExampleLoader import load_example

    make_hypothesis, make_data = load_example('Number')

    data = make_data(1000)

    from LOTlib.MCMCSummary.Z import Z
    from LOTlib.MCMCSummary.TopN import TopN
    z = Z(unique=True)
    tn = TopN(N=10)

    sampler = ParallelTemperingSampler(make_hypothesis, data, steps=100000,
                                       whichtemperature='acceptance_temperature',
                                       temperatures=[1.0, 2., 3., 5., 10., 20.])

    for h in break_ctrlc(tn(z(sampler))):
        # print h.posterior_score, h
        pass
Ejemplo n.º 8
0
           self.propose_swaps()

        if self.yield_only_t0 and self.chain_idx != 0:
            return self.next() # keep going until we're on the one we yield
            ## TODO: FIX THIS SINCE IT WILL BREAK FOR HUGE NUMBERS OF CHAINS due toi recursion depth
        else:
            return self.chains[self.chain_idx].next()


if __name__ == "__main__":

    from LOTlib import break_ctrlc
    from LOTlib.Miscellaneous import Infinity
    from LOTlib.Examples.ExampleLoader import load_example

    make_hypothesis, make_data = load_example('Number')

    data = make_data(1000)

    z = Z(unique=True)
    tn = TopN(N=10)

    sampler = ParallelTemperingSampler(make_hypothesis, data, steps=100000,
                                       whichtemperature='acceptance_temperature',
                                       temperatures=[1.0, 2., 3., 5., 10., 20.])

    for h in break_ctrlc(tn(z(sampler))):
        # print h.posterior_score, h
        pass

    for x in tn.get_all(sorted=True):