1- /** *****************************************************************************
1+ /* *****************************************************************************
22 * Copyright (c) 2015-2019 Skymind, Inc.
33 *
44 * This program and the accompanying materials are made available under the
2020import org .apache .commons .math3 .random .RandomGenerator ;
2121import org .apache .commons .math3 .random .SynchronizedRandomGenerator ;
2222import org .deeplearning4j .arbiter .ComputationGraphSpace ;
23- import org .deeplearning4j .arbiter .optimize .api .OptimizationResult ;
24- import org .deeplearning4j .arbiter .optimize .api .saving .ResultReference ;
2523import org .deeplearning4j .arbiter .optimize .generator .GeneticSearchCandidateGenerator ;
26- import org .deeplearning4j .arbiter .optimize .generator .genetic .Chromosome ;
2724import org .deeplearning4j .arbiter .optimize .generator .genetic .crossover .CrossoverOperator ;
2825import org .deeplearning4j .arbiter .optimize .generator .genetic .crossover .KPointCrossover ;
2926import org .deeplearning4j .arbiter .optimize .generator .genetic .crossover .parentselection .TwoParentSelection ;
3027import org .deeplearning4j .arbiter .optimize .generator .genetic .culling .CullOperator ;
3128import org .deeplearning4j .arbiter .optimize .generator .genetic .culling .LeastFitCullOperator ;
32- import org .deeplearning4j .arbiter .optimize .generator .genetic .population .PopulationListener ;
3329import org .deeplearning4j .arbiter .optimize .generator .genetic .population .PopulationModel ;
3430import org .deeplearning4j .arbiter .optimize .generator .genetic .selection .GeneticSelectionOperator ;
3531import org .deeplearning4j .arbiter .optimize .generator .genetic .selection .SelectionOperator ;
36- import org .deeplearning4j .arbiter .optimize .runner .IOptimizationRunner ;
3732import org .deeplearning4j .arbiter .scoring .impl .EvaluationScoreFunction ;
38- import org .deeplearning4j .eval .Evaluation ;
39- import org .deeplearning4j .nn .graph .ComputationGraph ;
33+ import org .nd4j .evaluation .classification .Evaluation .Metric ;
4034
41- import java . util . List ;
35+ import static org . deeplearning4j . examples . arbiter . genetic . BaseGeneticHyperparameterOptimizationExample . run ;
4236
4337/**
4438 * In this hyperparameter optimization example, we change the default behavior of the genetic candidate generator.
@@ -55,7 +49,7 @@ public static void main(String[] args) throws Exception {
5549
5650 ComputationGraphSpace cgs = GeneticSearchExampleConfiguration .GetGraphConfiguration ();
5751
58- EvaluationScoreFunction scoreFunction = new EvaluationScoreFunction (Evaluation . Metric .F1 );
52+ EvaluationScoreFunction scoreFunction = new EvaluationScoreFunction (Metric .F1 );
5953
6054 // The ExampleCullOperator extends the default cull operator (least fit) to include an artificial predator.
6155 CullOperator cullOperator = new ExampleCullOperator ();
@@ -85,30 +79,8 @@ public static void main(String[] args) throws Exception {
8579 .build ();
8680
8781 // Let's have a listener to print the population size after each evaluation.
88- populationModel .addListener (new ExamplePopulationListener ());
89-
90- IOptimizationRunner runner = GeneticSearchExampleConfiguration .BuildRunner (candidateGenerator , scoreFunction );
91-
92- //Start the hyperparameter optimization
93- runner .execute ();
94-
95- //Print out some basic stats regarding the optimization procedure
96- String s = "Best score: " + runner .bestScore () + "\n " +
97- "Index of model with best score: " + runner .bestScoreCandidateIndex () + "\n " +
98- "Number of configurations evaluated: " + runner .numCandidatesCompleted () + "\n " ;
99- System .out .println (s );
100-
101-
102- //Get all results, and print out details of the best result:
103- int indexOfBestResult = runner .bestScoreCandidateIndex ();
104- List <ResultReference > allResults = runner .getResults ();
105-
106- OptimizationResult bestResult = allResults .get (indexOfBestResult ).getResult ();
107- ComputationGraph bestModel = (ComputationGraph ) bestResult .getResultReference ().getResultModel ();
108-
109- System .out .println ("\n \n Configuration of best model:\n " );
110- System .out .println (bestModel .getConfiguration ().toJson ());
111-
82+ populationModel .addListener (new BaseGeneticHyperparameterOptimizationExample .ExamplePopulationListener ());
83+ run (populationModel , candidateGenerator , scoreFunction );
11284 }
11385
11486 // This is an example of a custom behavior for the genetic algorithm. We force one of the parent to be one of the
@@ -158,17 +130,4 @@ public void cullPopulation() {
158130 System .out .println (String .format ("Randomly removed %1$s candidate(s)." , preyCount ));
159131 }
160132 }
161-
162- public static class ExamplePopulationListener implements PopulationListener {
163-
164- @ Override
165- public void onChanged (List <Chromosome > population ) {
166- double best = population .get (0 ).getFitness ();
167- double average = population .stream ()
168- .mapToDouble (Chromosome ::getFitness )
169- .average ()
170- .getAsDouble ();
171- System .out .println (String .format ("\n Population size is %1$s, best score is %2$s, average score is %3$s" , population .size (), best , average ));
172- }
173- }
174133}
0 commit comments