| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void trainC(ClassificationDataSet dataSet, ExecutorService threadPool) |
| 231 | { |
| 232 | predicting = dataSet.getPredicting(); |
| 233 | hypWeights = new DoubleList(maxIterations); |
| 234 | hypoths = new ArrayList<Classifier>(maxIterations); |
| 235 | final int N = dataSet.getSampleSize(); |
| 236 | |
| 237 | List<DataPointPair<Integer>> dataPoints = dataSet.getTwiceShallowClone().getAsDPPList(); |
| 238 | //Initialization step, set up the weights so they are all 1 / size of dataset |
| 239 | for(DataPointPair<Integer> dpp : dataPoints) |
| 240 | dpp.getDataPoint().setWeight(1.0/N);//Scaled, they are all 1 |
| 241 | double weightSum = 1; |
| 242 | |
| 243 | |
| 244 | //Keep track of the cumaltive score for everything |
| 245 | double[] H_cur = new double[N]; |
| 246 | double[] curH_Result = new double[N]; |
| 247 | |
| 248 | for(int t = 0; t < maxIterations; t++) |
| 249 | { |
| 250 | Classifier weak = weakLearner.clone(); |
| 251 | if(threadPool != null && !(threadPool instanceof FakeExecutor)) |
| 252 | weak.trainC(new ClassificationDataSet(dataPoints, predicting), threadPool); |
| 253 | else |
| 254 | weak.trainC(new ClassificationDataSet(dataPoints, predicting)); |
| 255 | |
| 256 | double error = 0.0; |
| 257 | for(int i = 0; i < dataPoints.size(); i++) |
| 258 | { |
| 259 | DataPointPair<Integer> dpp = dataPoints.get(i); |
| 260 | double y_hat = H_cur[i] = H(weak, dpp.getDataPoint()); |
| 261 | double y_true = dpp.getPair()*2-1;//{-1 or 1} |
| 262 | error += dpp.getDataPoint().getWeight()*y_hat*y_true; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | if(error < 0) |
| 267 | return; |
| 268 | |
| 269 | double alpha_m = Math.log((1+error)/(1-error))/2; |
| 270 | |
| 271 | weightSum = 0; |
| 272 | |
| 273 | for(int i = 0; i < dataPoints.size(); i++) |
| 274 | { |
| 275 | curH_Result[i] += alpha_m * H_cur[i]; |
| 276 | double f_t = curH_Result[i]; |
| 277 | |
| 278 | DataPointPair<Integer> dpp = dataPoints.get(i); |
| 279 | DataPoint dp = dpp.getDataPoint(); |
| 280 | double y_true = dpp.getPair()*2-1; |
| 281 | |
| 282 | double w_i = Math.exp(lambda*Math.pow(f_t-y_true, 2) - (1-lambda)*f_t*f_t); |
| 283 | if(Double.isInfinite(w_i)) |
| 284 | w_i = 50;//Let it grow back isntead of bizaro huge values |
| 285 | weightSum += w_i; |
| 286 | dp.setWeight(w_i); |