From: Aaron A. <aa...@us...> - 2007-05-27 11:23:29
|
Update of /cvsroot/jboost/jboost/src/jboost/booster In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv27526/src/jboost/booster Modified Files: AbstractBoosterTest.java BoosterTestSuite.java BrownBoost.java BrownBoostTest.java Log Message: BrownBoost implemented w/some tests Index: AbstractBoosterTest.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/AbstractBoosterTest.java,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** AbstractBoosterTest.java 16 May 2007 04:06:02 -0000 1.1.1.1 --- AbstractBoosterTest.java 27 May 2007 11:23:23 -0000 1.2 *************** *** 32,36 **** public AbstractBoosterTest(String arg0) { super(arg0); - } --- 32,35 ---- Index: BoosterTestSuite.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/BoosterTestSuite.java,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** BoosterTestSuite.java 16 May 2007 04:06:02 -0000 1.1.1.1 --- BoosterTestSuite.java 27 May 2007 11:23:23 -0000 1.2 *************** *** 23,29 **** suite.addTest(new TestSuite(AdaBoostTest.class)); suite.addTest(new TestSuite(LogLossBoostTest.class)); ! suite.addTest(new TestSuite(YabaBoostTest.class)); suite.addTest(new TestSuite(BrownBoostTest.class)); ! // suite.addTest(new TestSuite(MulticlassWrapMHTest.class)); //$JUnit-END$ return suite; --- 23,29 ---- suite.addTest(new TestSuite(AdaBoostTest.class)); suite.addTest(new TestSuite(LogLossBoostTest.class)); ! //suite.addTest(new TestSuite(YabaBoostTest.class)); suite.addTest(new TestSuite(BrownBoostTest.class)); ! //suite.addTest(new TestSuite(MulticlassWrapMHTest.class)); //$JUnit-END$ return suite; Index: BrownBoost.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/BrownBoost.java,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** BrownBoost.java 16 May 2007 04:06:02 -0000 1.1.1.1 --- BrownBoost.java 27 May 2007 11:23:23 -0000 1.2 *************** *** 53,57 **** m_s = m_c; m_initialPotential = (1-erf(Math.sqrt(m_c)))/2; ! System.out.println("BrownBoost:\n \t m_c: " + m_c + "\n \t intial potential: " + m_initialPotential); } --- 53,67 ---- m_s = m_c; m_initialPotential = (1-erf(Math.sqrt(m_c)))/2; ! //System.out.println("BrownBoost:\n \t m_c: " + m_c + "\n \t intial potential: " + m_initialPotential); ! } ! ! ! public double getInitialPotential(){ ! return m_initialPotential; ! } ! ! ! public boolean isFinished(){ ! return m_s <= 0; } *************** *** 196,200 **** double dj = margin + bj*alpha + m_s - t; double wj = Math.exp(-(dj*dj)/m_c); ! vars.W += wj; vars.U += wj*dj*bj; --- 206,211 ---- double dj = margin + bj*alpha + m_s - t; double wj = Math.exp(-(dj*dj)/m_c); ! ! /* vars.W += wj; vars.U += wj*dj*bj; *************** *** 204,207 **** --- 215,231 ---- vars.E += erf(dj / Math.sqrt(m_c)) - erf(aj / Math.sqrt(m_c)); vars.Potential += (1-erf( dj /Math.sqrt(m_c)))/2; + */ + + + + double new_margin = margin + bj*alpha; + double new_time_remaining = m_s - t; + double new_weight = calculateWeight(new_margin, new_time_remaining); + double new_potential = calculatePotential(new_margin, new_time_remaining); + double orig_potential = calculatePotential(margin, m_s); + + vars.E += orig_potential - new_potential; // - orig_potential; + vars.B += new_weight * bj; + vars.Potential += calculatePotential(new_margin, new_time_remaining) / m_hypPredictions.length; //System.out.println("Example: " + j + ", Step: " + step + ", Label: " + label + ", Margin: " + margin + ", Pred: " + pred); *************** *** 211,214 **** --- 235,240 ---- } + + protected double solve_constraints(double hyp_err) *************** *** 229,236 **** double new_t = 0.0; - /* ! int k = 0; ! while(k < 200){ k += 1; --- 255,261 ---- double new_t = 0.0; /* ! int k = 0; ! while(k < 200){ k += 1; *************** *** 243,252 **** // then we're done! if (vars.B < EPS && vars.E < EPS){ ! System.out.println("Solved constrains by minimizing B and E."); ! break; } ! if (Math.abs(det) < EPS){ ! System.out.println("DETERMINANT IS TOO SMALL -- SUGGESTS JACOBIAN IS ALMOST SINGULAR!!!"); } --- 268,277 ---- // then we're done! if (vars.B < EPS && vars.E < EPS){ ! System.out.println("Solved constrains by minimizing B and E."); ! break; } ! if (Math.abs(det) < EPS){ ! System.out.println("DETERMINANT IS TOO SMALL -- SUGGESTS JACOBIAN IS ALMOST SINGULAR!!!"); } *************** *** 257,282 **** EPS = 0.001; if (vars.E < EPS && vars.W < EPS){ ! System.out.println("Solved constrains by minimizing E and W."); ! break; } EPS = 0.0001; if (Math.abs(alpha-new_alpha) < EPS){ ! System.out.println("Solved constraints by similar old and new alpha."); ! break; } alpha = new_alpha; t = new_t; ! } ! if (k>=100){ System.err.println("WE DIDN'T ACTUAL FINISH ANYTHING!!! WE JUST TIMED OUT!!!"); ! } ! */ double nr_alpha = alpha; double nr_t = t; ! //System.out.println("Newton-Raphson gives alpha=" + nr_alpha + ", t=" + nr_t); ! // try binary search // find the maximal value for t for which there exists a value --- 282,310 ---- EPS = 0.001; if (vars.E < EPS && vars.W < EPS){ ! System.out.println("Solved constrains by minimizing E and W."); ! break; } EPS = 0.0001; if (Math.abs(alpha-new_alpha) < EPS){ ! System.out.println("Solved constraints by similar old and new alpha."); ! break; } alpha = new_alpha; t = new_t; ! } ! if (k>=100){ System.err.println("WE DIDN'T ACTUAL FINISH ANYTHING!!! WE JUST TIMED OUT!!!"); ! } ! double nr_alpha = alpha; double nr_t = t; ! System.out.println("Newton-Raphson gives alpha=" + nr_alpha + ", t=" + nr_t); ! */ ! ! ! // try binary search // find the maximal value for t for which there exists a value *************** *** 285,336 **** //System.out.println("try binary search"); - double t_step = 0.1; - alpha=-1;//0; - t=-1;//0; - - int NUM_ITERATIONS_WITH_T_OVER_S = 10; - int count_t_large = 0; - - double EPS = 0.00001; - while(Math.abs(t_step) > EPS) { - t+=t_step; ! // TODO: correct this condition. ! if(t>m_s){ ! t = m_s; ! t_step = -t_step/2; ! count_t_large += 1; ! if(count_t_large > NUM_ITERATIONS_WITH_T_OVER_S){ break; } } - - System.out.print("\nt: " + t); - double alpha_step = 0.1; - while(Math.abs(alpha_step) > EPS) { - alpha += alpha_step; - System.out.print(" alpha: " + alpha); - vars = calc_constraints (alpha, t); - if(sign(vars.B) != sign(alpha_step)) alpha_step /= -2; // reverse alpha search direction - if(Math.abs(vars.B) < EPS) break; // found a good enough value for alpha - } - if(sign(vars.E) != sign(t_step)) t_step /= -2; // reverse t search direction - System.out.print("\n(alpha:" + alpha + ", t:" + t + ") is, gamma + pot_diff = |" - + vars.B + "| + |" + vars.E + "| = " - + (Math.abs(vars.B) + Math.abs(vars.E))); - } ! double bs_alpha = alpha; ! double bs_t = t; ! System.out.println("\nBinary Search gives alpha=" + bs_alpha + ", t=" + bs_t); ! //System.out.println("Difference is bs_alpha-nr_alpha=" + (bs_alpha - nr_alpha) + ", bs_t - nr_t" + (bs_t - nr_t)); ! alpha = bs_alpha; t = bs_t; if(t<0) { ! //System.err.println("\nERROR: The value of t: " + t); ! //System.err.println("ERROR: Bad solution for t<0"); m_s = m_oldS; return(0.0); --- 313,416 ---- //System.out.println("try binary search"); ! double t_step = 0.1; ! t=0.3; ! alpha=0; ! ! int NUM_ITERATIONS_FINISH_GAME = 10; ! int count_t_over_s = 0; ! ! double lastE = 0; ! ! double STEP_EPS = 0.00001; ! double CORR_EPS = 0.001; ! ! boolean first_iter = true; ! while(Math.abs(t_step) > STEP_EPS) { ! if(Math.abs(t_step) > m_s){ ! t_step = m_s/2 * sign(t_step); ! } ! ! t+=t_step; ! ! if (t >= m_s) { ! t = m_s - STEP_EPS; ! t_step = -t_step; ! count_t_over_s++; ! // if we keep going over m_s, the game is probably done ! if(count_t_over_s > NUM_ITERATIONS_FINISH_GAME){ ! t = m_s + 0.001; // m_s is updated after loop break; } + continue; } ! if (t < STEP_EPS) { ! t = STEP_EPS; ! t_step = -t_step; ! continue; ! } ! ! alpha = 0; ! //System.out.print("\nt: " + t); ! double alpha_step = 0.1; ! while(Math.abs(alpha_step) > STEP_EPS) { ! ! alpha += alpha_step; ! //System.out.print(" alpha: " + alpha); ! ! ! if(alpha < 0) { ! alpha = STEP_EPS; ! alpha_step = -alpha_step/2; ! continue; ! } ! ! // calculate constraints for values of alpha and t ! vars = calc_constraints (alpha, t); ! ! // reverse alpha search direction ! if(sign(vars.B) != sign(alpha_step)) alpha_step /= -2; ! if(Math.abs(vars.B) < CORR_EPS) break; ! /* ! if (alpha > 5) { ! alpha = 0.1; ! vars = calc_constraints (alpha, t); ! break; ! } ! */ ! } ! ! /* ! System.out.println("(alpha:" + alpha + ", t:" + t + ") is, gamma + pot_diff = |" ! + vars.B + "| + |" + vars.E + "| = " ! + (Math.abs(vars.B) + Math.abs(vars.E))); ! */ ! ! // reverse t search direction ! if(sign(vars.E) != sign(t_step)) t_step /= -2; ! } ! ! // The bisection (binary search) alpha and t ! double bs_alpha = alpha; ! double bs_t = t; ! ! ! /* ! System.out.println("\nBinary Search gives alpha=" + bs_alpha ! + ", t=" + bs_t + ", s=" + m_s ! + ", pot_diff=" + vars.E ! + ", pot=" + vars.Potential); ! */ ! alpha = bs_alpha; t = bs_t; + System.out.format("\tBrownBoost: alpha=%.4f, t=%.4f, time left=%.4f, " + + "potential=%.4f\n", alpha, t, m_s, vars.Potential); if(t<0) { ! System.err.println("\nERROR: The value of t: " + t); ! System.err.println("ERROR: Bad solution for t<0"); m_s = m_oldS; return(0.0); *************** *** 343,346 **** --- 423,428 ---- } + + /** *************** *** 426,430 **** public double calculatePotential(double margin, double time_remaining) { double s = time_remaining; ! return (1-erf((margin+s)/m_c))/2; } --- 508,512 ---- public double calculatePotential(double margin, double time_remaining) { double s = time_remaining; ! return (1-erf((margin+s)/Math.sqrt(m_c)))/2; } *************** *** 536,539 **** --- 618,622 ---- gamma /= total_weight; potential /= m_margins.length; + /* System.out.println("---------------------------------------"); System.out.println("---------------------------------------"); *************** *** 541,544 **** --- 624,629 ---- System.out.print(", potential (unweighted):" + potential); System.out.print(", hyp error (unweighted):" + hyp_err + "\n"); + System.out.println(""); + */ /* Index: BrownBoostTest.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/BrownBoostTest.java,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** BrownBoostTest.java 16 May 2007 04:06:02 -0000 1.1.1.1 --- BrownBoostTest.java 27 May 2007 11:23:23 -0000 1.2 *************** *** 27,37 **** --- 27,43 ---- protected void setUp() throws Exception { m_odd= new BrownBoost(); + ((BrownBoost)m_odd).setRuntime(1.0); m_even= new BrownBoost(); + ((BrownBoost)m_even).setRuntime(1.0); m_allTrue= new BrownBoost(); + ((BrownBoost)m_allTrue).setRuntime(1.0); m_allFalse= new BrownBoost(); + ((BrownBoost)m_allFalse).setRuntime(1.0); m_brownBoost= new BrownBoost(); + ((BrownBoost)m_brownBoost).setRuntime(1.0); m_solitaires= new BrownBoost[COUNT]; for (int i=0; i < COUNT; i++) { m_solitaires[i]= new BrownBoost(); + ((BrownBoost)m_solitaires[i]).setRuntime(1.0); } super.setUp(); *************** *** 80,83 **** --- 86,90 ---- //TODO Implement update(). // fill in booster + Bag bags[]; int[] indices= new int[COUNT]; int[] ones= new int[COUNT/2]; *************** *** 96,101 **** /* m_brownBoost.finalizeData(); ! Prediction p1= m_brownBoost.getPrediction(m_brownBoost.newBag(zeroes)); ! m_brownBoost.update(new Prediction[] {p1}, new int[][] {zeroes}); for (int i=0; i < zeroes.length; i++) { --- 103,111 ---- /* m_brownBoost.finalizeData(); ! int[][] partition = new int[][] {zeroes}; ! bags = new Bag[1]; ! bags[0] = m_brownBoost.newBag(zeroes); ! Prediction[] p1= ((BrownBoost)m_brownBoost).getPredictions(bags, partition); ! m_brownBoost.update(p1, partition); for (int i=0; i < zeroes.length; i++) { *************** *** 104,116 **** int index= zeroes[j]; double weight= m_brownBoost.m_weights[index]; ! double margin= m_brownBoost.m_oldMargins[index]; ! double s = m_brownBoost.m_oldS; double c = m_brownBoost.m_c; ! if(Double.isNaN(weight)){ ! System.err.println("Warning: weight is NaN!"); ! assertEquals(-1,1,0.0001); ! continue; ! } ! assertEquals(weight, Math.exp(-Math.pow(margin+s,2)/c), 0.00001); } } --- 114,124 ---- int index= zeroes[j]; double weight= m_brownBoost.m_weights[index]; ! double margin= m_brownBoost.m_margins[index]; ! double s = m_brownBoost.m_s; double c = m_brownBoost.m_c; ! System.out.println("weight, margin, s, c : " + weight ! + ", " + margin ! + ", " + s + ", " + c); ! assertEquals(weight, Math.exp(-Math.pow(margin+s,2)/c), 0.0001); } } *************** *** 118,123 **** for (int i=0; i < ones.length; i++) { ones[i]= indices[COUNT/2 + i]; ! p1= m_brownBoost.getPrediction(m_brownBoost.newBag(ones)); ! m_brownBoost.update(new Prediction[] {p1}, new int[][] {ones}); for (int j=0; j < ones.length; j++) { int index= ones[j]; --- 126,134 ---- for (int i=0; i < ones.length; i++) { ones[i]= indices[COUNT/2 + i]; ! bags = new Bag[1]; ! bags[0] = m_brownBoost.newBag(ones); ! int[][] parition = new int[][] {ones}; ! p1 = ((BrownBoost)m_brownBoost).getPredictions(bags, partition); ! m_brownBoost.update(p1, parition); for (int j=0; j < ones.length; j++) { int index= ones[j]; *************** *** 130,134 **** continue; } ! assertEquals(weight, Math.exp(-Math.pow(margin+s,2)/c), 0.00001); } } --- 141,145 ---- continue; } ! assertEquals(weight, Math.exp(-Math.pow(margin+s,2)/c), 0.001); } } *************** *** 169,175 **** final public void testErf() { - System.out.println("erf(0.5):"+BrownBoost.erf(0.5)); - System.out.println("erf(1):"+BrownBoost.erf(1)); - System.out.println("erf(2):"+BrownBoost.erf(2)); assertEquals(BrownBoost.erf(0.5), .5204998, 0.0001); assertEquals(BrownBoost.erf(1.0), .8427007, 0.0001); --- 180,183 ---- |