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 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); }
/** * 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); } } }
for (int i = 0; i < size(); i++) { if (i < n_train) { train.input_list.add(input_list.get(i));
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; }
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); 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));
int bp_size = size(); for (int i = 0; i < bp_size; i++) index_list.add(i);
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;