Пример #1
0
def expectation_check(training_sentences,training_labels,w):
    lhs_array = np.zeros(np.size(w))
    rhs_array = np.zeros(np.size(w))
    for i in range(len(training_labels)):
        y = training_labels[i]
        x = training_sentences[i]
        N = len(y)
        x_info = ff.sent_precheck(x)
        for k in range(1,N):
            trueFF = ff.metaff(y[k-1],y[k],x_info,k)
            for j in trueFF:
                lhs_array[j] += 1
        
        g = sr.g(w,x)
        e_g = np.exp(g)
        alpha = sr.alpha_mat(g)
        beta = sr.beta_mat(g)
        z = sr.Z(alpha,beta)
        for k in range(np.shape(g)[0]):
            for m1 in range(8):
                for m2 in range(8):
                    factor = alpha[k,m1]*beta[k+1,m2]*e_g[k,m1,m2]/z
                    #get list of non-zero (and thus =1) f_j for (i,m1,m2)
                    trueFF = ff.metaff(m1,m2,x_info,k)
                    #add the weighting factor to them
                    for j in trueFF:
                        rhs_array[j] += factor
    return lhs_array,rhs_array
Пример #2
0
def LCL_single(x,y,w):
    """
    computes the LCL for a single training example
    """
    x_info = ff.sent_precheck(x)
    
    g = sr.g(w,x)
    alpha = sr.alpha_mat(g)
    beta = sr.beta_mat(g)
    z = sr.Z(alpha,beta)
    N = len(x)
    sum_on_j = 0.0
    
    for i in range(1,N):
        trueFF = ff.metaff(y[i-1],y[i],x_info,i)
        for j in trueFF:
            sum_on_j += w[j]
    LCL = -math.log(z)
    return LCL
Пример #3
0
def compute_gradient(x, y, w, dw):
    """
    This term computes the gradient vector
    """
    dw *= 0.0

    # get info about this training example
    x_info = ff.sent_precheck(x)
    N = len(x)

    # compute some necessary arrays of factors from the subroutines module
    g = sr.g(w, x)
    e_g = np.exp(g)
    alpha = sr.alpha_mat(g)
    beta = sr.beta_mat(g)
    z = sr.Z(alpha, beta)
    # iterate over position in sentence and tag values, getting a list of indices
    # of feature functions to update at for each position and tag pair value.
    for i in range(np.shape(g)[0]):
        for m1 in range(8):
            for m2 in range(8):
                factor = alpha[i, m1] * beta[i + 1, m2] * e_g[i, m1, m2] / z
                # get list of non-zero (and thus =1) f_j for (i,m1,m2)
                # print m1,m2,x_info,i
                trueFF = ff.metaff(m1, m2, x_info, i + 1)
                # add the weighting factor to them
                for j in trueFF:
                    dw[j] -= factor

    # now I go through and use data from y to compute the "true" value of F_J,
    # once more iterating over i, but not m1,m2 (instead getting those values
    # from the supplied y
    for i in range(1, N):
        trueFF = ff.metaff(y[i - 1], y[i], x_info, i)
        for j in trueFF:
            dw[j] += 1
    return dw