org.deeplearning4j.arbiter.optimize.runner.IOptimizationRunner Java Examples

The following examples show how to use org.deeplearning4j.arbiter.optimize.runner.IOptimizationRunner. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestGeneticSearch.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void GeneticSearchCandidateGenerator_getCandidate_GeneticExceptionShouldMarkCandidateAsFailed() {

    ScoreFunction scoreFunction = new BraninFunction.BraninScoreFunction();

    //Define configuration:
    CandidateGenerator candidateGenerator =
                    new GeneticSearchCandidateGenerator.Builder(new BraninFunction.BraninSpace(), scoreFunction)
                                    .selectionOperator(new TestSelectionOperator()).build();

    TestTerminationCondition testTerminationCondition = new TestTerminationCondition();

    OptimizationConfiguration configuration = new OptimizationConfiguration.Builder()
                    .candidateGenerator(candidateGenerator).scoreFunction(scoreFunction)
                    .terminationConditions(testTerminationCondition).build();

    IOptimizationRunner runner = new LocalOptimizationRunner(configuration, new BraninFunction.BraninTaskCreator());

    runner.addListeners(new LoggingStatusListener());
    runner.execute();

    Assert.assertTrue(testTerminationCondition.hasAFailedCandidate);
}
 
Example #2
Source File: TestGeneticSearch.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void GeneticSearchCandidateGenerator_getCandidate_ShouldGenerateCandidates() throws Exception {

    ScoreFunction scoreFunction = new BraninFunction.BraninScoreFunction();

    //Define configuration:
    CandidateGenerator candidateGenerator =
                    new GeneticSearchCandidateGenerator.Builder(new BraninFunction.BraninSpace(), scoreFunction)
                                    .build();

    TestTerminationCondition testTerminationCondition = new TestTerminationCondition();
    OptimizationConfiguration configuration = new OptimizationConfiguration.Builder()
                    .candidateGenerator(candidateGenerator).scoreFunction(scoreFunction)
                    .terminationConditions(new MaxCandidatesCondition(50), testTerminationCondition).build();

    IOptimizationRunner runner = new LocalOptimizationRunner(configuration, new BraninFunction.BraninTaskCreator());

    runner.addListeners(new LoggingStatusListener());
    runner.execute();

    Assert.assertFalse(testTerminationCondition.hasAFailedCandidate);
}
 
Example #3
Source File: TestGeneticSearch.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public boolean terminate(IOptimizationRunner optimizationRunner) {
    if (++evalCount == 50) {
        // Generator did not handle GeneticGenerationException
        return true;
    }

    for (CandidateInfo candidateInfo : optimizationRunner.getCandidateStatus()) {
        if (candidateInfo.getCandidateStatus() == CandidateStatus.Failed) {
            hasAFailedCandidate = true;
            return true;
        }
    }

    return false;
}
 
Example #4
Source File: TestErrors.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000L)
public void testAllInvalidDataConfigMismatchCG() throws Exception {
    //Valid config - but mismatched with provided data

    File f = temp.newFolder();
    ComputationGraphSpace mls = new ComputationGraphSpace.Builder()
            .addInputs("in")
            .layer("0", new DenseLayerSpace.Builder().nIn(4).nOut(10)
                    .activation(Activation.TANH).build(), "in")
            .addLayer("1", new OutputLayerSpace.Builder().nIn(10).nOut(3).activation(Activation.SOFTMAX)
                    .lossFunction(LossFunctions.LossFunction.MCXENT).build(), "0")
            .setOutputs("1")
            .build();

    CandidateGenerator candidateGenerator = new RandomSearchGenerator(mls);

    OptimizationConfiguration configuration = new OptimizationConfiguration.Builder()
            .candidateGenerator(candidateGenerator).dataProvider(new TestDataProviderMnist(32, 3))
            .modelSaver(new FileModelSaver(f)).scoreFunction(new TestSetLossScoreFunction(true))
            .terminationConditions(
                    new MaxCandidatesCondition(5))
            .build();

    IOptimizationRunner runner = new LocalOptimizationRunner(configuration, new MultiLayerNetworkTaskCreator());
    runner.execute();
}
 
