StructuredProblem train = new StructuredProblem(); StructuredProblem test = new StructuredProblem(); if (weight_list != null) { train.weight_list = new ArrayList<Double>(); for (int i = 0; i < size(); i++) { if (i < n_train) { train.input_list.add(input_list.get(i));
public LabeledMulticlassData(Map<String, Integer> m, Integer n_fea) { label_mapping = m; n_base_feature_in_train = n_fea; sp = new StructuredProblem(); }
.splitData(n_fold, new Random(0));
public static double getPredictedScore(StructuredProblem sp, RerankerBestItemFinder s_finder, WeightVector wv) throws Exception { double pred_loss = 0.0; for (int i = 0; i < sp.size(); i++) { RerankingInstance ri = (RerankingInstance) sp.input_list.get(i); LabeledRerankingIns pred = (LabeledRerankingIns) s_finder .getBestStructure(wv, ri); pred_loss += ri.score_list.get(pred.pred_item); } return pred_loss/sp.size(); } }
public static StructuredProblem readFeatureFile(String fea_file_name) throws IOException { int index = 0; StructuredProblem sp = new StructuredProblem(); ArrayList<String> fea_lines = LineIO.read(fea_file_name);
public static double crossValidation(double C, int n_thread, int n_fold, LabeledMulticlassData train) throws Exception { double cv_res = 0; List<Pair<StructuredProblem, StructuredProblem>> data_pair_list = train.sp .splitData(n_fold, new Random(0)); for (int i = 0; i < n_fold; i++) { StructuredProblem cv_sp_train = data_pair_list.get(i).getFirst(); StructuredProblem cv_sp_test = data_pair_list.get(i).getSecond(); LabeledMulticlassData cv_train = new LabeledMulticlassData( train.label_mapping, train.n_base_feature_in_train, cv_sp_train); LabeledMulticlassData cv_test = new LabeledMulticlassData( train.label_mapping, train.n_base_feature_in_train, cv_sp_test); MulticlassModel cv_model = trainMultiClassModel(C, n_thread, cv_train); Pair<int[], int[]> gold_pred = getPredictionResults(cv_model, cv_test); double test_acc = getTestingAcc(gold_pred.getFirst(), gold_pred.getSecond()); cv_res += test_acc; System.out.println("Fold " + i + ":" + test_acc); } return cv_res / n_fold; }
int bp_size = size(); for (int i = 0; i < bp_size; i++) index_list.add(i); StructuredProblem cv_train = new StructuredProblem(); StructuredProblem cv_test = new StructuredProblem();
public static Pair<int[],int[]> getPredictionResults(MulticlassModel model, LabeledMulticlassData test) throws Exception { int[] gold = new int[test.sp.size()]; int[] pred = new int[test.sp.size()]; for (int i = 0; i < test.sp.size(); i++) { LabeledMultiClassStructure prediction = (LabeledMultiClassStructure) model.s_finder .getBestStructure(model.wv, test.sp.input_list.get(i)); gold[i] = ((LabeledMultiClassStructure) test.sp.output_list .get(i)).output; pred[i] = prediction.output; } return new Pair<int[], int[]>(gold, pred); }
public static Pair<Double,Double> crossValidation(double C, int n_thread, int n_fold, LabeledMulticlassData train, double[][] cost_matrix) throws Exception { double cv_acc = 0; double cv_cost = 0; List<Pair<StructuredProblem, StructuredProblem>> data_pair_list = train.sp .splitData(n_fold, new Random(0)); for (int i = 0; i < n_fold; i++) { StructuredProblem cv_sp_train = data_pair_list.get(i).getFirst(); StructuredProblem cv_sp_test = data_pair_list.get(i).getSecond(); LabeledMulticlassData cv_train = new LabeledMulticlassData( train.label_mapping, train.n_base_feature_in_train, cv_sp_train); LabeledMulticlassData cv_test = new LabeledMulticlassData( train.label_mapping, train.n_base_feature_in_train, cv_sp_test); MulticlassModel cv_model = trainCostSensitiveMultiClassModel(C, n_thread, cv_train,cost_matrix); Pair<int[], int[]> gold_pred = getPredictionResults(cv_model, cv_test); double test_acc = getTestingAcc(gold_pred.getFirst(), gold_pred.getSecond()); cv_acc += test_acc; System.out.println("Fold " + i + " acc :" + test_acc); double test_cost = getTestingCost(gold_pred.getFirst(), gold_pred.getSecond(), cost_matrix); cv_cost += test_cost; System.out.println("Fold " + i + " cost :" + test_cost); } return new Pair<Double, Double>(cv_acc/n_fold, cv_cost/n_fold); }
if (para.TRAINMINI && 5 * para.TRAINMINI_SIZE < sp.size()) { int t_size = para.TRAINMINI_SIZE; System.out.println("Train a mini sp to speed up! size = " + t_size); StructuredProblem minisp = new StructuredProblem(); minisp.input_list = new ArrayList<IInstance>(); minisp.output_list = new ArrayList<IStructure>(); ArrayList<Integer> index_list = new ArrayList<Integer>(); for (int i = 0; i < sp.size(); i++) index_list.add(i); Collections.shuffle(index_list, new Random(0));
/** * A helper function that shuffles the order of the examples in this * problem. * * @param rnd * A random number generator ---- if you use the same random * generator (with the same seed), you will get the same * ordering. */ public void shuffle(Random rnd) { int n_ex = size(); for (int i = 0; i < n_ex; i++) { int j = i + rnd.nextInt(n_ex - i); IInstance tmp_ins = input_list.get(i); input_list.set(i, input_list.get(j)); input_list.set(j, tmp_ins); IStructure tmp_st = output_list.get(i); output_list.set(i, output_list.get(j)); output_list.set(j, tmp_st); if (weight_list != null) { Double tmp_weight = weight_list.get(i); weight_list.set(i, weight_list.get(j)); weight_list.set(j, tmp_weight); } } }
public WeightVector parallelTrainLatentStructuredSVMWithInitStructures_old( final AbstractLatentLossSensitiveStructureFinder[] struct_finder_list, final StructuredProblem sp, final JLISParameters para) throws Exception { WeightVector wv = new WeightVector(para.total_number_features + 1); // +1 for (int i = 0; i < para.MAX_OUT_ITER; i++) { wv = multiThreadGetJointWeightVector(wv, struct_finder_list, sp, empty_b, para); for (int j = 0; j < sp.size(); j++) { IStructure newLatentStructureWithSameOutputStructure = struct_finder_list[0] .getBestLatentStructure(wv, sp.input_list.get(j), sp.output_list.get(j)); sp.output_list .set(j, newLatentStructureWithSameOutputStructure); } } return wv; }
for (int i = 0; i < sp.size(); i++) { RerankingInstance ri = (RerankingInstance) sp.input_list.get(i); LabeledRerankingIns pred = (LabeledRerankingIns) s_finder
protected WeightVector multiThreadGetJointWeightVector(WeightVector old_wv, final AbstractStructureFinder[] struct_finder_list, StructuredProblem sp, BinaryProblem bp, JLISParameters para) throws Exception { int struct_size = sp.size(); int binary_size = bp.size(); int total_size = struct_size + binary_size; System.out.println("Number of traing data: #struct: " + struct_size + " #binary: " + binary_size); WeightVector new_wv = new WeightVector(old_wv, 0); // allocate bias term // for indirect // supervision L2LossInstanceWithAlphas[] alpha_ins_list = initArrayOfInstances(sp, bp, para.c_struct, para.c_binary, struct_size, total_size); return multitreadTrainJLIS(struct_finder_list, para.MAX_OUT_ITER, struct_size, total_size, new_wv, alpha_ins_list, para) .getFirst(); }
BinaryProblem bp, JLISParameters para) throws Exception { L2LossInstanceWithAlphas.setJLISParameters(para); int struct_size = sp.size(); int binary_size = bp.size();
int struct_size = sp.size(); int binary_size = empty_b.size(); int total_size = struct_size + binary_size;