Ejemplo n.º 1
0
class Top(SampleStream):
    """
    This stores the top samples and then *only* on exit does it allow them to pass through. (It can't before
    exit since it won't know what the samples are!)
    """
    def __init__(self, N=1000, key='posterior_score', sorted=True):
        """

        :param N: How many samples to store.
        :param key:  The key we sort by
        :param sorted: When we output, do we output sorted? (slightly slower)
        :return:
        """
        self.__dict__.update(locals())
        SampleStream.__init__(self)
        self.top = TopN(N=N, key=key)

    def process(self, x):
        """ Overwrite process so all outputs are NOT sent to children.
        """
        self.top.add(x)
        return None  # Do no pass through

    def __exit__(self, t, value, traceback):

        ## Here, only on exit do I give my data (the tops) to my outputs
        for v in self.top.get_all(sorted=sorted):
            # Cannot just call self.process_and_push since self.process always returns None
            if v is not None:
                for a in self.outputs:
                    a.process_and_push(v)

        return SampleStream.__exit__(self, t, value, traceback)
Ejemplo n.º 2
0
class Top(SampleStream):
    """
    This stores the top samples and then *only* on exit does it allow them to pass through. (It can't before
    exit since it won't know what the samples are!)
    """

    def __init__(self, N=1000, key='posterior_score', sorted=True):
        """

        :param N: How many samples to store.
        :param key:  The key we sort by
        :param sorted: When we output, do we output sorted? (slightly slower)
        :return:
        """
        self.__dict__.update(locals())
        SampleStream.__init__(self)
        self.top = TopN(N=N, key=key)

    def process(self, x):
        """ Overwrite process so all outputs are NOT sent to children.
        """
        self.top.add(x)
        return None # Do no pass through

    def __exit__(self, t, value, traceback):

        ## Here, only on exit do I give my data (the tops) to my outputs
        for v in self.top.get_all(sorted=sorted):
            # Cannot just call self.process_and_push since self.process always returns None
            if v is not None:
                for a in self.outputs:
                    a.process_and_push(v)

        return SampleStream.__exit__(self, t,value,traceback)
def run(data, TOP=100, STEPS=1000):
    #if LOTlib.SIG_INTERRUPTED:
      #  return ""
    #data = [FunctionData(input=(), output={lst: len(lst)})]
    h0 = MyHypothesis()
    tn = TopN(N=TOP)
    # run the sampler
    counter = Counter()
    for h in MHSampler(h0, data, steps=STEPS, acceptance_temperature=1.0, likelihood_temperature=1.0):#, likelihood_temperature=10.0):
        # counter[h] += 1
        tn.add(h)

    z = logsumexp([h.posterior_score for h in tn])
    sort_post_probs = [(h, exp(h.posterior_score - z)) for h in tn.get_all(sorted=True)][::-1]
    return sort_post_probs
Ejemplo n.º 4
0
def myrun(observed_set):

    if LOTlib.SIG_INTERRUPTED:
        return set()

    h0 = NumberGameHypothesis(grammar=grammar)

    data = [FunctionData(input=[], output=observed_set, alpha=ALPHA)]

    tn = TopN(N=options.TOP_COUNT)
    for h in break_ctrlc(MHSampler(h0, data, steps=options.STEPS)):
        tn.add(h)

    print "# Finished %s" % str(observed_set)

    return set(tn.get_all())
Ejemplo n.º 5
0
def myrun(observed_set):

    if LOTlib.SIG_INTERRUPTED:
        return set()

    h0 = NumberGameHypothesis(grammar=grammar)

    data = [FunctionData(input=[], output=observed_set, alpha=ALPHA)]

    tn = TopN(N=options.TOP_COUNT)
    for h in break_ctrlc(MHSampler(h0, data, steps=options.STEPS)):
        tn.add(h)

    print "# Finished %s" % str(observed_set)

    return set(tn.get_all())
Ejemplo n.º 6
0
            print_star("")
            print from_seq, to_seq
            data = [
                FunctionData(alpha=ALPHA,
                             input=[from_seq],
                             output={to_seq: len(to_seq)})
            ]
            h0 = MyHypothesis()
            step = 0
            tn = TopN(N=N_H)
            # Stream from the sampler to a printer
            for h in MHSampler(h0,
                               data,
                               steps=STEPS,
                               acceptance_temperature=5.):
                tn.add(h)

            print

            for h in tn.get_all(sorted=True):
                out = h(from_seq)
                if len(out) >= len(to_seq):
                    hd = hamming_distance(out[:len(to_seq)], to_seq)
                else:
                    hd = 15

                print h.posterior_score, h.likelihood, h.prior, h, hd
                print out[:len(to_seq)]

            print_star("")
Ejemplo n.º 7
0
from LOTlib.Hypotheses.LOTHypothesis import LOTHypothesis
from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler
from LOTlib.Projects.Quantifier.Model import *

ALPHA = 0.9
SAMPLES = 100000
DATA_SIZE = 1000

if __name__ == "__main__":

    ## sample the target data
    data = generate_data(DATA_SIZE)

    W = 'every'

    # Now to use it as a LOTHypothesis, we need data to have an "output" field which is true/false for whether its the target word. This is then used by LOTHypothesis.compute_likelihood to see if we match or not with whether a word was said (ignoring the other words -- that's why its a pseudolikelihood)
    for di in data:
        di.output = (di.utterance == W)
        #print (di.word == W)

    FBS = TopN(N=100)

    H = LOTHypothesis(grammar, display='lambda A,B,S: %s', ALPHA=ALPHA)
    # Now just run the sampler with a LOTHypothesis
    for s in MHSampler(H, data, SAMPLES, skip=10):
        #print s.lp, "\t", s.prior, "\t", s.likelihood, "\n", s, "\n\n"
        FBS.push(s, s.lp)

    for k in reversed(FBS.get_all(sorted=True)):
        print k.lp, k.prior, k.likelihood, k
Ejemplo n.º 8
0
########################################
## Actually run
########################################
from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler
from LOTlib.SampleStream import *
## First let's make a bunch of hypotheses
from LOTlib.TopN import TopN

tn = TopN(1000)

h0 = MyHypothesisX()
for h in MHSampler(h0, data, steps=100000):  # run more steps
    tn.add(h)

# store these in a list (tn.get_all is defaultly a generator)
hypotheses = list(tn.get_all())

# Compute the normalizing constant
from LOTlib.Miscellaneous import logsumexp
z = logsumexp([h.posterior_score for h in hypotheses])

## Now compute a matrix of how likely each input is to go
## to each output
M = 20  # an MxM matrix of values
import numpy

# The probability of generalizing
G = numpy.zeros((M, M))

# Now add in each hypothesis' predictive
for h in hypotheses:
Ejemplo n.º 9
0
        return ret


if __name__ == "__main__":

    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number2015.Model import generate_data, make_h0
    data = generate_data(300)

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

    from LOTlib.Miscellaneous import logrange

    sampler = AdaptiveParallelTemperingSampler(make_h0, data, steps=1000000, \
                                               yield_only_t0=False, whichtemperature='acceptance_temperature', \
                                               temperatures=logrange(1.0, 10.0, 10))

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

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

    print z

    print sampler.nup, sampler.ndown
    print sampler.get_hist()
Ejemplo n.º 10
0
# for step, h in enumerate(MHSampler(h0, make_data(data_size=2), steps=stepNum)):
#     if step % 5000 == 0:
#         print ('current step: %d, current posterior score: %f' % (step, h.posterior_score))
#     posProbs.append(h.posterior_score)
#     topChoice.add(h)
#     h0 = h

# for step, h in enumerate(MHSampler(h0, make_data(data_size=3), steps=stepNum)):
#     if step % 5000 == 0:
#         print ('current step: %d, current posterior score: %f' % (step, h.posterior_score))
#     posProbs.append(h.posterior_score)
#     topChoice.add(h)
#     h0 = h

for h in topChoice.get_all(sorted=True):
    print h.posterior_score, h

wsHistoryListTuple = map(tuple, wsHistoryList)
historyC = Counter(wsHistoryListTuple)

print "\n\n\n\n\n----<history of world state>------\b"
print historyC.most_common(100)
print "\b----<history of world state>------\n\n\n\n\n"

# Plotting
import numpy as np
import matplotlib

matplotlib.use('agg')
import matplotlib.pyplot as plt