Example #5
Source File: TestErrors.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000L)
public void testAllInvalidConfigCG() throws Exception {
    //Invalid config - basically check that this actually terminates

    File f = temp.newFolder();
    ComputationGraphSpace mls = new ComputationGraphSpace.Builder()
            .addInputs("in")
            .layer("0", new DenseLayerSpace.Builder().nIn(4).nOut(new FixedValue<>(0))    //INVALID: nOut of 0
                    .activation(Activation.TANH)
                    .build(), "in")
            .layer("1", new OutputLayerSpace.Builder().nOut(3).activation(Activation.SOFTMAX)
                    .lossFunction(LossFunctions.LossFunction.MCXENT).build(), "0")
            .setOutputs("1")
            .build();

    CandidateGenerator candidateGenerator = new RandomSearchGenerator(mls);

    OptimizationConfiguration configuration = new OptimizationConfiguration.Builder()
            .candidateGenerator(candidateGenerator).dataProvider(new TestDataProviderMnist(32, 3))
            .modelSaver(new FileModelSaver(f)).scoreFunction(new TestSetLossScoreFunction(true))
            .terminationConditions(new MaxCandidatesCondition(5))
            .build();

    IOptimizationRunner runner = new LocalOptimizationRunner(configuration);
    runner.execute();
}
 
Example #6
Source File: TestErrors.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000L)
public void testAllInvalidDataConfigMismatch() throws Exception {
    //Valid config - but mismatched with provided data

    File f = temp.newFolder();
    MultiLayerSpace mls = new MultiLayerSpace.Builder()
            .addLayer(new DenseLayerSpace.Builder().nIn(4).nOut(10)    //INVALID: nOut of 0
                    .activation(Activation.TANH)
                    .build())
            .addLayer(new OutputLayerSpace.Builder().nIn(10).nOut(3).activation(Activation.SOFTMAX)
                    .lossFunction(LossFunctions.LossFunction.MCXENT).build())
            .build();

    CandidateGenerator candidateGenerator = new RandomSearchGenerator(mls);

    OptimizationConfiguration configuration = new OptimizationConfiguration.Builder()
            .candidateGenerator(candidateGenerator).dataProvider(new TestDataProviderMnist(32, 3))
            .modelSaver(new FileModelSaver(f)).scoreFunction(new TestSetLossScoreFunction(true))
            .terminationConditions(
                    new MaxCandidatesCondition(5))
            .build();

    IOptimizationRunner runner = new LocalOptimizationRunner(configuration);
    runner.execute();
}
 
Example #7
Source File: TestErrors.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000L)
public void testAllInvalidConfig() throws Exception {
    //Invalid config - basically check that this actually terminates

    File f = temp.newFolder();
    MultiLayerSpace mls = new MultiLayerSpace.Builder()
            .addLayer(new DenseLayerSpace.Builder().nIn(4).nOut(new FixedValue<>(0))    //INVALID: nOut of 0
                            .activation(Activation.TANH)
                            .build())
            .addLayer(new OutputLayerSpace.Builder().nOut(3).activation(Activation.SOFTMAX)
                    .lossFunction(LossFunctions.LossFunction.MCXENT).build())
            .build();

    CandidateGenerator candidateGenerator = new RandomSearchGenerator(mls);

    OptimizationConfiguration configuration = new OptimizationConfiguration.Builder()
            .candidateGenerator(candidateGenerator).dataProvider(new TestDataProviderMnist(32, 3))
            .modelSaver(new FileModelSaver(f)).scoreFunction(new TestSetLossScoreFunction(true))
            .terminationConditions(
                    new MaxCandidatesCondition(5))
            .build();

    IOptimizationRunner runner = new LocalOptimizationRunner(configuration);
    runner.execute();
}
 
Example #8
Source File: ArbiterStatusListener.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private GlobalConfigPersistable getNewStatusPersistable(IOptimizationRunner r){
//        if(ocJson == null || this.startTime == 0L){
//            //Want to update config once start time has been set
//            ocJson = JsonMapper.asJson(r.getConfiguration());
//            this.startTime = r.getConfiguration().getExecutionStartTime();
//        }
        //TODO: cache global config, but we don't want to have outdated info (like uninitialized termination conditions)

        try {
            ocJson = JsonMapper.getMapper().writeValueAsString(r.getConfiguration());
        } catch (IOException e){
            throw new RuntimeException(e);
        }

        GlobalConfigPersistable p = new GlobalConfigPersistable.Builder()
                .sessionId(sessionId)
                .timestamp(System.currentTimeMillis())
                .optimizationConfigJson(ocJson)
                .candidateCounts(r.numCandidatesQueued(), r.numCandidatesCompleted(),
                        r.numCandidatesFailed(), r.numCandidatesTotal())
                .optimizationRunner(r.getClass().getSimpleName())
                .build();

        return p;
    }
 
