(RegressionDataSet dataSet, final ExecutorService threadPool)
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public void train(RegressionDataSet dataSet, final ExecutorService threadPool) |
| 412 | { |
| 413 | learners = new ArrayList(rounds); |
| 414 | //Used to make the main thread wait for the working threads to finish before submiting a new job so we dont waist too much memory then we can use at once |
| 415 | final Semaphore waitForThread = new Semaphore(SystemInfo.LogicalCores); |
| 416 | //Used to make the main thread wait for the working threads to finish before returning |
| 417 | final CountDownLatch waitForFinish = new CountDownLatch(rounds); |
| 418 | |
| 419 | //Creat a synchrnozied view so we can add safely |
| 420 | final List synchronizedLearners = Collections.synchronizedList(learners); |
| 421 | final int[] sampleCount = new int[dataSet.getSampleSize()]; |
| 422 | for(int i = 0; i < rounds; i++) |
| 423 | { |
| 424 | sampleWithReplacement(sampleCount, sampleCount.length+extraSamples, random); |
| 425 | final RegressionDataSet sampleSet = getSampledDataSet(dataSet, sampleCount); |
| 426 | final Regressor learner = baseRegressor.clone(); |
| 427 | if(simultaniousTraining && threadPool != null) |
| 428 | { |
| 429 | try |
| 430 | { |
| 431 | //Wait for an available thread |
| 432 | waitForThread.acquire(); |
| 433 | threadPool.submit(new Runnable() { |
| 434 | |
| 435 | @Override |
| 436 | public void run() |
| 437 | { |
| 438 | learner.train(sampleSet); |
| 439 | synchronizedLearners.add(learner); |
| 440 | waitForThread.release();//Finish, allow another one to pass through |
| 441 | waitForFinish.countDown(); |
| 442 | } |
| 443 | }); |
| 444 | } |
| 445 | catch (InterruptedException ex) |
| 446 | { |
| 447 | Logger.getLogger(Bagging.class.getName()).log(Level.SEVERE, null, ex); |
| 448 | System.err.println(ex.getMessage()); |
| 449 | } |
| 450 | |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | if(threadPool != null) |
| 455 | learner.train(sampleSet, threadPool); |
| 456 | else |
| 457 | learner.train(sampleSet); |
| 458 | learners.add(learner); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (simultaniousTraining && threadPool != null) |
| 463 | try |
| 464 | { |
| 465 | waitForFinish.await(); |
| 466 | } |
| 467 | catch (InterruptedException ex) |
nothing calls this directly
no test coverage detected