From: Aaron A. <aa...@us...> - 2007-06-14 16:37:50
|
Update of /cvsroot/jboost/jboost/src/jboost/booster In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv21194/src/jboost/booster Modified Files: YabaBoost.java Log Message: can now move the paramter 'theta' to save potential Index: YabaBoost.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/YabaBoost.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** YabaBoost.java 31 May 2007 10:03:23 -0000 1.2 --- YabaBoost.java 14 Jun 2007 16:37:46 -0000 1.3 *************** *** 55,64 **** protected double m_theta; - /** ! * The average potential of the set of examples. ! protected double m_avgPotential; */ ! /** * A parameter indicating when we should stop the game. --- 55,65 ---- protected double m_theta; /** ! * This paramter cannot be changed and contains the original value ! * for theta. */ ! protected double m_origTheta; ! ! /** * A parameter indicating when we should stop the game. *************** *** 68,71 **** --- 69,80 ---- */ protected static final double FINISH_GAME_NOW = 0.05; + + /** + * A parameter indicating when we should stop the game. + * Technically, this should be 0; however due to numerical instability + * we finish the game slightly earlier then we should. This leads to + * an approximation of the 0/1 loss instead of the exact 0/1 loss. + */ + protected static final double THETA_UPDATE_STEP = 0.05; /** *************** *** 90,98 **** m_c = runtime; m_s = m_c; } public void setParams(double c1, double c2, double theta) throws Exception { double EPS = 0.01; ! if (c1 < EPS || c2 < EPS || theta < EPS) { throw new Exception("Yaba params are bad!\nc1:"+c1+"\nc2:" +c2+"\ntheta:"+theta+"\n"); --- 99,108 ---- m_c = runtime; m_s = m_c; + m_initialPotential = calculatePotential(0,m_c); } public void setParams(double c1, double c2, double theta) throws Exception { double EPS = 0.01; ! if (c1 < EPS || c2 < EPS || theta < EPS || m_c < EPS) { throw new Exception("Yaba params are bad!\nc1:"+c1+"\nc2:" +c2+"\ntheta:"+theta+"\n"); *************** *** 101,104 **** --- 111,116 ---- m_c2 = c2; m_theta = theta; + m_origTheta = theta; + m_initialPotential = calculatePotential(0,m_c); } *************** *** 129,149 **** protected ErfVars calc_constraints(double alpha, double t) { - /* - * Make sure that all examples are passed in. Not just the ones that - * have predictions this round. - - int num_examples = 0; - for (int i=0; i < exampleIndex.length; i++) { - num_examples += exampleIndex[i].length; - } - if (num_examples != m_margins.length) { - System.err.println("YabaBoost.calc_constraints: " - + "Not all examples are given"); - System.err.println("YabaBoost.calc_constraints: " - + "num example given is " + num_examples); - System.exit(2); - } - */ - ErfVars vars = new ErfVars(); --- 141,144 ---- *************** *** 168,174 **** double step = getStep(m_labels[example], m_hypPredictions[example]); ! double new_margin = (1-alpha)*margin + alpha*step; ! double new_weight = calculateWeight(new_margin, new_time_remaining); double new_pot = calculatePotential(new_margin, new_time_remaining); double orig_pot = calculatePotential(orig_margin, orig_time_remaining); --- 163,176 ---- double step = getStep(m_labels[example], m_hypPredictions[example]); ! double new_margin; ! ! double EPS = 0.0001; ! if (Math.abs(step) > EPS) { ! new_margin = (1-alpha)*margin + alpha*step; ! } else { ! new_margin = margin; ! } + double new_weight = calculateWeight(new_margin, new_time_remaining); double new_pot = calculatePotential(new_margin, new_time_remaining); double orig_pot = calculatePotential(orig_margin, orig_time_remaining); *************** *** 176,180 **** vars.B += new_weight*step; vars.E += orig_pot - new_pot; ! vars.Potential += new_pot / m_hypPredictions.length; totalWeight += new_weight; --- 178,182 ---- vars.B += new_weight*step; vars.E += orig_pot - new_pot; ! vars.Potential += new_pot; totalWeight += new_weight; *************** *** 184,187 **** --- 186,191 ---- } vars.B /= totalWeight; + vars.E /= m_hypPredictions.length; + vars.Potential /= m_hypPredictions.length; return vars; } *************** *** 202,207 **** System.err.println(""); System.err.println("Dumping everything"); - System.err.println("% " + v.Potential + ", " + m_initialPotential); - dumpfile.println("% Avg Diff Potential, Gamma correlation"); dumpfile.println("% " + v.E + ", " + v.B); --- 206,209 ---- *************** *** 286,292 **** double lastE = 0; ! ! ! double STEP_EPS = 0.00001; boolean first_iter = true; while(Math.abs(t_step) > STEP_EPS) { --- 288,292 ---- double lastE = 0; ! double STEP_EPS = 0.0001; boolean first_iter = true; while(Math.abs(t_step) > STEP_EPS) { *************** *** 304,310 **** * become as large as it needs to be. If it is too large, * we trim it down after the fact. Or maybe we do want to ! * do this. If we let t>m_s then we wind up with NaN. */ if (t >= m_s) { ! t = m_s - STEP_EPS; t_step = -t_step; count_t_over_s++; --- 304,311 ---- * become as large as it needs to be. If it is too large, * we trim it down after the fact. Or maybe we do want to ! * do this. If we let t>m_s then we wind up with NaN. ! */ if (t >= m_s) { ! t = m_s - 0.001; t_step = -t_step; count_t_over_s++; *************** *** 316,321 **** continue; } ! ! if (t < STEP_EPS) { t = STEP_EPS; --- 317,321 ---- continue; } ! if (t < STEP_EPS) { t = STEP_EPS; *************** *** 347,354 **** } ! System.out.format("\tYabaBoost: (alpha=%.4f, t=%.4f) -> (corr=%.6f, pot_diff=%.6f), " ! + "potential=%.6f\n", alpha, t, vars.B, vars.E, vars.Potential); // reverse t search direction if(sign(vars.E) != sign(t_step)) t_step /= -2; } --- 347,357 ---- } ! System.out.format("\tYabaBoost: (alpha=%.4f, t=%.4f) -> " ! + "(corr=%.6f, pot_diff=%.6f, pot_diff=%.6f), " ! + "potential=%.6f\n", alpha, t, vars.B, vars.E, ! m_initialPotential - vars.Potential, vars.Potential); // reverse t search direction + vars.E = m_initialPotential - vars.Potential; if(sign(vars.E) != sign(t_step)) t_step /= -2; } *************** *** 358,386 **** double bs_t = t; - // We use the binary search alpha and t alpha = bs_alpha; t = bs_t; - - System.out.format("YabaBoost: alpha=%.4f, t=%.4f, time left=%.4f, " - + "potential=%.4f\n", alpha, t, m_s, vars.Potential); - - - m_oldS = m_s; m_s -= t; ! /* Weird things happen towards the end of the game ! * (underflow/overflow). So we cheat a little and terminate ! * before the actual end of the game. */ ! if (m_s < FINISH_GAME_NOW) { ! m_s = -1; ! } - if (m_s < 0) { - alpha = 0; - } - System.out.println("Binary Search gives alpha=" + bs_alpha + ", t=" + bs_t); System.out.println("We use binary searchalpha=" + alpha + ", t=" + t); --- 361,374 ---- double bs_t = t; // We use the binary search alpha and t alpha = bs_alpha; t = bs_t; m_oldS = m_s; m_s -= t; ! System.out.format("YabaBoost: alpha=%.4f, t=%.4f, time left=%.4f, " ! + "potential=%.4f, theta=%.4f\n", alpha, t, m_s, vars.Potential, m_theta); System.out.println("Binary Search gives alpha=" + bs_alpha + ", t=" + bs_t); System.out.println("We use binary searchalpha=" + alpha + ", t=" + t); *************** *** 390,404 **** ! ! double CONSTRAINT_EPS = 0.001; ! if (t < m_s && ! (Math.abs(vars.Potential-m_initialPotential)>CONSTRAINT_EPS ! || Math.abs(vars.B)>CONSTRAINT_EPS)) { ! m_s = m_oldS; dump_everything(vars); ! System.err.println("Average potential difference or correlation is too big!"); System.err.println("Outputting all data to outfile!"); ! System.exit(2); } --- 378,423 ---- ! /* ! * When we can't obtain the potential we wanted, we need to ! * adjust the paramters. We adjust theta and c1 to maintain a ! * constant potential. ! */ double CONSTRAINT_EPS = 0.001; ! double EPS = 0.001; ! if (m_s > FINISH_GAME_NOW && Math.abs(vars.Potential-m_initialPotential)>CONSTRAINT_EPS) { ! m_s = m_oldS; dump_everything(vars); ! System.err.println("Average potential difference is too big!"); System.err.println("Outputting all data to outfile!"); ! ! s = m_c - m_s; ! double old_mu = m_theta - m_c1* ( Math.exp(-s) - Math.exp(-m_c) ); ! ! double theta_step = 0; ! if(vars.Potential-m_initialPotential > 0) { ! theta_step = - THETA_UPDATE_STEP; ! } else if(vars.Potential-m_initialPotential < 0) { ! theta_step = THETA_UPDATE_STEP/(Math.PI/3); // irrational number ! } ! m_theta += theta_step; ! m_c1 = m_c1 + theta_step / (Math.exp(-(0)) - Math.exp(-m_c)); ! ! double new_mu = m_theta - m_c1* ( Math.exp(-s) - Math.exp(-m_c) ); ! ! System.out.println("old_mu="+old_mu+", new_mu="+new_mu); ! double margin = 0; ! s = 0; ! double mu = m_theta - m_c1 * (Math.exp(-s) - Math.exp(-m_c)); ! double sd = Math.sqrt(m_c2 * (Math.exp(-2*s) - Math.exp(-2*m_c))) + FINISH_GAME_NOW; ! double pot = (1 - erf((margin-mu)/sd)) / 2; ! System.out.println("New potential is: " + pot); ! System.out.println("New intial potential is: " + calculatePotential(0, m_c)); ! System.out.println("New theta is:" + m_theta); ! return solve_constraints(hyp_err); ! } ! ! if (Math.abs(vars.B) > CONSTRAINT_EPS) { ! ! System.err.println("CORRELATION IS TOO BIG!!!"); } *************** *** 410,417 **** } ! ! ! ! m_lastAlpha = alpha; --- 429,440 ---- } ! /* ! * Weird things happen towards the end of the game ! * (underflow/overflow). So we cheat a little and terminate ! * before the actual end of the game. ! */ ! if (m_s < FINISH_GAME_NOW) { ! m_s = -1; ! } m_lastAlpha = alpha; *************** *** 431,444 **** return; } - - // save old m_weights - for (int i= 0; i < m_weights.length; i++){ - m_oldWeights[i]= m_weights[i]; - } - - // save old m_margins - for (int i= 0; i < m_margins.length; i++){ - m_oldMargins[i] = m_margins[i]; - } for (int i= 0; i < exampleIndex.length; i++) { --- 454,457 ---- *************** *** 461,464 **** --- 474,478 ---- System.err.println("prediction is: " + Math.abs(p)); } + m_oldMargins[example] = m_margins[example]; m_margins[example] = (1-alpha)*m_margins[example] + value[m_labels[example]]; *************** *** 468,489 **** + m_margins[example] + " greater than 1!!!!"); } - - m_totalWeight -= m_weights[example]; - m_weights[example] = calculateWeight(m_margins[example]); - m_totalWeight += m_weights[example]; } } ! // print all margins. ! /* ! for (int i= 0; i < m_margins.length; i++){ ! System.out.println("Margin: " + (m_margins[i] >= 0 ? " " : "") + m_margins[i] ); ! } ! */ } - /** --- 482,501 ---- + m_margins[example] + " greater than 1!!!!"); } } } ! for (int i=0; i < m_hypPredictions.length; i++) { ! m_oldWeights[i]= m_weights[i]; ! m_totalWeight -= m_weights[i]; ! m_weights[i] = calculateWeight(m_margins[i]); ! m_totalWeight += m_weights[i]; + m_totalPotential -= m_potentials[i]; + m_potentials[i] = calculatePotential(m_margins[i]); + m_totalPotential += m_potentials[i]; + } } /** *************** *** 511,515 **** double s = m_c - time_remaining; double mu = m_theta - m_c1* ( Math.exp(-s) - Math.exp(-m_c) ); ! double sd = Math.sqrt(m_c2*(Math.exp(-2*s) - Math.exp(-2*m_c))); double norm = 1 / Math.sqrt(Math.PI * sd * sd); return norm * Math.exp(- Math.pow((margin-mu)/sd,2)); --- 523,527 ---- double s = m_c - time_remaining; double mu = m_theta - m_c1* ( Math.exp(-s) - Math.exp(-m_c) ); ! double sd = Math.sqrt(m_c2*(Math.exp(-2*s) - Math.exp(-2*m_c))) + FINISH_GAME_NOW; double norm = 1 / Math.sqrt(Math.PI * sd * sd); return norm * Math.exp(- Math.pow((margin-mu)/sd,2)); *************** *** 524,528 **** double s = m_c - time_remaining; double mu = m_theta - m_c1 * (Math.exp(-s) - Math.exp(-m_c)); ! double sd = Math.sqrt(m_c2 * (Math.exp(-2*s) - Math.exp(-2*m_c))); double pot = (1 - erf((margin-mu)/sd)) / 2; /* --- 536,540 ---- double s = m_c - time_remaining; double mu = m_theta - m_c1 * (Math.exp(-s) - Math.exp(-m_c)); ! double sd = Math.sqrt(m_c2 * (Math.exp(-2*s) - Math.exp(-2*m_c))) + FINISH_GAME_NOW; double pot = (1 - erf((margin-mu)/sd)) / 2; /* *************** *** 575,579 **** /** Place holder to ensure that this function is not used in BrownBoost. */ public MixedBinaryPrediction calcPrediction(){ ! //System.err.println("Need to have alpha for prediction in BrownBag.calcPrediction()!"); return new MixedBinaryPrediction(0); } --- 587,592 ---- /** Place holder to ensure that this function is not used in BrownBoost. */ public MixedBinaryPrediction calcPrediction(){ ! System.err.println("Need to have alpha for prediction in BrownBag.calcPrediction()!"); ! System.exit(2); return new MixedBinaryPrediction(0); } |