Beispiel #1
0
def compare_charts():
    for I in range(e.N):
        for K in range(I + 1, e.N + 1):
            for X in range(gr.nsymbols):
                print[I, K, X], pp.chart[tri(I, K), X], p.chart[I, K,
                                                                X]['score']
                assert_equal(pp.chart[tri(I, K), X], p.chart[I, K, X]['score'])
Beispiel #2
0
def run():
    for I, K in e.nodes:
        print colors.green % '%s span (%s,%s): "%s"' % ('prune' if m[
            I, K] else 'unprune', I, K, ' '.join(sentence.split()[I:K]))
        cp = changeprop(I, K)
        bf = bruteforce(I, K)
        dp = dynprogram(I, K)
        bs = bsurrogate(I, K)
        if 0:
            print colors.yellow % 'New derivation:'
            print 'CP tree:', cp.coarse
            print 'BF tree:', bf.coarse
        print 'BF: %g' % bf.reward
        print 'CP: %g' % cp.reward
        print 'DP: %g' % dp.reward
        print 'BS: %g' % bs.reward
        assert_equal(bs.reward, dp.reward, 'bs v. dp', verbose=1)
        assert_equal(bf.reward, cp.reward, 'bf v. cp', verbose=1)
Beispiel #3
0
def _test_correctness(example, grammar, aggressive):
    "Test correctness under on-policy roll-in and roll-outs."

    pi = random_mask(example, aggressive)

    m = pi.copy()

    # first parser is assumed to be the "gold standard"
    parsers = [
        #BruteForceAgendaParser(viterbi.DynamicParser, 'brute', grammar),
        BruteParser(leftchild.pruned_parser, 'brute', grammar),
        CPParser(viterbi.DynamicParser, 'changeprop', grammar),
    ]

    Q = {p.name: {} for p in parsers}

    for p in parsers:
        p.initial_rollout(example, m)
        for x in example.nodes:
            [I, K] = x
            Q[p.name][x] = p.change(I, K, 1 - pi[I, K])

    had_tie = False
    for x in Q['brute']:
        ref = Q['brute'][x]
        got = Q['changeprop'][x]

        r = grammar.coarse_derivation(ref.derivation)
        g = grammar.coarse_derivation(got.derivation)

        # [2016-02-05 Fri] OK, well somehow changeprop is finding parses
        # with multiple unary rewrites.
        if r != g:
            # Derivations weren't equal. Assert they're equally likely (tied).
            assert_equal(ref.likelihood,
                         grammar.llh(list2tree(ref.derivation)),
                         'ref-llh-check')
            assert_equal(got.likelihood,
                         grammar.llh(list2tree(got.derivation)),
                         'got-llh-check')
            # we must have a tie because derivations are different and scores are tied.
            had_tie = True

        # At this point we've established that the derivations are the same, may
        # as well check that likelihood is the same.  assert abs(ref[0] -
        # got[0]) < 1e-10, ['llh', ref[0], got[0]]
        assert_equal(ref.likelihood, got.likelihood, 'llh')

        # Note: We allow a little bit of sloppiness in runtime measurement.
        assert_equal(ref.pops, got.pops, 'pops', throw=0, tol=5)

    if had_tie:
        print colors.cyan % 'tie'
    print colors.green % 'ok'
Beispiel #4
0
def _test_correctness_boolean(example, grammar, aggressive):
    "Test correctness under on-policy roll-in and roll-outs."

    pi = random_mask(example, aggressive)

    m = pi.copy()

    # first parser is assumed to be the "gold standard"
    parsers = [
        BruteParser(leftchild.pruned_parser, 'brute', grammar),
        CPParser(boolean.DynamicParser, 'cp-bool', grammar),
    ]

    Q = {p.name: {} for p in parsers}

    for p in parsers:
        p.initial_rollout(example, m)
        for x in example.nodes:
            [I, K] = x
            Q[p.name][x] = p.change(I, K, 1 - pi[I, K])

    for x in Q['brute']:
        ref = Q['brute'][x]
        got = Q['cp-bool'][x]

        #r = grammar.coarse_derivation(ref.derivation)
        #g = grammar.coarse_derivation(got.derivation)

        # At this point we've established that the derivations are the same, may
        # as well check that likelihood is the same.  assert abs(ref[0] -
        # got[0]) < 1e-10, ['llh', ref[0], got[0]]
        assert (ref.likelihood > semizero) == (got.likelihood > semizero)

        # Note: We allow a little bit of sloppiness in runtime measurement.
        assert_equal(ref.pops, got.pops, 'pops', throw=0, tol=5, verbose=1)

    print colors.green % 'ok'
Beispiel #5
0
e = Example(sentence, gr, None)
m = e.mask
m[:, :] = 1

print yellow % 'Sentence:'
print e.sentence

pp = parse(e, gr, m.copy())

p = viterbi.DynamicParser(e.tokens, gr, m.copy())
p.run()

print yellow % 'Derivation:'
print gr.coarse_derivation(p.derivation())

assert_equal(pp.pushes, p.state().pushes, 'pushes', verbose=1, throw=0)
assert_equal(pp.pops, p.state().pops, 'pops', throw=0)

a = 0
print
print yellow % 'span(%s,%s) = %s: %s' % (I, K, a, ' '.join(
    sentence.split()[I:K]))
p.change(I, K, a)
m[I, K] = a

pp = parse(e, gr, m)
assert_equal(pp.pushes, p.state().pushes, 'pushes', verbose=1, throw=0)
assert_equal(pp.pops, p.state().pops, 'pops', throw=0)

a = 1
print