Fix and optimize how progress is displayed while training trees

This commit is contained in:
Joel Therrien 2019-04-05 11:22:59 -07:00
parent ea176cff9a
commit 50b4a3cd89

View file

@ -71,20 +71,19 @@ public class ForestTrainer<Y, TO, FO> {
final Random random = new Random(); final Random random = new Random();
for(int j=0; j<ntree; j++){ for(int j=0; j<ntree; j++){
if(displayProgress){
System.out.print("\rFinished tree " + j + "/" + ntree + " trees");
}
trees.add(trainTree(bootstrapper, random)); trees.add(trainTree(bootstrapper, random));
if(displayProgress){
if(j==0) {
System.out.println();
}
System.out.print("\rFinished tree " + (j+1) + "/" + ntree);
if(j==ntree-1){
System.out.println();
}
}
} }
if(displayProgress){
System.out.println("\rFinished tree " + ntree + "/" + ntree + " trees");
System.out.println("Finished");
}
return Forest.<TO, FO>builder() return Forest.<TO, FO>builder()
.treeResponseCombiner(treeResponseCombiner) .treeResponseCombiner(treeResponseCombiner)
.trees(trees) .trees(trees)
@ -117,7 +116,8 @@ public class ForestTrainer<Y, TO, FO> {
} }
if(displayProgress){ if(displayProgress){
System.out.println("\nFinished"); System.out.println("\rFinished tree " + ntree + "/" + ntree + " trees");
System.out.println("Finished");
} }
} }
@ -136,10 +136,12 @@ public class ForestTrainer<Y, TO, FO> {
} }
executorService.shutdown(); executorService.shutdown();
int prevNumberTreesSet = -1;
while(!executorService.isTerminated()){ while(!executorService.isTerminated()){
try{ try{
Thread.sleep(100); Thread.sleep(500);
} catch (InterruptedException e) { } catch (InterruptedException e) {
// do nothing; who cares? // do nothing; who cares?
} }
@ -152,7 +154,13 @@ public class ForestTrainer<Y, TO, FO> {
} }
} }
System.out.print("\rFinished " + numberTreesSet + "/" + ntree + " trees"); // Only output trees set on screen if there was a change
// In some environments where standard output is streamed to a file this method below causes frequent writes to output
if(numberTreesSet != prevNumberTreesSet){
System.out.print("\rFinished " + numberTreesSet + "/" + ntree + " trees");
prevNumberTreesSet = numberTreesSet;
}
} }
} }
@ -188,6 +196,8 @@ public class ForestTrainer<Y, TO, FO> {
} }
executorService.shutdown(); executorService.shutdown();
int prevNumberTreesSet = -1;
while(!executorService.isTerminated()){ while(!executorService.isTerminated()){
try{ try{
@ -197,8 +207,15 @@ public class ForestTrainer<Y, TO, FO> {
} }
if(displayProgress) { if(displayProgress) {
int numberTreesSet = treeCount.get();
// Only output trees set on screen if there was a change
// In some environments where standard output is streamed to a file this method below causes frequent writes to output
if(numberTreesSet != prevNumberTreesSet){
System.out.print("\rFinished " + numberTreesSet + "/" + ntree + " trees");
prevNumberTreesSet = numberTreesSet;
}
System.out.print("\rFinished " + treeCount.get() + "/" + ntree + " trees");
} }
} }