Пример #1
0
def computeGibbsProbabilities(crf,
                              blocksFunction,
                              choiceFunction,
                              xs,
                              samples=2000):
    """
    Empirically estimate the probabilities of various tag sequences. You
    should count the number of labelings over many samples from the
    Gibbs sampler.
    @param xs list string - Observation sequence
    @param samples int - Number of epochs to produce samples
    @return Counter - A counter of tag sequences with an empirical
                      estimate of their probabilities.
    Example output:
        Counter({
        ('-FEAT-', '-SIZE-', '-SIZE-'): 0.379, 
        ('-SIZE-', '-SIZE-', '-SIZE-'): 0.189, 
        ('-FEAT-', '-SIZE-', '-FEAT-'): 0.166, 
        ('-SIZE-', '-SIZE-', '-FEAT-'): 0.135, 
        ('-FEAT-', '-FEAT-', '-SIZE-'): 0.053, 
        ('-SIZE-', '-FEAT-', '-SIZE-'): 0.052, 
        ('-FEAT-', '-FEAT-', '-FEAT-'): 0.018, 
        ('-SIZE-', '-FEAT-', '-FEAT-'): 0.008})

    Possibly useful:
    * Counter
    * gibbsRun
    """
    # BEGIN_YOUR_CODE (around 2 lines of code expected)
    particles = Counter(
        gibbsRun(crf, blocksFunction, choiceFunction, xs, samples))
    Counters.scale(particles, 1.0 / sum(particles.values()))
    return particles
Пример #2
0
def computeGibbsProbabilities(crf, blocksFunction, choiceFunction, xs, samples = 2000):
    """
    Empirically estimate the probabilities of various tag sequences. You
    should count the number of labelings over many samples from the
    Gibbs sampler.
    @param xs list string - Observation sequence
    @param samples int - Number of epochs to produce samples
    @return Counter - A counter of tag sequences with an empirical
                      estimate of their probabilities.
    Example output:
        Counter({
        ('-FEAT-', '-SIZE-', '-SIZE-'): 0.379, 
        ('-SIZE-', '-SIZE-', '-SIZE-'): 0.189, 
        ('-FEAT-', '-SIZE-', '-FEAT-'): 0.166, 
        ('-SIZE-', '-SIZE-', '-FEAT-'): 0.135, 
        ('-FEAT-', '-FEAT-', '-SIZE-'): 0.053, 
        ('-SIZE-', '-FEAT-', '-SIZE-'): 0.052, 
        ('-FEAT-', '-FEAT-', '-FEAT-'): 0.018, 
        ('-SIZE-', '-FEAT-', '-FEAT-'): 0.008})

    Possibly useful:
    * Counter
    * gibbsRun
    """
    # BEGIN_YOUR_CODE (around 2 lines of code expected)
    particles = Counter(gibbsRun(crf,blocksFunction,choiceFunction,xs,samples))
    Counters.scale(particles, 1.0/sum(particles.values()))
    return particles
Пример #3
0
def part2a_0():
    """Check that you have all the features we expect"""
    xs = exampleInput
    phi = Counter({('-BEGIN-', '-FEAT-'): 1.0, ('-FEAT-', 'Beautiful'): 1.0, ('-FEAT-', 'PREV:-BEGIN-'): 1.0, ('-FEAT-', 'NEXT:2'): 1.0, ('-FEAT-', '-CAPITALIZED-'): 1.0, ('-FEAT-', '-POST-CAPITALIZED-'): 0.0})
    phi_ = submission.nerFeatureFunction(0, '-BEGIN-', '-FEAT-', xs)
    grader.requireIsTrue( Counters.approximateEquals(phi, phi_) )

    phi = Counter({('-FEAT-', '-SIZE-'): 1.0, ('-SIZE-', 'PREV:Beautiful'): 1.0, ('-SIZE-', 'NEXT:bedroom'): 1.0, ('-SIZE-', '-PRE-CAPITALIZED-'): 1.0, ('-SIZE-', '2'): 1.0, ('-SIZE-', '-POST-CAPITALIZED-'): 0.0, ('-SIZE-', '-CAPITALIZED-'): 0.0})
    phi_ = submission.nerFeatureFunction(1, '-FEAT-', '-SIZE-', xs) 
    grader.requireIsTrue( Counters.approximateEquals(phi, phi_) )
    
    phi = Counter({('-SIZE-', '-SIZE-'): 1.0, ('-SIZE-', 'PREV:2'): 1.0, ('-SIZE-', 'bedroom'): 1.0, ('-SIZE-', 'NEXT:-END-'): 1.0, ('-SIZE-', '-CAPITALIZED-'): 0.0, ('-SIZE-', '-PRE-CAPITALIZED-'): 0.0})
    phi_ = submission.nerFeatureFunction(2, '-SIZE-', '-SIZE-', xs)
    grader.requireIsTrue( Counters.approximateEquals(phi, phi_) )
Пример #4
0
def part1b_3():
    """Check that values match our reference"""
    xs = exampleInput
    backward = [
            Counter({'-SIZE-': 0.564, '-FEAT-': 0.435}),
            Counter({'-SIZE-': 0.567, '-FEAT-': 0.432}),
            Counter({'-FEAT-': 0.5, '-SIZE-': 0.5})]
    backward_ = submission.computeBackward(simpleCRF, xs)
    for vec, vec_ in zip( backward, backward_):
        grader.requireIsTrue( Counters.approximateEquals( vec, vec_ ) )