Example #9
Source File: TestRandomSearch.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void test() throws Exception {
        Map<String, Object> commands = new HashMap<>();
        commands.put(DataSetIteratorFactoryProvider.FACTORY_KEY, new HashMap<>());

        //Define configuration:
        CandidateGenerator candidateGenerator = new RandomSearchGenerator(new BraninFunction.BraninSpace(), commands);
        OptimizationConfiguration configuration = new OptimizationConfiguration.Builder()
                        .candidateGenerator(candidateGenerator).scoreFunction(new BraninFunction.BraninScoreFunction())
                        .terminationConditions(new MaxCandidatesCondition(50)).build();

        IOptimizationRunner runner = new LocalOptimizationRunner(configuration, new BraninFunction.BraninTaskCreator());

        runner.addListeners(new LoggingStatusListener());
        runner.execute();


//        System.out.println("----- Complete -----");
    }
 
Example #10
Source File: ComputationGraphTaskCreator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public GraphLearningTask(Candidate candidate, DataProvider dataProvider, ScoreFunction scoreFunction,
                         ModelEvaluator modelEvaluator, List<StatusListener> listeners,
                         TaskListener taskListener, IOptimizationRunner runner) {
    this.candidate = candidate;
    this.dataProvider = dataProvider;
    this.scoreFunction = scoreFunction;
    this.modelEvaluator = modelEvaluator;
    this.listeners = listeners;
    this.taskListener = taskListener;
    this.runner = runner;
}
 
Example #11
Source File: ComputationGraphTaskCreator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public GraphLearningTask(Candidate candidate, Class<? extends DataSource> dataSource, Properties dataSourceProperties,
                         ScoreFunction scoreFunction, ModelEvaluator modelEvaluator, List<StatusListener> listeners,
                         TaskListener taskListener, IOptimizationRunner runner) {
    this.candidate = candidate;
    this.dataSource = dataSource;
    this.dataSourceProperties = dataSourceProperties;
    this.scoreFunction = scoreFunction;
    this.modelEvaluator = modelEvaluator;
    this.listeners = listeners;
    this.taskListener = taskListener;
    this.runner = runner;
}
 
Example #12
Source File: ComputationGraphTaskCreator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public Callable<OptimizationResult> create(Candidate candidate, DataProvider dataProvider,
                                           ScoreFunction scoreFunction, List<StatusListener> statusListener,
                                           IOptimizationRunner runner) {

    return new GraphLearningTask(candidate, dataProvider, scoreFunction, modelEvaluator, statusListener,
            taskListener, runner);
}
 
Example #13
Source File: MultiLayerNetworkTaskCreator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public DL4JLearningTask(Candidate candidate, Class<? extends DataSource> dataSource, Properties dataSourceProperties,
                        ScoreFunction scoreFunction, ModelEvaluator modelEvaluator, List<StatusListener> listeners, TaskListener taskListener,
                        IOptimizationRunner runner) {
    this.candidate = candidate;
    this.dataSource = dataSource;
    this.dataSourceProperties = dataSourceProperties;
    this.scoreFunction = scoreFunction;
    this.modelEvaluator = modelEvaluator;
    this.listeners = listeners;
    this.taskListener = taskListener;
    this.runner = runner;
}
 
Example #14
Source File: MultiLayerNetworkTaskCreator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public Callable<OptimizationResult> create(Candidate candidate, DataProvider dataProvider,
                                           ScoreFunction scoreFunction, List<StatusListener> statusListeners,
                                           IOptimizationRunner runner) {

    return new DL4JLearningTask(candidate, dataProvider, scoreFunction, modelEvaluator, statusListeners, taskListener, runner);
}
 
Example #15
Source File: ArbiterStatusListener.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void onCandidateStatusChange(CandidateInfo candidateInfo, IOptimizationRunner runner, OptimizationResult result) {
    ModelInfoPersistable p = lastModelInfoPersistable.get(candidateInfo.getIndex());
    if(p == null){
        p = new ModelInfoPersistable.Builder()
                .timestamp(candidateInfo.getCreatedTime())
                .sessionId(sessionId)
                .workerId(String.valueOf(candidateInfo.getIndex()))
                .modelIdx(candidateInfo.getIndex())
                .score(candidateInfo.getScore())
                .status(candidateInfo.getCandidateStatus())
                .exceptionStackTrace(candidateInfo.getExceptionStackTrace())
                .build();

        lastModelInfoPersistable.put(candidateInfo.getIndex(), p);
    }

    if(p.getScore() == null){
        p.setScore(candidateInfo.getScore());
    }

    if(result != null && p.getExceptionStackTrace() == null && result.getCandidateInfo().getExceptionStackTrace() != null){
        //Update exceptions that may have occurred since earlier model info instance
        p.setExceptionStackTrace(result.getCandidateInfo().getExceptionStackTrace());
    }

    p.setStatus(candidateInfo.getCandidateStatus());

    statsStorage.putUpdate(p);
}
 
