org.deeplearning4j.arbiter.task.MultiLayerNetworkTaskCreator Maven / Gradle / Ivy
/*
*
* * Copyright 2016 Skymind,Inc.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package org.deeplearning4j.arbiter.task;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.deeplearning4j.arbiter.DL4JConfiguration;
import org.deeplearning4j.arbiter.listener.BaseUIStatusReportingListener;
import org.deeplearning4j.arbiter.listener.UIStatusReportingListener;
import org.deeplearning4j.arbiter.optimize.api.Candidate;
import org.deeplearning4j.arbiter.optimize.api.OptimizationResult;
import org.deeplearning4j.arbiter.optimize.api.TaskCreator;
import org.deeplearning4j.arbiter.optimize.api.data.DataProvider;
import org.deeplearning4j.arbiter.optimize.api.evaluation.ModelEvaluator;
import org.deeplearning4j.arbiter.optimize.api.score.ScoreFunction;
import org.deeplearning4j.arbiter.optimize.runner.Status;
import org.deeplearning4j.arbiter.optimize.runner.listener.candidate.UICandidateStatusListener;
import org.deeplearning4j.datasets.iterator.DataSetIterator;
import org.deeplearning4j.earlystopping.EarlyStoppingConfiguration;
import org.deeplearning4j.earlystopping.EarlyStoppingResult;
import org.deeplearning4j.earlystopping.trainer.EarlyStoppingTrainer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.ui.components.text.ComponentText;
import java.util.concurrent.Callable;
@AllArgsConstructor @NoArgsConstructor
public class MultiLayerNetworkTaskCreator implements TaskCreator{
private ModelEvaluator modelEvaluator;
@Override
public Callable> create(
Candidate candidate, DataProvider dataProvider,
ScoreFunction scoreFunction,
UICandidateStatusListener statusListener) {
return new DL4JLearningTask<>(candidate,dataProvider,scoreFunction,modelEvaluator,statusListener);
}
private static class DL4JLearningTask implements Callable> {
private Candidate candidate;
private DataProvider dataProvider;
private ScoreFunction scoreFunction;
private ModelEvaluator modelEvaluator;
private BaseUIStatusReportingListener dl4jListener;
public DL4JLearningTask(Candidate candidate, DataProvider dataProvider, ScoreFunction scoreFunction, ModelEvaluator modelEvaluator, UICandidateStatusListener listener) {
this.candidate = candidate;
this.dataProvider = dataProvider;
this.scoreFunction = scoreFunction;
this.modelEvaluator = modelEvaluator;
dl4jListener = new UIStatusReportingListener(listener);
}
@Override
public OptimizationResult call() throws Exception {
//Create network
MultiLayerNetwork net = new MultiLayerNetwork(candidate.getValue().getMultiLayerConfiguration());
net.init();
net.setListeners(dl4jListener);
//Early stopping or fixed number of epochs:
DataSetIterator dataSetIterator = dataProvider.testData(candidate.getDataParameters());
EarlyStoppingConfiguration esConfig = candidate.getValue().getEarlyStoppingConfiguration();
EarlyStoppingResult esResult = null;
if(esConfig != null){
EarlyStoppingTrainer trainer = new EarlyStoppingTrainer(esConfig,net,dataSetIterator,dl4jListener);
try{
esResult = trainer.fit();
net = esResult.getBestModel(); //Can return null if failed OR if
} catch(Exception e){
dl4jListener.postReport(Status.Failed, null,
new ComponentText("Unexpected exception during model training\n", null),
new ComponentText(ExceptionUtils.getStackTrace(e), null));
throw e;
}
switch(esResult.getTerminationReason()){
case Error:
dl4jListener.postReport(Status.Failed, esResult);
break;
case IterationTerminationCondition:
case EpochTerminationCondition:
dl4jListener.postReport(Status.Complete, esResult);
break;
}
} else {
//Fixed number of epochs
int nEpochs = candidate.getValue().getNumEpochs();
for( int i=0; i(candidate, net, score, candidate.getIndex(), additionalEvaluation);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy