Search in sources :

Example 11 with IterationListener

use of org.deeplearning4j.optimize.api.IterationListener in project deeplearning4j by deeplearning4j.

the class AutoEncoderTest method testAutoEncoder.

@Test
public void testAutoEncoder() throws Exception {
    MnistDataFetcher fetcher = new MnistDataFetcher(true);
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().momentum(0.9f).optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT).iterations(1).learningRate(1e-1f).layer(new org.deeplearning4j.nn.conf.layers.AutoEncoder.Builder().nIn(784).nOut(600).corruptionLevel(0.6).lossFunction(LossFunctions.LossFunction.RECONSTRUCTION_CROSSENTROPY).build()).build();
    fetcher.fetch(100);
    DataSet d2 = fetcher.next();
    INDArray input = d2.getFeatureMatrix();
    int numParams = conf.getLayer().initializer().numParams(conf);
    INDArray params = Nd4j.create(1, numParams);
    AutoEncoder da = (AutoEncoder) conf.getLayer().instantiate(conf, Arrays.<IterationListener>asList(new ScoreIterationListener(1)), 0, params, true);
    assertEquals(da.params(), da.params());
    assertEquals(471784, da.params().length());
    da.setParams(da.params());
    da.fit(input);
}
Also used : MnistDataFetcher(org.deeplearning4j.datasets.fetchers.MnistDataFetcher) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) IterationListener(org.deeplearning4j.optimize.api.IterationListener) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) Test(org.junit.Test)

Example 12 with IterationListener

use of org.deeplearning4j.optimize.api.IterationListener in project deeplearning4j by deeplearning4j.

the class OutputLayerTest method testIris2.

@Test
public void testIris2() {
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(10).learningRate(1e-1).layer(new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder().nIn(4).nOut(3).weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).lossFunction(LossFunctions.LossFunction.MCXENT).build()).build();
    int numParams = conf.getLayer().initializer().numParams(conf);
    INDArray params = Nd4j.create(1, numParams);
    OutputLayer l = (OutputLayer) conf.getLayer().instantiate(conf, Collections.<IterationListener>singletonList(new ScoreIterationListener(1)), 0, params, true);
    l.setBackpropGradientsViewArray(Nd4j.create(1, params.length()));
    DataSetIterator iter = new IrisDataSetIterator(150, 150);
    DataSet next = iter.next();
    next.shuffle();
    SplitTestAndTrain trainTest = next.splitTestAndTrain(110);
    trainTest.getTrain().normalizeZeroMeanZeroUnitVariance();
    l.fit(trainTest.getTrain());
    DataSet test = trainTest.getTest();
    test.normalizeZeroMeanZeroUnitVariance();
    Evaluation eval = new Evaluation();
    INDArray output = l.output(test.getFeatureMatrix());
    eval.eval(test.getLabels(), output);
    log.info("Score " + eval.stats());
}
Also used : RnnOutputLayer(org.deeplearning4j.nn.layers.recurrent.RnnOutputLayer) Evaluation(org.deeplearning4j.eval.Evaluation) IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSet(org.nd4j.linalg.dataset.DataSet) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) IterationListener(org.deeplearning4j.optimize.api.IterationListener) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) SplitTestAndTrain(org.nd4j.linalg.dataset.SplitTestAndTrain) Test(org.junit.Test)

Example 13 with IterationListener

use of org.deeplearning4j.optimize.api.IterationListener in project deeplearning4j by deeplearning4j.

the class OutputLayerTest method testIris.

@Test
public void testIris() {
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT).iterations(5).learningRate(1e-1).layer(new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder().nIn(4).nOut(3).weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).lossFunction(LossFunctions.LossFunction.MCXENT).build()).build();
    int numParams = conf.getLayer().initializer().numParams(conf);
    INDArray params = Nd4j.create(1, numParams);
    OutputLayer l = (OutputLayer) conf.getLayer().instantiate(conf, Collections.<IterationListener>singletonList(new ScoreIterationListener(1)), 0, params, true);
    l.setBackpropGradientsViewArray(Nd4j.create(1, params.length()));
    DataSetIterator iter = new IrisDataSetIterator(150, 150);
    DataSet next = iter.next();
    next.shuffle();
    SplitTestAndTrain trainTest = next.splitTestAndTrain(110);
    trainTest.getTrain().normalizeZeroMeanZeroUnitVariance();
    l.fit(trainTest.getTrain());
    DataSet test = trainTest.getTest();
    test.normalizeZeroMeanZeroUnitVariance();
    Evaluation eval = new Evaluation();
    INDArray output = l.output(test.getFeatureMatrix());
    eval.eval(test.getLabels(), output);
    log.info("Score " + eval.stats());
}
Also used : RnnOutputLayer(org.deeplearning4j.nn.layers.recurrent.RnnOutputLayer) Evaluation(org.deeplearning4j.eval.Evaluation) IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSet(org.nd4j.linalg.dataset.DataSet) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) IterationListener(org.deeplearning4j.optimize.api.IterationListener) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) SplitTestAndTrain(org.nd4j.linalg.dataset.SplitTestAndTrain) Test(org.junit.Test)

Example 14 with IterationListener

use of org.deeplearning4j.optimize.api.IterationListener 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 15 with IterationListener

use of org.deeplearning4j.optimize.api.IterationListener 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)

Aggregations

IterationListener (org.deeplearning4j.optimize.api.IterationListener)24 ScoreIterationListener (org.deeplearning4j.optimize.listeners.ScoreIterationListener)15 Test (org.junit.Test)15 DataSet (org.nd4j.linalg.dataset.DataSet)12 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)11 INDArray (org.nd4j.linalg.api.ndarray.INDArray)9 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)7 IrisDataSetIterator (org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator)6 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)5 DataSetIterator (org.nd4j.linalg.dataset.api.iterator.DataSetIterator)5 RoutingIterationListener (org.deeplearning4j.api.storage.listener.RoutingIterationListener)4 Evaluation (org.deeplearning4j.eval.Evaluation)4 OptimizationAlgorithm (org.deeplearning4j.nn.api.OptimizationAlgorithm)4 Layer (org.deeplearning4j.nn.api.Layer)3 RnnOutputLayer (org.deeplearning4j.nn.layers.recurrent.RnnOutputLayer)3 SplitTestAndTrain (org.nd4j.linalg.dataset.SplitTestAndTrain)3 Serializable (java.io.Serializable)2 StatsStorageRouterProvider (org.deeplearning4j.api.storage.StatsStorageRouterProvider)2 IOutputLayer (org.deeplearning4j.nn.api.layers.IOutputLayer)2 RecurrentLayer (org.deeplearning4j.nn.api.layers.RecurrentLayer)2