From: Sunsern C. <sch...@us...> - 2009-02-05 05:54:05
|
Update of /cvsroot/jboost/jboost/src/jboost/booster In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv8885/src/jboost/booster Added Files: RobustBinaryPrediction.java RobustBoostTest.java RobustBoost.java Log Message: Added RobustBoost classes to JBoost. --- NEW FILE: RobustBoostTest.java --- package jboost.booster; import jboost.booster.RobustBoost.NewtonSolver; /** * @author Sunsern Cheamanunkul * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class RobustBoostTest extends AbstractBoosterTest { RobustBoost m_robustBoost; /** * Constructor for BrownBoostTest. * @param arg0 */ public RobustBoostTest(String arg0) { super(arg0); } /** * Tests the BrownBoost constructor and sets up boosters for * other tests. * @see TestCase#setUp() */ protected void setUp() throws Exception { m_odd= new RobustBoost(); m_even= new RobustBoost(); m_allTrue= new RobustBoost(); m_allFalse= new RobustBoost(); m_robustBoost= new RobustBoost(); m_solitaires= new RobustBoost[COUNT]; for (int i=0; i < COUNT; i++) { m_solitaires[i]= new RobustBoost(); } super.setUp(); } final public void testAddExample() { //TODO Implement addExample(). } final public void testFinalizeData() { //TODO Implement finalizeData(). } final public void testClear() { //TODO Implement clear(). } /* * Test for Bag newBag(int[]) */ final public void testNewBagintArray() { //TODO Implement newBag(). } /* * Test for Bag newBag() */ final public void testNewBag() { //TODO Implement newBag(). } /* * Test for Bag newBag(Bag) */ final public void testNewBagBag() { //TODO Implement newBag(). } public final void testUpdate() { } final public void testGetPredictions() { //TODO Implement getPredictions(). } final public void testGetTheoryBound() { //TODO Implement getTheoryBound(). } final public void testGetMargins() { //TODO Implement getMargins(). } final public void testGetPrediction() { //TODO Implement getPrediction(). } final public void testNewtonSolver() { double sigma_f = 0.1d; double theta = 0.2d; double rho = 1.6; double[] margins = new double[] {-10,-10,-10,-10,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,10,10,10,10}; double[] steps = new double[] {-1,-1,1,1,-1,-1,1,1,1,-1,1,-1,1,-1,1,1,1,1,-1,1,1}; NewtonSolver ns = m_robustBoost.new NewtonSolver(margins,steps,0.1,0.6,10,rho,theta,sigma_f); //System.out.print(ns.getLog()); assertEquals(ns.getDs(),1.427, 0.001); assertEquals(ns.getDt(),0.263, 0.001); } } --- NEW FILE: RobustBinaryPrediction.java --- package jboost.booster; import jboost.examples.Label; /** * This is used by RobustBoost. * * This is a prediction for a binary label, which consists of a single * real valued number whose sign is the prediction and whose magnitude * is the prediction confidence **/ public class RobustBinaryPrediction extends Prediction{ /** time step used to scale previous predictions */ protected double dt; protected double prediction; public RobustBinaryPrediction(double p) {prediction=p; dt=0; } public RobustBinaryPrediction() {prediction=0.0; dt=0; } public RobustBinaryPrediction(double p, double dt) {prediction=p; this.dt=dt;} public double getDt() { return dt; } public void setDt(double dt) { this.dt = dt; } public Object clone(){ Object a = new RobustBinaryPrediction(prediction); return a; } public Prediction add(Prediction p) { if (p != null) { prediction += ((RobustBinaryPrediction) p).prediction; } return this; } public Prediction scale(double w) { prediction *= w; return this; } public Prediction add(double w, Prediction p) { prediction += w * ((RobustBinaryPrediction) p).prediction; return this; } public double[] getClassScores() { double[] a = {-prediction,prediction}; return a; } /** * computes margin as y * prediction, where y = -1 if label = 0, * y = +1 if label = 1. */ public double[] getMargins(Label label) { int l = label.getSingleValue(); if(l > 1 || l < 0) { throw new IllegalArgumentException( "Adaboost.getMargin should get a label which " + "is either 0 or 1. Instead it got " + l); } return new double[] { (l == 1 ? prediction : -prediction) }; } public boolean equals(Prediction other) { RobustBinaryPrediction bp= (RobustBinaryPrediction) other; return (prediction == bp.prediction); } /** * computes margin as absolute value of prediction. */ /* public double getMargin() { return (prediction < 0.0 ? -prediction : prediction); } */ public Label getBestClass() { return new Label(prediction > 0 ? 1 : 0); } public String toString() { return "BinaryPrediction. p(1)= "+prediction; } public String shortText() { return Double.toString(prediction); } public String cPreamble() { return "typedef double Prediction_t;\n" + "#define reset_pred() {p = 0.0;}\n" + "#define add_pred(X) {p += (X);}\n" + "#define finalize_pred()" + " ((r) ? (r[1] = p , r[0] = -p) : -p)\n"; } public String javaPreamble() { return "" + " static private double p;\n" + " static private void reset_pred() { p = 0.0; }\n" + " static private void add_pred(double x) { p += x; }\n" + " static private double[] finalize_pred() {\n" + " return new double[] {-p, p};\n" + " }\n"; } public double[] toCodeArray() { return new double[] {prediction}; } } --- NEW FILE: RobustBoost.java --- package jboost.booster; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; import jboost.controller.Configuration; import jboost.examples.Label; /** * Java implemantation of RobustBoost. * * @author Sunsern Cheamanunkul */ public class RobustBoost extends AbstractBooster { [...1101 lines suppressed...] public boolean isSucceeded() { return succeeded; } public double getDs() { return ds; } public double getDt() { return dt; } public String getLog() { return log.toString(); } } } /** end of class AdaBoost */ |