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
def loss(y, ybar, sparm): """Return the loss of ybar relative to the true labeling y. Returns the loss for the correct label y and the predicted label ybar. In the event that y and ybar are identical loss must be 0. Presumably as y and ybar grow more and more dissimilar the returned value will increase from that point. sparm.loss_function holds the loss function option specified on the command line via the -l option. The default behavior is to perform 0/1 loss based on the truth of y==ybar.""" return path.compute_loss(y, ybar, path.L2)
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