@Override public IStructure getBestStructure(WeightVector weight, IInstance ins) throws Exception { RerankingInstance ri = (RerankingInstance) ins; int max_index = -1; double max_score = Double.NEGATIVE_INFINITY; for(int i=0; i < ri.fea_list.size(); i ++){ double score = weight.dotProduct(ri.fea_list.get(i)); if (score > max_score){ max_score = score; max_index = i; } } assert max_index != -1; return new LabeledRerankingIns(ri, max_index); }
public static WeightVector getWeightVectorBySumAlpahFv( L2LossInstanceWithAlphas[] alpha_ins_list, boolean is_extendable, int n_ex, int verbose_level) { int max_n = -1; for (int i = 0; i < n_ex; i++) { int cur_idx = alpha_ins_list[i].getMaxIdx(); if (cur_idx > max_n) max_n = cur_idx; } if (verbose_level >= JLISParameters.VLEVEL_MID) System.out.println("number of features: " + max_n); WeightVector cur_wv = new WeightVector(max_n + 1); cur_wv.setExtendable(is_extendable); // double[] cur_w = new double[max_n + 1]; for (int i = 0; i < n_ex; i++) { alpha_ins_list[i].fillWeightVector(cur_wv); } return cur_wv; }
@Override public void solveSubProblemAndUpdateW(L2SolverInfo si, WeightVector w) { double C = sC; double dot_product = w.dotProduct(fv); double xij_norm2 = fv.l2NormSqure(); double NG = 1.0 - y * dot_product - (alpha / (2.0 * C)); double PG = -NG; if (alpha == 0f) { PG = Math.min(-NG, 0); } si.PGmax_new = Math.max(si.PGmax_new, PG); si.PGmin_new = Math.min(si.PGmin_new, PG); if (Math.abs(PG) > UPDATE_CONDITION) { double step = NG / (xij_norm2 + (1.0 / (2.0 * C))); double new_alpha = Math.max(alpha + step, 0);// make sure // alpha_[i][j] // is w.addSparseFeatureVector(fv, (new_alpha - alpha) * y); alpha = new_alpha; } }
bestFeatures.slowAddFeature(INDIRECT_GLOBAL_BIAS, 1.0); double score = 1.0 - y * wv.dotProduct(bestFeatures) - xi; double s = 1.0 - y * wv.dotProduct(tmp_alpha_loss.getSecond()) - xi; System.out.println("bias term:" + wv.getGlobalBiasTerm());
double obj = 0; obj += cur_wv.getTwoNormSquare() * 0.5; double dot_product = cur_wv.dotProduct(fv); sins.gold_struct); double loss = res.getSecond() + cur_wv.dotProduct(res.getFirst().getFeatureVector()) - cur_wv.dotProduct(sins.gold_struct.getFeatureVector());
L2LossInstanceWithAlphas.setJLISParameters(para); WeightVector wv = new WeightVector(para.total_number_features + 1); // +1 WeightVector new_wv = new WeightVector(wv, 0); new_wv.setExtendable(wv.isExtendable());
@CommandDescription(description = "<DESCRIPTION>\n" + "\tThis procedure is used to extract the weight vector from a saved model \n" + "<INPUT>\n" + "\tIt receives 2 different arguments. \n" + "\t1) model_file (a string), the file name of a trained model \n" + "\t2) output_file (a string), the file name that will be used to put the contain of the weight vector. \n" + "<OUTPUT>\n" + "\tThe weight vector will be output in the ${output_file}.") public static void outputWeightVector(String model_name, String output_file) throws IOException, ClassNotFoundException { JLISModelIOManager io = new JLISModelIOManager(); RerankerModel model = (RerankerModel) io.loadModel(model_name); ArrayList<String> out = new ArrayList<String>(); double[] w = model.wv.getInternalArray(); for (int i = 0; i < w.length; i++) out.add(i + ":" + w[i]); LineIO.write(output_file, out); System.out.println("Finish putting the weight vector at " + output_file); } }
sp, para); System.out.println("w size: " + init_wv.getWeightVectorLength()); System.out.println("w extendable ?: " + init_wv.isExtendable());
+ " #binary: " + binary_size); WeightVector new_wv = new WeightVector(old_wv, 0); // allocate bias term alpha_ins_list, new_wv.isExtendable(), para.WORKINGSETSVM_STOP, MAX_RESOLVED_SVM_ITER, para.verbose_level); alpha_ins_list, new_wv.isExtendable(), para.WORKINGSETSVM_STOP, para.MAX_SVM_ITER, para.verbose_level);
/** * The function for the users to call for the structured SVM * * @param struct_finder * The inference solver (dynamic programming, ILP,...). Given an * input (IInstance) and a Weight vector (WeightVector), return * the best structure (AbstractStructures) * @param sp * Structured Labeled Dataset * @param para * parameters for JLIS * @return * @throws Exception */ @Override public WeightVector trainStructuredSVM( final AbstractLossSensitiveStructureFinder struct_finder, final StructuredProblem sp, JLISParameters para) throws Exception { WeightVector wv = new WeightVector(para.total_number_features + 1); // +1 // because // we // skip // wv.u[0] //wv.setExtendable(false); return getJointWeightVectorFast(wv, struct_finder, sp, empty_b, para); }
@Override public void fillWeightVector(WeightVector w) { w.addSparseFeatureVector(fv, y * alpha); }
public static double getDualObjectiveWithCurrentCuts( L2LossInstanceWithAlphas[] alpha_ins_list, WeightVector cur_wv) { double obj = 0; obj += cur_wv.getTwoNormSquare() * 0.5; for (int i = 0; i < alpha_ins_list.length; i++) { L2LossInstanceWithAlphas instanceWithAlphas = alpha_ins_list[i]; double w_sum = instanceWithAlphas.getLossWeightAlphaSum(); double sum = instanceWithAlphas.getAlphaSum(); double C = instanceWithAlphas.getC(); obj -= w_sum; obj += (1.0 / (4.0 * C)) * sum * sum; } return obj; }
@CommandDescription(description = "<DESCRIPTION>\n" + "\tThis procedure is used to extract the weight vector from a saved model \n" + "<INPUT>\n" + "\tIt receives 2 different arguments. \n" + "\t1) model_file (a string), the file name of a trained model \n" + "\t2) output_file (a string), the file name that will be used to put the contain of the weight vector. \n" + "<OUTPUT>\n" + "\tThe weight vector will be output in the ${output_file}.") public static void outputWeightVector(String model_name, String output_file) throws IOException, ClassNotFoundException { JLISModelIOManager io = new JLISModelIOManager(); MulticlassModel model = (MulticlassModel) io.loadModel(model_name); String[] reverse = model.getReverseMapping(); ArrayList<String> out = new ArrayList<String>(); int start = 0; double[] w = model.wv.getInternalArray(); for (int i = 0; i < reverse.length; i++) { out.add("Label:" + reverse[i]); for (int t = 0; t < model.n_base_feature_in_train; t++) { if (t == model.n_base_feature_in_train - 1) out.add(t + ":" + w[start + t] + " (bias)"); else out.add(t + ":" + w[start + t]); } start += model.n_base_feature_in_train; } LineIO.write(output_file, out); System.out.println("Finish putting the weight vector at " + output_file); }
System.out.println("w size: " + init_wv.getWeightVectorLength()); System.out.println("w extendable ?: " + init_wv.isExtendable());
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; }
@Override public void fillWeightVector(WeightVector w) { // for (Pair<double[], FeatureVector> p : alphafv_map.values()) { for (Pair<double[], FeatureVector> p : al_fv_list) { double alpha = p.getFirst()[0]; FeatureVector fv = p.getSecond(); w.addSparseFeatureVector(fv, alpha); } }
@Override public Pair<IStructure, Double> getLossSensitiveBestStructure( WeightVector weight, IInstance ins, IStructure goldStructure) throws Exception { RerankingInstance ri = (RerankingInstance) ins; LabeledRerankingIns lri = (LabeledRerankingIns) goldStructure; int max_index = -1; double max_score = Double.NEGATIVE_INFINITY; for(int i=0; i < ri.fea_list.size(); i ++){ double loss = -ri.score_list.get(i)+ ri.score_list.get(lri.pred_item); if (loss< 0){ System.out.println("-----------"); System.out.println(ri.score_list); System.out.println("best: " + lri.pred_item); } assert loss >= 0; double score = weight.dotProduct(ri.fea_list.get(i)) + loss; if (score > max_score){ max_score = score; max_index = i; } } assert max_index != -1; double loss = -ri.score_list.get(max_index)+ ri.score_list.get(lri.pred_item); LabeledRerankingIns pred = new LabeledRerankingIns(ri, max_index); //System.out.println("pred: " + max_index + " gold: " + lri.pred_item); return new Pair<IStructure, Double>(pred, loss); }
double dot_product = w.dotProduct(fv); double xij_norm2 = fv.l2NormSqure(); double new_alpha = Math.max(alpha + step, 0); sum_alpha += (new_alpha - alpha); w.addSparseFeatureVector(fv, y * (new_alpha - alpha)); p.setFirst(new_alpha);
final StructuredProblem sp, JLISParameters para) throws Exception { WeightVector wv = new WeightVector(10000);
@Override public void fillWeightVector(WeightVector w) { for (Pair<Double, FeatureVector> p : alphafv_list) { double alpha = p.getFirst(); FeatureVector fv = p.getSecond(); w.addSparseFeatureVector(fv, y * alpha); } }