Пример #1
0
def expectation(model, instance):
    '''
    Perform forward-backward algorithm to calculate the second component
    of the detrieve.
    '''
    # get the cached score

    L = len(instance)
    T = model.nr_tags
    A = model.nr_attrs
    g0, g = build_score_cache(model.w, L, T, A, instance)

    a = forward(g0, g, L, T)
    b = backward(g, L, T)

    logZ = logsumexp(a[L-1,:])

    E = defaultdict(float)
    f = instance.features_table

    c = exp(g0 + b[0,:] - logZ).clip(0., 1.)
    for j in xrange(T):
        for k in f[0,None,j]:
            E[k] += c[j]

    for i in xrange(1, L):
        c = exp(add.outer(a[i-1,:], b[i,:]) + g[i,:,:] - logZ).clip(0.,1.)
        for j in range(T):
            for k in range(T):
                for e in f[i,j,k]:
                    E[e] += c[j,k]

    return E
Пример #2
0
def expectation(model, instance):
    '''
    Perform forward-backward algorithm to calculate the second component
    of the detrieve.
    '''
    # get the cached score

    L = len(instance)
    T = model.nr_tags
    A = model.nr_attrs
    g0, g = build_score_cache(model.w, L, T, A, instance)

    a = forward(g0, g, L, T)
    b = backward(g, L, T)

    logZ = logsumexp(a[L - 1, :])

    E = defaultdict(float)
    f = instance.features_table

    c = exp(g0 + b[0, :] - logZ).clip(0., 1.)
    for j in xrange(T):
        for k in f[0, None, j]:
            E[k] += c[j]

    for i in xrange(1, L):
        c = exp(add.outer(a[i - 1, :], b[i, :]) + g[i, :, :] - logZ).clip(
            0., 1.)
        for j in range(T):
            for k in range(T):
                for e in f[i, j, k]:
                    E[e] += c[j, k]

    return E
Пример #3
0
def viterbi(model, instance):
    '''
    '''
    L = len(instance)
    T = model.nr_tags
    A = model.nr_attrs

    build_instance(model.attrs, model.tags, instance, False)
    g0, g = build_score_cache(model.w, L, T, A, instance)
    destroy_instance(instance)

    s, p = argmax(g0, g, L, T)

    v, i = s[L - 1].argmax(), L - 1

    ret = []
    while i >= 0:
        ret.append(v)
        v = p[i][v]
        i -= 1

    ret.reverse()
    return ret
Пример #4
0
def viterbi(model, instance):
    '''
    '''
    L = len(instance)
    T = model.nr_tags
    A = model.nr_attrs

    build_instance(model.attrs, model.tags, instance, False)
    g0, g = build_score_cache(model.w, L, T, A, instance)
    destroy_instance(instance)

    s, p = argmax(g0, g, L, T)

    v, i = s[L -1].argmax(), L -1

    ret = []
    while i >= 0:
        ret.append(v)
        v = p[i][v]
        i -= 1

    ret.reverse()
    return ret