Example #16
Source File: BraninFunction.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
        public Callable<OptimizationResult> create(final Candidate c, DataProvider dataProvider,
                        final ScoreFunction scoreFunction, final List<StatusListener> statusListeners,
                        IOptimizationRunner runner) {

            return new Callable<OptimizationResult>() {
                @Override
                public OptimizationResult call() throws Exception {

                    BraninConfig candidate = (BraninConfig) c.getValue();

                    double score = scoreFunction.score(candidate, null, (Map) null);
//                    System.out.println(candidate.getX1() + "\t" + candidate.getX2() + "\t" + score);

                    Thread.sleep(20);

                    if (statusListeners != null) {
                        for (StatusListener sl : statusListeners) {
                            sl.onCandidateIteration(null, null, 0);
                        }
                    }

                    CandidateInfo ci = new CandidateInfo(-1, CandidateStatus.Complete, score,
                                    System.currentTimeMillis(), null, null, null, null);

                    return new OptimizationResult(c, score, c.getIndex(), null, ci, null);
                }
            };
        }
 
Example #17
Source File: MultiLayerNetworkTaskCreator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public DL4JLearningTask(Candidate candidate, DataProvider dataProvider, ScoreFunction scoreFunction,
                ModelEvaluator modelEvaluator, List<StatusListener> listeners, TaskListener taskListener,
                        IOptimizationRunner runner) {
    this.candidate = candidate;
    this.dataProvider = dataProvider;
    this.scoreFunction = scoreFunction;
    this.modelEvaluator = modelEvaluator;
    this.listeners = listeners;
    this.taskListener = taskListener;
    this.runner = runner;
}
 
Example #18
Source File: BraninFunction.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public Callable<OptimizationResult> create(Candidate candidate, Class<? extends DataSource> dataSource,
                Properties dataSourceProperties, ScoreFunction scoreFunction,
                List<StatusListener> statusListeners, IOptimizationRunner runner) {
    throw new UnsupportedOperationException();
}
 
Example #19
Source File: MaxTimeCondition.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(IOptimizationRunner optimizationRunner) {
    startTime = System.currentTimeMillis();
    this.endTime = startTime + timeUnit.toMillis(duration);
}
 
Example #20
Source File: MaxTimeCondition.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public boolean terminate(IOptimizationRunner optimizationRunner) {
    return System.currentTimeMillis() >= endTime;
}
 
Example #21
Source File: MaxCandidatesCondition.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(IOptimizationRunner optimizationRunner) {
    //No op
}
 
Example #22
Source File: MaxCandidatesCondition.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public boolean terminate(IOptimizationRunner optimizationRunner) {
    return optimizationRunner.numCandidatesCompleted() + optimizationRunner.numCandidatesFailed() >= maxCandidates;
}
 
Example #23
Source File: StatusListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/** Called when optimization runner starts execution */
void onInitialization(IOptimizationRunner runner);
 
Example #24
Source File: StatusListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/** Called when optimization runner terminates */
void onShutdown(IOptimizationRunner runner);
 
Example #25
Source File: BaseStatusListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void onInitialization(IOptimizationRunner runner) {
    //No op
}
 
Example #26
Source File: BaseStatusListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void onShutdown(IOptimizationRunner runner) {
    //No op
}
 
Example #27
Source File: BaseStatusListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void onRunnerStatusChange(IOptimizationRunner runner) {
    //No op
}
 
Example #28
Source File: TestGeneticSearch.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(IOptimizationRunner optimizationRunner) {}
 
Example #29
Source File: BaseStatusListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void onCandidateStatusChange(CandidateInfo candidateInfo, IOptimizationRunner runner, OptimizationResult result) {
    //No op
}
 
Example #30
Source File: LoggingStatusListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void onInitialization(IOptimizationRunner runner) {
    log.info("Optimization runner: initialized");
}