From: Sunsern C. <sch...@us...> - 2009-03-12 23:42:13
|
Update of /cvsroot/jboost/jboost/src/jboost/booster In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv16854/src/jboost/booster Modified Files: RobustBoost.java Log Message: * Remove an early stopping criteria from RobustBoost * New HistogramFrame.java and DataSet.java that works with new VisualizeScores.py. Index: RobustBoost.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/RobustBoost.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** RobustBoost.java 19 Feb 2009 14:22:20 -0000 1.5 --- RobustBoost.java 12 Mar 2009 23:42:04 -0000 1.6 *************** *** 33,58 **** protected int m_numExamples; ! /** RobustBoost: current time in [0,1] */ protected double m_t; ! /** RobustBoost: epsilon, fraction of allowed errors */ protected double m_epsilon; ! /** RobustBoost: conf.rated flag */ protected boolean m_conf_rated; ! /** RobustBoost: sigma_f for each class */ protected double[] m_sigma_f; ! /** RobustBoost: theta (goal margin) for each class */ protected double[] m_theta; ! /** RobustBoost: rho for each class */ protected double[] m_rho; ! /** RobustBoost: mistake cost for each class */ protected double[] m_cost; ! /** RobustBoost: most recently used ds */ protected double m_last_ds; ! /** RobustBoost: most recently used dt */ protected double m_last_dt; protected double m_old_t; /** temporary location for storing the examples as they are read in */ protected List<TmpData> m_tmpList; --- 33,64 ---- protected int m_numExamples; ! /** current RobustBoost time [0,1] */ protected double m_t; ! /** epsilon, fraction of allowed errors */ protected double m_epsilon; ! /** conf.rated flag */ protected boolean m_conf_rated; ! /** sigma_f for each class */ protected double[] m_sigma_f; ! /** theta (goal margin) for each class */ protected double[] m_theta; ! /** rho for each class */ protected double[] m_rho; ! /** mistake cost for each class */ protected double[] m_cost; ! /** most recently used ds */ protected double m_last_ds; ! /** most recently used dt */ protected double m_last_dt; + /** RobustBoost time at last iteration */ protected double m_old_t; + /** minimum epsilon that works */ + protected static final double MIN_EPSILON = 1E-6; + /** maximum epsilon that works */ + protected static final double MAX_EPSILON = 0.999; + /** temporary location for storing the examples as they are read in */ protected List<TmpData> m_tmpList; *************** *** 79,84 **** m_tmpList= new ArrayList<TmpData>(); m_numExamples= 0; ! ! m_epsilon = epsilon; m_theta = theta; m_sigma_f = sigma_f; --- 85,92 ---- m_tmpList= new ArrayList<TmpData>(); m_numExamples= 0; ! ! m_epsilon = Math.max(epsilon, MIN_EPSILON); ! m_epsilon = Math.min(m_epsilon, MAX_EPSILON); ! m_theta = theta; m_sigma_f = sigma_f; *************** *** 104,108 **** public void init(Configuration config) { ! m_epsilon= config.getDouble("rb_epsilon", m_epsilon); double theta= config.getDouble("rb_theta", m_theta[0]); double sigma_f= config.getDouble("rb_sigma_f", m_sigma_f[0]); --- 112,118 ---- public void init(Configuration config) { ! m_epsilon= Math.max(config.getDouble("rb_epsilon", m_epsilon), MIN_EPSILON); ! m_epsilon= Math.min(m_epsilon, MAX_EPSILON); ! double theta= config.getDouble("rb_theta", m_theta[0]); double sigma_f= config.getDouble("rb_sigma_f", m_sigma_f[0]); *************** *** 300,311 **** } - public double getLastDs() { - return m_last_ds; - } - - public double getLastDt() { - return m_last_dt; - } - /** * Returns a string with all the weights, margins, etc --- 310,313 ---- *************** *** 397,401 **** public Prediction[] getPredictions(Bag[] bags, int[][] exampleIndex, Prediction[] basePredictions) { ! final double EPS = 1E-5; Prediction[] predictions = new RobustBinaryPrediction[bags.length]; --- 399,403 ---- public Prediction[] getPredictions(Bag[] bags, int[][] exampleIndex, Prediction[] basePredictions) { ! final double EPS = 1E-7; Prediction[] predictions = new RobustBinaryPrediction[bags.length]; *************** *** 407,412 **** double bp = basePredictions[i].getClassScores()[1]; ! // if bp (confidence of weak learner) is too small, don't make any prediction ! if (Math.abs(bp) < EPS) { predictions[i] = new RobustBinaryPrediction(0.0); } --- 409,414 ---- double bp = basePredictions[i].getClassScores()[1]; ! // if bp (confidence of weak learner) is too small or bag is weightless, don't make any prediction ! if (Math.abs(bp) < EPS || bags[i].isWeightless()) { predictions[i] = new RobustBinaryPrediction(0.0); } *************** *** 439,443 **** public void update(Prediction[] predictions, int[][] exampleIndex) { ! final double EPS = 1E-5; // save old m_weights --- 441,445 ---- public void update(Prediction[] predictions, int[][] exampleIndex) { ! final double EPS = 1E-7; // save old m_weights *************** *** 458,462 **** double[] value = rbp.getClassScores(); ! // check if this is a zero prediction if (Math.abs(value[1]) < EPS) { continue; --- 460,464 ---- double[] value = rbp.getClassScores(); ! // if this is a zero prediction, we skip if (Math.abs(value[1]) < EPS) { continue; *************** *** 467,489 **** for (int j= 0; j < exampleIndex[i].length; j++) mask[exampleIndex[i][j]] = true; - // create a set of starting points for NS - double init_ds, init_dt; - double[][] initial_points = new double[3][]; - - // #1. go as far in the future as possible - init_dt = 1 - m_t - 0.01; - init_ds = Math.sqrt(init_dt); - initial_points[0] = new double[] {init_ds, init_dt}; - - // #2. alpha in adaboost - init_ds = rbp.init_ds; - init_dt = init_ds*init_ds; - initial_points[1] = new double[] {init_ds, init_dt}; - - // #3. most recently used - init_ds = m_last_ds; - init_dt = m_last_dt; - initial_points[2] = new double[] {init_ds, init_dt}; - // how many iterations in NewtonSolver before we give up int max_iter = 30; --- 469,472 ---- *************** *** 491,525 **** boolean foundSolution = false; double ds = Double.NaN, dt = Double.NaN; ! for (int k=0;k<initial_points.length;k++) { ! NewtonSolver ns = new NewtonSolver(this, mask, value, initial_points[k], max_iter); ! ds = ns.getDs(); ! dt = ns.getDt(); ! if (ns.isSucceeded()) { ! foundSolution = true; ! break; ! } } ! // if failed, do more search with starting point on a grid ! if (!foundSolution) { ! done: ! for (double temp_ds=0;temp_ds < 10;temp_ds+=0.1) { ! for (double temp_dt=0;temp_dt < 1;temp_dt+=0.1) { ! double[] init_p = new double[] {temp_ds, temp_dt}; ! NewtonSolver ns = new NewtonSolver(this,mask,value,init_p,30); ! if (ns.isSucceeded()) { ! ds = ns.getDs(); ! dt = ns.getDt(); ! break done; } } ! } ! } // if there is a valid solution ! if (ds != Double.NaN && dt != Double.NaN && dt > 0) { --- 474,536 ---- boolean foundSolution = false; double ds = Double.NaN, dt = Double.NaN; + + // create a solver + NewtonSolver ns = new NewtonSolver(this, mask, value, max_iter); ! if (ns.canFinishNow()) { ! foundSolution = true; ! ds = 0; ! dt = Math.abs(1 - m_t); } + else { + // create a set of starting points for NS + double init_ds, init_dt; + double[][] initial_points = new double[3][]; ! // #1. go as far in the future as possible ! init_dt = 1 - m_t; ! init_ds = Math.sqrt(init_dt); ! initial_points[0] = new double[] {init_ds, init_dt}; ! // #2. alpha in adaboost ! init_ds = rbp.init_ds; ! init_dt = init_ds*init_ds; ! initial_points[1] = new double[] {init_ds, init_dt}; ! ! // #3. most recently used ! init_ds = m_last_ds; ! init_dt = m_last_dt; ! initial_points[2] = new double[] {init_ds, init_dt}; ! ! for (int k=0;k<initial_points.length;k++) { ! ns.solve(initial_points[k][0], initial_points[k][1]); ! if (ns.isSucceeded()) { ! ds = ns.getDs(); ! dt = ns.getDt(); ! foundSolution = true; ! break; ! } ! } ! ! // if failed, do more search with starting point on a grid ! if (!foundSolution) { ! done: ! for (double temp_ds=0;temp_ds < 10;temp_ds+=0.1) { ! for (double temp_dt=0.1;temp_dt < 1-m_t;temp_dt+=0.1) { ! ns.solve(temp_ds, temp_dt); ! if (ns.isSucceeded()) { ! ds = ns.getDs(); ! dt = ns.getDt(); ! foundSolution = true; ! break done; ! } } } ! } } // if there is a valid solution ! if (foundSolution && ! ds != Double.NaN && dt != Double.NaN && dt > 0) { *************** *** 550,553 **** --- 561,566 ---- else { + System.out.println("Warning: Newton's method has failed. This might or might not be a problem."); + m_last_ds = 0; m_last_dt = 0; *************** *** 557,563 **** } ! } // update m_weights and m_potentials m_totalWeight = 0; --- 570,578 ---- } ! ! //System.out.println("T = " + m_t); } + // update m_weights and m_potentials m_totalWeight = 0; *************** *** 577,581 **** */ public boolean isFinished() { ! return (1 - m_t < 0.001 || (m_t > 0 && (m_t - m_old_t) < 1E-7)); } --- 592,596 ---- */ public boolean isFinished() { ! return (1 - m_t < 0.001); } *************** *** 686,690 **** public int getNumActiveExamples() { ! double EPS = 1E-4; int num = 0; for (int i=0;i<m_numExamples;i++) { --- 701,705 ---- public int getNumActiveExamples() { ! double EPS = 1E-7; int num = 0; for (int i=0;i<m_numExamples;i++) { *************** *** 1025,1029 **** double RHS_EPS = 1E-5; - double TIME_EPS = 1E-5; double DET_EPS = 1E-7; --- 1040,1043 ---- *************** *** 1033,1037 **** protected double[] value; protected double t; - protected double[] initial_point; protected int max_iter; protected double ds,dt; --- 1047,1050 ---- *************** *** 1040,1044 **** protected boolean succeeded; ! public NewtonSolver(RobustBoost rb, boolean[] mask, double[] value, double[] init_point, int max_iter) { this.rb = rb; --- 1053,1057 ---- protected boolean succeeded; ! public NewtonSolver(RobustBoost rb, boolean[] mask, double[] value, int max_iter) { this.rb = rb; *************** *** 1047,1058 **** this.t = rb.m_t; - this.initial_point = init_point; this.max_iter = max_iter; succeeded = false; log = new StringBuffer(); - - solve(); - } --- 1060,1071 ---- this.t = rb.m_t; this.max_iter = max_iter; succeeded = false; + + this.ds = Double.NaN; + this.dt = Double.NaN; + log = new StringBuffer(); } *************** *** 1067,1071 **** * @return */ ! private double[] calculateJ() { double[] output = new double[] {0,0,0,0}; --- 1080,1084 ---- * @return */ ! public double[] calculateJ(double ds, double dt) { double[] output = new double[] {0,0,0,0}; *************** *** 1118,1122 **** * @return */ ! private double[] calculateF() { double[] output = new double[] {0,0}; --- 1131,1135 ---- * @return */ ! public double[] calculateF(double ds, double dt) { double[] output = new double[] {0,0}; *************** *** 1145,1153 **** return output; } ! private void solve() { ! ! ds = initial_point[0]; ! dt = initial_point[1]; double[] F; --- 1158,1168 ---- return output; } + + public void solve(double init_ds, double init_dt) { ! succeeded = false; ! ! ds = init_ds; ! dt = init_dt; double[] F; *************** *** 1160,1165 **** // calculate F and J ! F = calculateF(); ! J = calculateJ(); // solve for dds, ddt --- 1175,1180 ---- // calculate F and J ! F = calculateF(ds,dt); ! J = calculateJ(ds,dt); // solve for dds, ddt *************** *** 1255,1258 **** --- 1270,1279 ---- } + public boolean canFinishNow() { + double[] F = calculateF(0, 1 - t); + if (-F[1]/m_numExamples < Math.max(0.2*m_epsilon,0.00001) ) return true; + else return false; + } + } |