def test_grad_crf(x, train, c, dimX, dimY, from_file=True): model = CRFModel(dimX, dimY) model.load_X(x, from_file=from_file) W = model._W # column format T = model._T # column format reg = np.concatenate([W.T.reshape(-1), T.T.reshape(-1)]) g = grad_crf_wrapper(x, train, dimX, dimY, from_file=from_file) g = -c * g + reg return g
def crf_obj(x, train_data, c): """Compute the CRF objective and gradient on the list of words (word_list) evaluated at the current model x (w_y and T, stored as a vector) """ print("Evaluating grad") global iteration iteration += 1 print(iteration) # x is a vector as required by the solver. logCrf = log_crf_wrapper(x, train_data, 128, 26, from_file=False) model = CRFModel(128, 26) model.load_X(x, from_file=False) W = model._W # column format T = model._T # column format # Compute the objective value of CRF f = (-c * logCrf) + (0.5 * np.sum(W * W)) + ( 0.5 * np.sum(T * T)) # objective log-likelihood + regularizer reg = np.concatenate([W.T.reshape(-1), T.T.reshape(-1)]) g = grad_crf_wrapper(x, train_data, 128, 26, from_file=False) g = -c * g + reg return [f, g]