def find_best_smoother(x_tr, y_tr, x_dv, y_dv, smoothers): ''' find the smoothing value that gives the best accuracy on the dev data :param x_tr: training instances :param y_tr: training labels :param x_dv: dev instances :param y_dv: dev labels :param smoothers: list of smoothing values :returns: best smoothing value :rtype: float ''' #raise NotImplementedError scores = {} max_score = -1e9 best_smoother = None best_theta_nb = None for smoother in smoothers: theta_nb = estimate_nb(x_tr, y_tr, smoother) y_hat = clf_base.predict_all(x_dv, theta_nb, y_dv) score = evaluation.acc(y_hat, y_dv) if score > max_score: max_score = score best_smoother = smoother best_theta_nb = theta_nb return best_smoother, best_theta_nb """
def logistic_reg_method(): print('Training', args.method, '...') X_tr = preproc.make_numpy(x_tr_pruned,vocab) X_dv = preproc.make_numpy(x_dv_pruned,vocab) Y_tr = np.array([label_set.index(y_i) for y_i in y_tr]) Y_dv = np.array([label_set.index(y_i) for y_i in y_dv]) print('X_tr', X_tr.shape, type(X_tr)) print('Y_tr', Y_tr.shape, type(Y_tr)) X_tr_var = torch.from_numpy(X_tr.astype(np.float32)) X_dv_var = torch.from_numpy(X_dv.astype(np.float32)) Y_tr_var = torch.from_numpy(Y_tr) Y_dv_var = torch.from_numpy(Y_dv) print('X_tr_var', X_tr_var[0]) print('Y_tr_var', Y_tr_var[0]) # build a new model with a fixed seed torch.manual_seed(765) model = logreg.build_linear(X_tr,Y_tr) model.add_module('softmax',torch.nn.LogSoftmax(dim=1)) loss = torch.nn.NLLLoss() #Y_tr_var = torch.from_numpy(Y_tr) #Y_dv_var = torch.from_numpy(Y_dv) #logP = model.forward(X_tr_var) #logreg.nll_loss(logP.data.numpy(), Y_tr) model_trained, losses, accuracies = logreg.train_model(loss,model, X_tr_var, Y_tr_var, X_dv_var=X_dv_var, Y_dv_var = Y_dv_var, num_its=1000, optim_args={'lr':0.009}) #inference: after training model, use the trained model to make prediction on an evaluation set. _, Y_hat_dv = model_trained.forward(X_dv_var).max(dim=1) print('Y_hat_dv', Y_hat_dv.shape) print('Evaluating', args.method, '...') Y_np = Y_hat_dv.data.numpy() Y_hat_dv_list = [] for i in range(Y_np.shape[0]): Y_hat_dv_list.append(label_set[Y_np[i]]) Y_hat_dv_np = np.array(Y_hat_dv_list) print(Y_hat_dv_np) evaluation.write_predictions(Y_hat_dv_np, args.validation_file + '.preds') # np.save('logreg-es-dev.preds.npy', Y_hat_dv.data.numpy()) return evaluation.acc(np.load('logreg-es-dev.preds.npy'),Y_dv_var.data.numpy())
def train_model(loss, model, X_tr_var, Y_tr_var, num_its=200, X_dv_var=None, Y_dv_var=None, status_frequency=10, optim_args={ 'lr': 0.002, 'momentum': 0 }, param_file='best.params'): # initialize optimizer optimizer = optim.SGD(model.parameters(), **optim_args) #optimizer = optim.Adam(model.parameters(), **optim_args) losses = [] accuracies = [] for epoch in range(num_its): # set gradient to zero optimizer.zero_grad() # run model forward to produce loss output = loss.forward(model.forward(X_tr_var), Y_tr_var) # backpropagate and train output.backward() optimizer.step() losses.append(output.item()) # write parameters if this is the best epoch yet if X_dv_var is not None: # run forward on dev data _, Y_hat = model.forward(X_dv_var).max(dim=1) # compute dev accuracy acc = evaluation.acc(Y_hat.data.numpy(), Y_dv_var.data.numpy()) # save if len(accuracies) == 0 or acc > max(accuracies): state = { 'state_dict': model.state_dict(), 'epoch': len(accuracies) + 1, 'accuracy': acc } torch.save(state, param_file) accuracies.append(acc) # print status message if desired if status_frequency > 0 and epoch % status_frequency == 0: print("Epoch " + str(epoch + 1) + ": Dev Accuracy: " + str(acc)) # load parameters of best model checkpoint = torch.load(param_file) model.load_state_dict(checkpoint['state_dict']) return model, losses, accuracies
def perceptron_method(): print('Training', args.method, '...') print(x_tr_pruned) theta_perc,theta_perc_history = perceptron.estimate_perceptron(x_tr_pruned,y_tr,20) #inference: # to write the predictions on the dev and training data y_hat_dv = clf_base.predict_all(x_dv_pruned,theta_perc,labels) print('Evaluating', args.method, '...') evaluation.write_predictions(y_hat_dv, args.validation_file + '.preds') #y_hat_te = clf_base.predict_all(x_te_pruned,theta_perc,labels) #evaluation.write_predictions(y_hat_te,'perc-test.preds') #y_hat = evaluation.read_predictions('perc-dev.preds') return evaluation.acc(y_hat_dv,y_dv)
def naive_bayes_method(): print('Training', args.method, '...') smoother_vals = np.logspace(-1,2,5) #best_smoother, scores = naive_bayes.find_best_smoother( # x_tr_pruned, y_tr, x_dv_pruned, y_dv, smoother_vals) #theta_nb = naive_bayes.estimate_nb(x_tr_pruned, y_tr, best_smoother) best_smoother, theta_nb = naive_bayes.find_best_smoother( x_tr_pruned, y_tr, x_dv_pruned, y_dv, smoother_vals) #inference: y_hat = clf_base.predict_all(x_dv_pruned, theta_nb, labels) # this shows how we write and read predictions for evaluation #evaluation.write_predictions(y_hat,'nb-dev.preds') print('Evaluating', args.method, '...') evaluation.write_predictions(y_hat, args.validation_file + '.preds') #y_hat_dv = evaluation.read_predictions('nb-dev.preds') #return evaluation.acc(y_hat_dv,y_dv) return evaluation.acc(y_hat, y_dv)