use of org.deeplearning4j.optimize.listeners.ScoreIterationListener in project deeplearning4j by deeplearning4j.
the class BackTrackLineSearchTest method testBackTrackLineCG.
@Test
public void testBackTrackLineCG() {
OptimizationAlgorithm optimizer = OptimizationAlgorithm.CONJUGATE_GRADIENT;
DataSet data = irisIter.next();
data.normalizeZeroMeanZeroUnitVariance();
MultiLayerNetwork network = new MultiLayerNetwork(getIrisMultiLayerConfig(Activation.RELU, 5, optimizer));
network.init();
IterationListener listener = new ScoreIterationListener(1);
network.setListeners(Collections.singletonList(listener));
double firstScore = network.score(data);
network.fit(data.getFeatureMatrix(), data.getLabels());
double score = network.score();
assertTrue(score < firstScore);
}
use of org.deeplearning4j.optimize.listeners.ScoreIterationListener in project deeplearning4j by deeplearning4j.
the class BackTrackLineSearchTest method testBackTrackLineGradientDescent.
///////////////////////////////////////////////////////////////////////////
@Test
public void testBackTrackLineGradientDescent() {
OptimizationAlgorithm optimizer = OptimizationAlgorithm.LINE_GRADIENT_DESCENT;
DataSetIterator irisIter = new IrisDataSetIterator(1, 1);
DataSet data = irisIter.next();
MultiLayerNetwork network = new MultiLayerNetwork(getIrisMultiLayerConfig(Activation.SIGMOID, 100, optimizer));
network.init();
IterationListener listener = new ScoreIterationListener(1);
network.setListeners(Collections.singletonList(listener));
double oldScore = network.score(data);
network.fit(data.getFeatureMatrix(), data.getLabels());
double score = network.score();
assertTrue(score < oldScore);
}
use of org.deeplearning4j.optimize.listeners.ScoreIterationListener in project deeplearning4j by deeplearning4j.
the class TestListenerSetting method testSettingListenersUnsupervised.
@Test
public void testSettingListenersUnsupervised() {
//Pretrain layers should get copies of the listeners, in addition to the
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().list().layer(0, new RBM.Builder().nIn(10).nOut(10).build()).layer(1, new AutoEncoder.Builder().nIn(10).nOut(10).build()).layer(2, new VariationalAutoencoder.Builder().nIn(10).nOut(10).build()).build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
net.setListeners(new ScoreIterationListener(), new TestRoutingListener());
for (Layer l : net.getLayers()) {
Collection<IterationListener> layerListeners = l.getListeners();
assertEquals(l.getClass().toString(), 2, layerListeners.size());
IterationListener[] lArr = layerListeners.toArray(new IterationListener[2]);
assertTrue(lArr[0] instanceof ScoreIterationListener);
assertTrue(lArr[1] instanceof TestRoutingListener);
}
Collection<IterationListener> netListeners = net.getListeners();
assertEquals(2, netListeners.size());
IterationListener[] lArr = netListeners.toArray(new IterationListener[2]);
assertTrue(lArr[0] instanceof ScoreIterationListener);
assertTrue(lArr[1] instanceof TestRoutingListener);
ComputationGraphConfiguration gConf = new NeuralNetConfiguration.Builder().graphBuilder().addInputs("in").addLayer("0", new RBM.Builder().nIn(10).nOut(10).build(), "in").addLayer("1", new AutoEncoder.Builder().nIn(10).nOut(10).build(), "0").addLayer("2", new VariationalAutoencoder.Builder().nIn(10).nOut(10).build(), "1").setOutputs("2").build();
ComputationGraph cg = new ComputationGraph(gConf);
cg.init();
cg.setListeners(new ScoreIterationListener(), new TestRoutingListener());
for (Layer l : cg.getLayers()) {
Collection<IterationListener> layerListeners = l.getListeners();
assertEquals(2, layerListeners.size());
lArr = layerListeners.toArray(new IterationListener[2]);
assertTrue(lArr[0] instanceof ScoreIterationListener);
assertTrue(lArr[1] instanceof TestRoutingListener);
}
netListeners = cg.getListeners();
assertEquals(2, netListeners.size());
lArr = netListeners.toArray(new IterationListener[2]);
assertTrue(lArr[0] instanceof ScoreIterationListener);
assertTrue(lArr[1] instanceof TestRoutingListener);
}
use of org.deeplearning4j.optimize.listeners.ScoreIterationListener in project deeplearning4j by deeplearning4j.
the class Dl4jServingRouteTest method testServingRoute.
@Test
public void testServingRoute() throws Exception {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT).iterations(5).seed(123).list().layer(0, new DenseLayer.Builder().nIn(4).nOut(3).weightInit(WeightInit.XAVIER).activation(Activation.TANH).build()).layer(1, new DenseLayer.Builder().nIn(3).nOut(2).weightInit(WeightInit.XAVIER).activation(Activation.TANH).build()).layer(2, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).nIn(2).nOut(3).build()).backprop(true).pretrain(false).build();
MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
network.setListeners(new ScoreIterationListener(1));
network.fit(next);
String outputPath = "networktest.zip";
dir.mkdirs();
File tmp = new File(dir, "tmp.txt");
tmp.createNewFile();
tmp.deleteOnExit();
ModelSerializer.writeModel(network, outputPath, false);
final boolean computationGraph = false;
final String uri = String.format("file://%s?fileName=tmp.txt", dir.getAbsolutePath());
context.addRoutes(DL4jServeRouteBuilder.builder().computationGraph(computationGraph).zooKeeperPort(zookeeper.getPort()).kafkaBroker(kafkaCluster.getBrokerList()).consumingTopic(topicName).modelUri(outputPath).outputUri(uri).finalProcessor(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(exchange.getIn().getBody().toString());
}
}).build());
context.startAllRoutes();
Endpoint endpoint = context.getRoutes().get(1).getConsumer().getEndpoint();
ConsumerTemplate consumerTemplate = context.createConsumerTemplate();
ProducerTemplate producerTemplate = context.createProducerTemplate();
producerTemplate.sendBody("direct:start", "hello");
consumerTemplate.receiveBody(endpoint, 3000, String.class);
String contents = FileUtils.readFileToString(new File(dir, "tmp.txt"));
}
use of org.deeplearning4j.optimize.listeners.ScoreIterationListener in project deeplearning4j by deeplearning4j.
the class TestEarlyStoppingSparkCompGraph method testEarlyStoppingIris.
@Test
public void testEarlyStoppingIris() {
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).updater(Updater.SGD).weightInit(WeightInit.XAVIER).graphBuilder().addInputs("in").addLayer("0", new OutputLayer.Builder().nIn(4).nOut(3).lossFunction(LossFunctions.LossFunction.MCXENT).build(), "in").setOutputs("0").pretrain(false).backprop(true).build();
ComputationGraph net = new ComputationGraph(conf);
net.setListeners(new ScoreIterationListener(1));
JavaRDD<DataSet> irisData = getIris();
EarlyStoppingModelSaver<ComputationGraph> saver = new InMemoryModelSaver<>();
EarlyStoppingConfiguration<ComputationGraph> esConf = new EarlyStoppingConfiguration.Builder<ComputationGraph>().epochTerminationConditions(new MaxEpochsTerminationCondition(5)).iterationTerminationConditions(new MaxTimeIterationTerminationCondition(1, TimeUnit.MINUTES)).scoreCalculator(new SparkLossCalculatorComputationGraph(irisData.map(new DataSetToMultiDataSetFn()), true, sc.sc())).modelSaver(saver).build();
TrainingMaster tm = new ParameterAveragingTrainingMaster(true, numExecutors(), 1, 10, 1, 0);
IEarlyStoppingTrainer<ComputationGraph> trainer = new SparkEarlyStoppingGraphTrainer(getContext().sc(), tm, esConf, net, irisData.map(new DataSetToMultiDataSetFn()));
EarlyStoppingResult<ComputationGraph> result = trainer.fit();
System.out.println(result);
assertEquals(5, result.getTotalEpochs());
assertEquals(EarlyStoppingResult.TerminationReason.EpochTerminationCondition, result.getTerminationReason());
Map<Integer, Double> scoreVsIter = result.getScoreVsEpoch();
assertEquals(5, scoreVsIter.size());
String expDetails = esConf.getEpochTerminationConditions().get(0).toString();
assertEquals(expDetails, result.getTerminationDetails());
ComputationGraph out = result.getBestModel();
assertNotNull(out);
//Check that best score actually matches (returned model vs. manually calculated score)
ComputationGraph bestNetwork = result.getBestModel();
double score = bestNetwork.score(new IrisDataSetIterator(150, 150).next());
double bestModelScore = result.getBestModelScore();
assertEquals(bestModelScore, score, 1e-3);
}
Aggregations