def find_most_violated_constraint_margin(F, y, model, sparm):
    """Return ybar associated with x's most violated constraint.

    The find most violated constraint function for margin rescaling.
    The default behavior is that this returns the value from the
    general find_most_violated_constraint function."""

    if len(y) != 2:
        raise Exception('y should be a pair (states,orients)')

    data_weights, T = diagonal.unpack_weights(list(model.w))
    states, orients = y
    A = path.compute_loss_augmented_terms(F, data_weights, states, path.L2)
    ybar = diagonal.solve(A, T)

    if len(ybar) != 2:
        raise Exception('ybar should be a pair (states,orients)')

    print '\nFinding most violated constraint'
    print '  w: ', list(model.w)
    print '  data w: ', data_weights
    print '  transition:\n', T
    print '  true y: ', y
    print '  classified ybar: ', ybar
    print '  feature(true y): ', path.compute_path_features(F, y)
    print '  feature(ybar): ', path.compute_path_features(F, ybar)
    print '  loss: ', path.compute_loss(y[0], ybar[0], path.L2)

    return ybar
def find_most_violated_constraint_margin(F, y, model, sparm):
    """Return ybar associated with x's most violated constraint.

    The find most violated constraint function for margin rescaling.
    The default behavior is that this returns the value from the
    general find_most_violated_constraint function."""

    if len(y) != 2:
        raise Exception('y should be a pair (states,orients)')

    data_weights,T = diagonal.unpack_weights(list(model.w))
    states,orients = y
    A = path.compute_loss_augmented_terms(F, data_weights, states, path.L2)
    ybar = diagonal.solve(A,T)

    if len(ybar) != 2:
        raise Exception('ybar should be a pair (states,orients)')

    print '\nFinding most violated constraint'
    print '  w: ',list(model.w)
    print '  data w: ',data_weights
    print '  transition:\n',T
    print '  true y: ',y
    print '  classified ybar: ',ybar
    print '  feature(true y): ',path.compute_path_features(F,y)
    print '  feature(ybar): ',path.compute_path_features(F,ybar)
    print '  loss: ',path.compute_loss(y[0], ybar[0], path.L2)

    return ybar
示例#3
0
def find_most_violated_constraint_margin(x, y, model, sparm):
    """Return ybar associated with x's most violated constraint.

    The find most violated constraint function for margin rescaling.
    The default behavior is that this returns the value from the
    general find_most_violated_constraint function."""

    w = list(model.w)

    print '\nFinding most violated constraint'
    print '  w: ',w
    print '  y: ',y

    A = path.compute_loss_augmented_terms(x, w, y, path.L2)
    ybar = viterbi.solve(A)

    D = path.compute_data_terms(x, w)
    print '  ybar: ',ybar
    print '  loss: ',path.compute_loss(y, ybar, path.L2)
    #print 'Data terms:\n', np.round(D, 2)
    #print 'Loss augmented terms:\n', np.round(A, 2)

    return ybar