From: Sunsern C. <sch...@us...> - 2009-02-11 03:15:53
|
Update of /cvsroot/jboost/jboost/src/jboost/booster In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv31999/src/jboost/booster Modified Files: RobustBoost.java Log Message: -remove BinarySearch -fix class specific weighting Index: RobustBoost.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/RobustBoost.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RobustBoost.java 9 Feb 2009 10:45:30 -0000 1.3 --- RobustBoost.java 11 Feb 2009 03:15:39 -0000 1.4 *************** *** 84,96 **** m_cost = cost; ! m_rho = new double[] {calculateRho(m_sigma_f[0],m_epsilon,m_theta[0]), ! calculateRho(m_sigma_f[1],m_epsilon,m_theta[1])}; m_t = 0.0; - - m_conf_rated = conf_rated; m_last_ds = 0; m_last_dt = 0; } --- 84,97 ---- m_cost = cost; ! m_conf_rated = conf_rated; ! ! m_rho = new double[] {calculateRho(m_sigma_f[0],m_epsilon,m_theta[0],m_cost[0]), ! calculateRho(m_sigma_f[1],m_epsilon,m_theta[1],m_cost[1])}; m_t = 0.0; m_last_ds = 0; m_last_dt = 0; + } *************** *** 110,114 **** m_theta[1]= config.getDouble("rb_theta_1", m_theta[1]); - m_sigma_f[0] = sigma_f; m_sigma_f[1] = sigma_f; --- 111,114 ---- *************** *** 120,128 **** m_cost[1]= config.getDouble("rb_cost_1", m_cost[1]); - m_rho = new double[] {calculateRho(m_sigma_f[0],m_epsilon,m_theta[0]), - calculateRho(m_sigma_f[1],m_epsilon,m_theta[1])}; - m_conf_rated = config.getBool("rb_conf_rated", m_conf_rated); } --- 120,128 ---- m_cost[1]= config.getDouble("rb_cost_1", m_cost[1]); m_conf_rated = config.getBool("rb_conf_rated", m_conf_rated); + m_rho = new double[] {calculateRho(m_sigma_f[0],m_epsilon,m_theta[0],m_cost[0]), + calculateRho(m_sigma_f[1],m_epsilon,m_theta[1],m_cost[1])}; + } *************** *** 497,515 **** } ! // check if NewtonSolver fails if (!foundSolution) { - - //System.out.println(">> NewtonSolver failed!"); - //System.out.println(">> Trying BinarySearch..."); ! // try binary search ! BinarySearch bs = new BinarySearch(this, mask, value); ! ! if (bs.isSucceeded()) { ! //System.out.println(">> BinarySearch found a solution!"); ! ds = bs.getDs(); ! dt = bs.getDt(); ! } ! } --- 497,516 ---- } ! // 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; ! } ! } ! } ! } *************** *** 540,548 **** } } // no solutions found else { - //throw new RuntimeException("Error: Cannot find (ds,dt). Please use another value of epsilon, sigma_f or theta!"); - m_last_ds = 0; m_last_dt = 0; --- 541,548 ---- } } + // no solutions found else { m_last_ds = 0; m_last_dt = 0; *************** *** 613,617 **** public static double calculatePotential(boolean conf_rated, double rho, double theta, double sigma_f, double cost, double m, double t) { ! if (conf_rated) { return cost * Math.min(1.0, 1.0 - erf((m - calculateMu(rho,theta,t))/calculateSigma(sigma_f,t))); --- 613,617 ---- public static double calculatePotential(boolean conf_rated, double rho, double theta, double sigma_f, double cost, double m, double t) { ! if (conf_rated) { return cost * Math.min(1.0, 1.0 - erf((m - calculateMu(rho,theta,t))/calculateSigma(sigma_f,t))); *************** *** 648,652 **** */ public static double calculateWeight(boolean conf_rated, double rho, double theta, double sigma_f, double cost, double m, double t) { ! double mu_t = calculateMu(rho, theta, t); double sigma_t_sq = calculateSigmaSquare(sigma_f,t); --- 648,652 ---- */ public static double calculateWeight(boolean conf_rated, double rho, double theta, double sigma_f, double cost, double m, double t) { ! double mu_t = calculateMu(rho, theta, t); double sigma_t_sq = calculateSigmaSquare(sigma_f,t); *************** *** 681,685 **** public int getNumActiveExamples() { ! double EPS = 1E-6; int num = 0; for (int i=0;i<m_numExamples;i++) { --- 681,685 ---- public int getNumActiveExamples() { ! double EPS = 1E-4; int num = 0; for (int i=0;i<m_numExamples;i++) { *************** *** 695,702 **** * @return rho */ ! private double calculateRho(double sigma_f, double epsilon, double theta) { if (!m_conf_rated) epsilon = epsilon * 2; double f1 = Math.sqrt(Math.exp(2.0)*((sigma_f * sigma_f) + 1.0) - 1.0); double f2 = erfinv(1.0 - epsilon); --- 695,704 ---- * @return rho */ ! private double calculateRho(double sigma_f, double epsilon, double theta, double cost) { if (!m_conf_rated) epsilon = epsilon * 2; + epsilon /= cost; + double f1 = Math.sqrt(Math.exp(2.0)*((sigma_f * sigma_f) + 1.0) - 1.0); double f2 = erfinv(1.0 - epsilon); *************** *** 704,707 **** --- 706,710 ---- double denom = 2.0 * (Math.E - 1.0); return numer / denom; + } *************** *** 1161,1166 **** // Found a solution if (Math.abs(F[0]) < RHS_EPS && Math.abs(F[1]) < RHS_EPS && dt > 0) { ! ! if (t + dt > 1) dt = 1 - t; log.append("Found a solution in " + i + " iterations!\n"); log.append("> ds = " + ds + "\n"); --- 1164,1168 ---- // Found a solution if (Math.abs(F[0]) < RHS_EPS && Math.abs(F[1]) < RHS_EPS && dt > 0) { ! //if (t + dt > 1) dt = 1 - t; log.append("Found a solution in " + i + " iterations!\n"); log.append("> ds = " + ds + "\n"); *************** *** 1185,1190 **** log.append("F[1] = " + F[1] + "\n"); ! if (Math.abs(F[1]) < RHS_EPS && dt > 0) { ! if (t + dt > 1) dt = 1 - t; log.append("Found a solution in " + i + " iterations!\n"); log.append("> ds = " + ds + "\n"); --- 1187,1193 ---- log.append("F[1] = " + F[1] + "\n"); ! // if this solution is ok ! if (Math.abs(F[0]) < RHS_EPS && Math.abs(F[1]) < RHS_EPS && dt > 0) { ! //if (t + dt > 1) dt = 1 - t; log.append("Found a solution in " + i + " iterations!\n"); log.append("> ds = " + ds + "\n"); *************** *** 1248,1406 **** } - - /** - * Binary search for solving ds,dt - * - * @author Sunsern Cheamanunkul - * - */ - public class BinarySearch { - - double SOL_EPS = 1E-4; - double EPS = 1E-6; - - protected StringBuffer log; - - protected boolean[] mask; - protected double[] value; - protected double t; - protected double ds,dt; - protected RobustBoost rb; - - protected boolean succeeded; - - public BinarySearch(RobustBoost rb, boolean[] mask, double[] value) { - - this.rb = rb; - this.t = rb.m_t; - this.value = value; - this.mask = mask; - - succeeded = false; - log = new StringBuffer(); - - //solve(); - - } - - - /** - * output is [a b] where - * a = f1(ds,dt) - * b = f2(ds,dt) - * - * @return - */ - private double[] calculateF(double ds, double dt) { - - double[] output = new double[] {0,0}; - double new_t, new_margin, new_weight; - double rho, theta, sigma_f, step, cost; - - new_t = t + dt; - - for (int i=0;i<rb.m_numExamples;i++) { - - rho = rb.m_rho[rb.m_labels[i]]; - theta = rb.m_theta[rb.m_labels[i]]; - sigma_f = rb.m_sigma_f[rb.m_labels[i]]; - cost = rb.m_cost[rb.m_labels[i]]; - step = (mask[i] ? value[rb.m_labels[i]] : 0); - - new_margin = rb.m_margins[i]*Math.exp(-dt) + step*ds; - new_weight = RobustBoost.calculateWeight(rb.m_conf_rated, rho, theta, sigma_f, cost, new_margin, new_t); - - output[0] += step*new_weight; - output[1] += RobustBoost.calculatePotential(rb.m_conf_rated, rho, theta, sigma_f, cost, rb.m_margins[i], t) - - RobustBoost.calculatePotential(rb.m_conf_rated, rho, theta, sigma_f, cost, new_margin, new_t); - - } - - return output; - } - - private void solve() { - - // start at small value - ds = EPS; - - double[] F,F1,F2; - double dt_l,dt_r; - - stop: - while (ds < 10) { - - // set boundary of dt - dt_l = EPS; - dt_r = 1-t; - - F = calculateF(ds,dt_l); - - if (Math.abs(F[1]) > 0.01) break; - - while (Math.abs(dt_l - dt_r) > 0.00001) { - - double dt_m = (dt_l+dt_r)/2; - double dt1 = (dt_l+dt_m)/2; - double dt2 = (dt_r+dt_m)/2; - - F = calculateF(ds,dt_m); - - if (Math.abs(F[1]) < SOL_EPS) { - //if (Math.abs(F[1]) < SOL_EPS && Math.abs(F[0]) < SOL_EPS) { - dt = dt_m; - if (t + dt > 1) dt = 1 - t; - log.append("Found a solution!\n"); - log.append("> ds = " + ds + "\n"); - log.append("> dt = " + dt + "\n"); - log.append("> F[0]=" + F[0] + "\n"); - log.append("> F[1]=" + F[1] + "\n"); - - succeeded = true; - break stop; - } - - F1 = calculateF(ds,dt1); - F2 = calculateF(ds,dt2); - - if (Math.abs(F1[0]) > Math.abs(F2[0])) { - dt_l = dt_m; - } - else { - dt_r = dt_m; - } - - } - - ds += 0.001; - } - - if (!succeeded) { - log.append("BinarySearch failed!\n"); - ds = Double.NaN; - dt = Double.NaN; - } - else { - log.append("BinarySearch completed successfully!\n"); - } - } - - public boolean isSucceeded() { - return succeeded; - } - - public double getDs() { - return ds; - } - - public double getDt() { - return dt; - } - - public String getLog() { - return log.toString(); - } - - } } --- 1251,1254 ---- |