| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public void train(final RegressionDataSet dataSet, final ExecutorService threadPool) |
| 358 | { |
| 359 | final PriorityQueue<RegressionModelEvaluation> bestModels = |
| 360 | new PriorityQueue<RegressionModelEvaluation>(folds, |
| 361 | new Comparator<RegressionModelEvaluation>() |
| 362 | { |
| 363 | @Override |
| 364 | public int compare(RegressionModelEvaluation t, RegressionModelEvaluation t1) |
| 365 | { |
| 366 | double v0 = t.getScoreStats(regressionTargetScore).getMean(); |
| 367 | double v1 = t1.getScoreStats(regressionTargetScore).getMean(); |
| 368 | int order = regressionTargetScore.lowerIsBetter() ? 1 : -1; |
| 369 | return order*Double.compare(v0, v1); |
| 370 | } |
| 371 | }); |
| 372 | |
| 373 | /** |
| 374 | * Each model is set to have different combination of parameters. We |
| 375 | * then train each model to determine the best one. |
| 376 | */ |
| 377 | final List<Regressor> paramsToEval = new ArrayList<Regressor>(); |
| 378 | |
| 379 | Random rand = RandomUtil.getRandom(); |
| 380 | for(int trial = 0; trial < trials; trial++) |
| 381 | { |
| 382 | for(int i = 0; i < searchParams.size(); i++) |
| 383 | { |
| 384 | double sampledValue = searchValues.get(i).invCdf(rand.nextDouble()); |
| 385 | |
| 386 | Parameter param = searchParams.get(i); |
| 387 | if(param instanceof DoubleParameter) |
| 388 | ((DoubleParameter)param).setValue(sampledValue); |
| 389 | else if(param instanceof IntParameter) |
| 390 | ((IntParameter)param).setValue((int) Math.round(sampledValue)); |
| 391 | } |
| 392 | |
| 393 | paramsToEval.add(baseRegressor.clone()); |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * This is the Executor used for training the models in parallel. If we |
| 398 | * are not supposed to do that, it will be an executor that executes |
| 399 | * them sequentually. |
| 400 | */ |
| 401 | final ExecutorService modelService; |
| 402 | if(trainModelsInParallel && threadPool != null) |
| 403 | modelService = threadPool; |
| 404 | else |
| 405 | modelService = new FakeExecutor(); |
| 406 | |
| 407 | //if we are doing our CV splits ahead of time, get them done now |
| 408 | final List<RegressionDataSet> preFolded; |
| 409 | |
| 410 | /** |
| 411 | * Pre-combine our training combinations so that any caching can be |
| 412 | * re-used |
| 413 | */ |