Пример #5
0
 def G(self, t, y_, y, xs):
     r"""
     Computes one of the potentials in the CRF.
     @param t int - index in the observation sequence, 0-based.
     @param y_ string - value of of tag at time t-1 (y_{t-1}),
     @param y string - value of of tag at time t (y_{t}),
     @param xs list string - The full observation seqeunce.
     @return double - G_t(y_{t-1}, y_t ; x, \theta)
     """
     return math.exp( Counters.dot( self.parameters, self.featureFunction(t, y_, y, xs) ) )
Пример #6
0
 def G(self, t, y_, y, xs):
     r"""
     Computes one of the potentials in the CRF.
     @param t int - index in the observation sequence, 0-based.
     @param y_ string - value of of tag at time t-1 (y_{t-1}),
     @param y string - value of of tag at time t (y_{t}),
     @param xs list string - The full observation seqeunce.
     @return double - G_t(y_{t-1}, y_t ; x, \theta)
     """
     return math.exp( Counters.dot( self.parameters, self.featureFunction(t, y_, y, xs) ) )
Пример #7
0
def part1c_1():
    """Check that values match our reference"""
    xs = exampleInput
    T = [ Counter({('-BEGIN-', '-FEAT-'): 0.561, ('-BEGIN-', '-SIZE-'): 0.439}),
          Counter({('-FEAT-', '-SIZE-'): 0.463, ('-SIZE-', '-SIZE-'): 0.343, 
                   ('-SIZE-', '-FEAT-'): 0.096, ('-FEAT-', '-FEAT-'): 0.096}),
          Counter({('-SIZE-', '-SIZE-'): 0.590, ('-SIZE-', '-FEAT-'): 0.217,
                   ('-FEAT-', '-SIZE-'): 0.151, ('-FEAT-', '-FEAT-'): 0.041})
        ]
    T_ = submission.computeEdgeMarginals(simpleCRF, xs)
    for t, t_ in zip(T, T_):
        grader.requireIsTrue( Counters.approximateEquals(t, t_) )
Пример #8
0
def part2a_0():
    """Check that you have all the features we expect"""
    xs = exampleInput
    phi = Counter({
        ('-BEGIN-', '-FEAT-'): 1.0,
        ('-FEAT-', 'Beautiful'): 1.0,
        ('-FEAT-', 'PREV:-BEGIN-'): 1.0,
        ('-FEAT-', 'NEXT:2'): 1.0,
        ('-FEAT-', '-CAPITALIZED-'): 1.0,
        ('-FEAT-', '-POST-CAPITALIZED-'): 0.0
    })
    phi_ = submission.nerFeatureFunction(0, '-BEGIN-', '-FEAT-', xs)
    grader.requireIsTrue(Counters.approximateEquals(phi, phi_))

    phi = Counter({
        ('-FEAT-', '-SIZE-'): 1.0,
        ('-SIZE-', 'PREV:Beautiful'): 1.0,
        ('-SIZE-', 'NEXT:bedroom'): 1.0,
        ('-SIZE-', '-PRE-CAPITALIZED-'): 1.0,
        ('-SIZE-', '2'): 1.0,
        ('-SIZE-', '-POST-CAPITALIZED-'): 0.0,
        ('-SIZE-', '-CAPITALIZED-'): 0.0
    })
    phi_ = submission.nerFeatureFunction(1, '-FEAT-', '-SIZE-', xs)
    grader.requireIsTrue(Counters.approximateEquals(phi, phi_))

    phi = Counter({
        ('-SIZE-', '-SIZE-'): 1.0,
        ('-SIZE-', 'PREV:2'): 1.0,
        ('-SIZE-', 'bedroom'): 1.0,
        ('-SIZE-', 'NEXT:-END-'): 1.0,
        ('-SIZE-', '-CAPITALIZED-'): 0.0,
        ('-SIZE-', '-PRE-CAPITALIZED-'): 0.0
    })
    phi_ = submission.nerFeatureFunction(2, '-SIZE-', '-SIZE-', xs)
    grader.requireIsTrue(Counters.approximateEquals(phi, phi_))
Пример #9
0
def part1b_3():
    """Check that values match our reference"""
    xs = exampleInput
    backward = [
        Counter({
            '-SIZE-': 0.564,
            '-FEAT-': 0.435
        }),
        Counter({
            '-SIZE-': 0.567,
            '-FEAT-': 0.432
        }),
        Counter({
            '-FEAT-': 0.5,
            '-SIZE-': 0.5
        })
    ]
    backward_ = submission.computeBackward(simpleCRF, xs)
    for vec, vec_ in zip(backward, backward_):
        grader.requireIsTrue(Counters.approximateEquals(vec, vec_))
Пример #10
0
def part1c_1():
    """Check that values match our reference"""
    xs = exampleInput
    T = [
        Counter({
            ('-BEGIN-', '-FEAT-'): 0.561,
            ('-BEGIN-', '-SIZE-'): 0.439
        }),
        Counter({
            ('-FEAT-', '-SIZE-'): 0.463,
            ('-SIZE-', '-SIZE-'): 0.343,
            ('-SIZE-', '-FEAT-'): 0.096,
            ('-FEAT-', '-FEAT-'): 0.096
        }),
        Counter({
            ('-SIZE-', '-SIZE-'): 0.590,
            ('-SIZE-', '-FEAT-'): 0.217,
            ('-FEAT-', '-SIZE-'): 0.151,
            ('-FEAT-', '-FEAT-'): 0.041
        })
    ]
    T_ = submission.computeEdgeMarginals(simpleCRF, xs)
    for t, t_ in zip(T, T_):
        grader.requireIsTrue(Counters.approximateEquals(t, t_))