Search in sources :

Example 66 with ScoreIterationListener

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);
}
Also used : OptimizationAlgorithm(org.deeplearning4j.nn.api.OptimizationAlgorithm) DataSet(org.nd4j.linalg.dataset.DataSet) IterationListener(org.deeplearning4j.optimize.api.IterationListener) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) Test(org.junit.Test)

Example 67 with ScoreIterationListener

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);
}
Also used : OptimizationAlgorithm(org.deeplearning4j.nn.api.OptimizationAlgorithm) IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSet(org.nd4j.linalg.dataset.DataSet) IterationListener(org.deeplearning4j.optimize.api.IterationListener) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) Test(org.junit.Test)

Example 68 with ScoreIterationListener

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);
}
Also used : VariationalAutoencoder(org.deeplearning4j.nn.conf.layers.variational.VariationalAutoencoder) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) Layer(org.deeplearning4j.nn.api.Layer) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) RoutingIterationListener(org.deeplearning4j.api.storage.listener.RoutingIterationListener) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) IterationListener(org.deeplearning4j.optimize.api.IterationListener) ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration) RBM(org.deeplearning4j.nn.conf.layers.RBM) ComputationGraph(org.deeplearning4j.nn.graph.ComputationGraph) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) AutoEncoder(org.deeplearning4j.nn.conf.layers.AutoEncoder) Test(org.junit.Test)

Example 69 with ScoreIterationListener

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"));
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) DenseLayer(org.deeplearning4j.nn.conf.layers.DenseLayer) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) File(java.io.File) Test(org.junit.Test)

Example 70 with ScoreIterationListener

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);
}
Also used : OutputLayer(org.deeplearning4j.nn.conf.layers.OutputLayer) IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSet(org.nd4j.linalg.dataset.DataSet) TrainingMaster(org.deeplearning4j.spark.api.TrainingMaster) ParameterAveragingTrainingMaster(org.deeplearning4j.spark.impl.paramavg.ParameterAveragingTrainingMaster) EarlyStoppingConfiguration(org.deeplearning4j.earlystopping.EarlyStoppingConfiguration) DataSetToMultiDataSetFn(org.deeplearning4j.spark.impl.graph.dataset.DataSetToMultiDataSetFn) SparkLossCalculatorComputationGraph(org.deeplearning4j.spark.earlystopping.SparkLossCalculatorComputationGraph) ComputationGraph(org.deeplearning4j.nn.graph.ComputationGraph) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) InMemoryModelSaver(org.deeplearning4j.earlystopping.saver.InMemoryModelSaver) MaxEpochsTerminationCondition(org.deeplearning4j.earlystopping.termination.MaxEpochsTerminationCondition) SparkEarlyStoppingGraphTrainer(org.deeplearning4j.spark.earlystopping.SparkEarlyStoppingGraphTrainer) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ParameterAveragingTrainingMaster(org.deeplearning4j.spark.impl.paramavg.ParameterAveragingTrainingMaster) SparkLossCalculatorComputationGraph(org.deeplearning4j.spark.earlystopping.SparkLossCalculatorComputationGraph) ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration) MaxTimeIterationTerminationCondition(org.deeplearning4j.earlystopping.termination.MaxTimeIterationTerminationCondition) Test(org.junit.Test)

Aggregations

ScoreIterationListener (org.deeplearning4j.optimize.listeners.ScoreIterationListener)76 Test (org.junit.Test)75 DataSetIterator (org.nd4j.linalg.dataset.api.iterator.DataSetIterator)44 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)43 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)41 IrisDataSetIterator (org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator)39 DataSet (org.nd4j.linalg.dataset.DataSet)37 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)35 InMemoryModelSaver (org.deeplearning4j.earlystopping.saver.InMemoryModelSaver)26 MaxEpochsTerminationCondition (org.deeplearning4j.earlystopping.termination.MaxEpochsTerminationCondition)26 INDArray (org.nd4j.linalg.api.ndarray.INDArray)23 MaxTimeIterationTerminationCondition (org.deeplearning4j.earlystopping.termination.MaxTimeIterationTerminationCondition)22 OutputLayer (org.deeplearning4j.nn.conf.layers.OutputLayer)21 ComputationGraphConfiguration (org.deeplearning4j.nn.conf.ComputationGraphConfiguration)17 ComputationGraph (org.deeplearning4j.nn.graph.ComputationGraph)17 IterationListener (org.deeplearning4j.optimize.api.IterationListener)15 MnistDataSetIterator (org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator)13 MaxScoreIterationTerminationCondition (org.deeplearning4j.earlystopping.termination.MaxScoreIterationTerminationCondition)13 IEarlyStoppingTrainer (org.deeplearning4j.earlystopping.trainer.IEarlyStoppingTrainer)13 EarlyStoppingConfiguration (org.deeplearning4j.earlystopping.EarlyStoppingConfiguration)12