def test_check_grad(): DIMX = 12 DIMY = 5 #def check_grad(): n_words = 10 n_chars = 3 np.random.seed(3) word_list = np.random.randint(10, size=DIMX * n_chars * n_words).reshape( n_words, n_chars, DIMX).tolist() label_list = np.random.choice(range(1, DIMY + 1), size=n_chars * n_words).reshape( n_words, n_chars).tolist() x = np.zeros((DIMX * DIMY) + DIMY * DIMY) # x= np.random.uniform(size=(DIMX*DIMY)+(DIMY*DIMY)) model = CRFModel(DIMX, DIMY) model.load_X(x) W1, T1 = model._W, model._T print("W = ", W1.shape) print("T = ", T1.shape) train = np.zeros((n_words, 2), dtype='object') for i in range(n_words): tempX = [] for word in word_list[i]: tempX.append(np.array(word, dtype=float)) train[i][0] = np.array(tempX) train[i][1] = np.array(label_list[i], dtype=int) score = opt.check_grad(log_crf_wrapper, grad_crf_wrapper, x, *[train, DIMX, DIMY, False]) print("Score = ", score) assert score < 1.0e-3 score = opt.check_grad(test_log_crf, test_grad_crf, x, *[train, 1000, DIMX, DIMY, False]) print("Score = ", score) assert score < 1.0e-3
def test_check_grad(): DIMX = 5 DIMY = 3 #def check_grad(): n_words = 5 n_chars = 2 np.random.seed(3) word_list = np.random.randint(10, size=DIMX * n_chars * n_words).reshape( n_words, n_chars, DIMX) label_list = np.random.choice(range(1, DIMY + 1), size=n_chars * n_words).reshape( n_words, n_chars) # x = np.zeros((DIMX*DIMY)+DIMY*DIMY) x = np.random.uniform(size=(DIMX * DIMY) + (DIMY * DIMY)) model = CRFModel(DIMX, DIMY) model.load_X(x) W1, T1 = model._W, model._T print("word = ", word_list.shape) print("W = ", W1.shape) print("T = ", T1.shape) print("label = ", label_list.shape) print("CRF = ", log_crf_wrapper(x, word_list, label_list, DIMX, DIMY)) g = grad_crf_wrapper(x, word_list, label_list, DIMX, DIMY) print("g = {}".format(g)) score = opt.check_grad(log_crf_wrapper, grad_crf_wrapper, x, *[word_list, label_list, DIMX, DIMY]) print("Score = ", score) assert score < 1.0e-4
def log_crf_wrapper(x, train, dimX, dimY, from_file=False): model = CRFModel(dimX, dimY) model.load_X(x, from_file=from_file) crfs = np.apply_along_axis(get_logCRF, 1, train,*[model]) # print(x) # print(crfs.mean()) return np.mean(crfs)
def grad_crf_wrapper(x, train, dimX, dimY, from_file=False): # print(x) model = CRFModel(dimX, dimY) model.load_X(x, from_file=from_file) g = np.apply_along_axis(calculate_gradient_crf, 1, train, *[model]) # print(g.mean(axis=0)) return g.mean(axis=0)
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 test_log_crf(x, train, c, dimX, dimY, from_file=True): logCrf = log_crf_wrapper(x, train, dimX, dimY, from_file=from_file) model = CRFModel(dimX, dimY) # print(x) model.load_X(x, from_file=from_file) 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 # print(f) return f
def grad_crf_wrapper(x, word_list, label_list, dimX, dimY, from_file=False): # print(x) model = CRFModel(dimX, dimY) model.load_X(x, from_file=from_file) # T[:,:] = 0.0 avg = np.zeros(x.shape) for i, word in enumerate(word_list): label = label_list[i] avg += calculate_gradient_crf(word, label, model) g = avg / len(word_list) # g[-DIMY*DIMY:] = 0 return g
def test(): X, W, T = parse_decode('../data/decode_input.txt') print("X: {} \nW: {}\nT: {}".format(X.shape, W.shape, T.shape)) ## Test with sample model = CRFModel(128, 26) model.load_WT(W, T) word_list = X[30:42].reshape(-1, 3, 128) pred_bf = crf_decode_bf(model, word_list) pred_dp = crf_decode(model, word_list) print(pred_bf) print(pred_dp) assert pred_bf == pred_dp generate_result(X, model)
def generate_result(): train_data = read_train("../data/train.txt") train = np.array(train_data) word_list = train[:, 0] label_list = train[:, 1] print("word_list shape :", word_list.shape) print("label_list shape :", label_list.shape) print("word shape:", word_list[3].shape) model = CRFModel(dimX=128, dimY=26) model.load_X("../data/model.txt", from_file=True) print(model._T.shape) print(model._W.shape) meanLogCRF = get_logCRF_all(model, word_list, label_list) print(meanLogCRF) #-29.954718407620692 x = np.loadtxt("../data/model.txt") g = grad_crf_wrapper(x, word_list, label_list, 128, 26, from_file=True) return g
def crf_test(x, test_data): """ Compute the test accuracy on the list of words (word_list); x is the current model (w_y and T, stored as a vector) """ word_list = test_data[:, 0] true_label = test_data[:, 1] # x is a vector. so reshape it into w_y and T model = CRFModel(128, 26) model.load_X(x, from_file=False) # Assume x in the format received from file # Compute the CRF prediction of test data using W and T y_predict = crf_decode(model, word_list) # print(y_predict) # Compute the test accuracy by comparing the prediction with the ground truth letterAcc, wordAcc = compare(y_predict, true_label) print('Letter Accuracy = {}\n'.format(letterAcc)) print('Word Accuracy = {} \n'.format(wordAcc)) return y_predict
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]
def log_crf_wrapper(x, word_list, label_list, dimX, dimY, from_file=False): model = CRFModel(dimX, dimY) model.load_X(x, from_file=from_file) return get_logCRF_all(model, word_list, label_list)