Search in sources :

Example 6 with OutputLayer

use of org.deeplearning4j.nn.layers.OutputLayer in project deeplearning4j by deeplearning4j.

the class BackTrackLineSearchTest method testSingleMinLineSearch.

@Test
public void testSingleMinLineSearch() throws Exception {
    OutputLayer layer = getIrisLogisticLayerConfig(Activation.SOFTMAX, 100, LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD);
    int nParams = layer.numParams();
    layer.setBackpropGradientsViewArray(Nd4j.create(1, nParams));
    layer.setInput(irisData.getFeatureMatrix());
    layer.setLabels(irisData.getLabels());
    layer.computeGradientAndScore();
    BackTrackLineSearch lineSearch = new BackTrackLineSearch(layer, layer.getOptimizer());
    double step = lineSearch.optimize(layer.params(), layer.gradient().gradient(), layer.gradient().gradient());
    assertEquals(1.0, step, 1e-3);
}
Also used : OutputLayer(org.deeplearning4j.nn.layers.OutputLayer) BackTrackLineSearch(org.deeplearning4j.optimize.solvers.BackTrackLineSearch) Test(org.junit.Test)

Example 7 with OutputLayer

use of org.deeplearning4j.nn.layers.OutputLayer in project deeplearning4j by deeplearning4j.

the class BackTrackLineSearchTest method testMultMaxLineSearch.

@Test
public void testMultMaxLineSearch() throws Exception {
    double score1, score2;
    irisData.normalizeZeroMeanZeroUnitVariance();
    OutputLayer layer = getIrisLogisticLayerConfig(Activation.SOFTMAX, 100, LossFunctions.LossFunction.MCXENT);
    int nParams = layer.numParams();
    layer.setBackpropGradientsViewArray(Nd4j.create(1, nParams));
    layer.setInput(irisData.getFeatureMatrix());
    layer.setLabels(irisData.getLabels());
    layer.computeGradientAndScore();
    score1 = layer.score();
    INDArray origGradient = layer.gradient().gradient().dup();
    DefaultStepFunction sf = new DefaultStepFunction();
    BackTrackLineSearch lineSearch = new BackTrackLineSearch(layer, sf, layer.getOptimizer());
    double step = lineSearch.optimize(layer.params().dup(), layer.gradient().gradient().dup(), layer.gradient().gradient().dup());
    INDArray currParams = layer.params();
    sf.step(currParams, origGradient, step);
    layer.setParams(currParams);
    layer.computeGradientAndScore();
    score2 = layer.score();
    assertTrue("score1 = " + score1 + ", score2 = " + score2, score1 < score2);
}
Also used : OutputLayer(org.deeplearning4j.nn.layers.OutputLayer) BackTrackLineSearch(org.deeplearning4j.optimize.solvers.BackTrackLineSearch) INDArray(org.nd4j.linalg.api.ndarray.INDArray) NegativeDefaultStepFunction(org.deeplearning4j.optimize.stepfunctions.NegativeDefaultStepFunction) DefaultStepFunction(org.deeplearning4j.optimize.stepfunctions.DefaultStepFunction) Test(org.junit.Test)

Example 8 with OutputLayer

use of org.deeplearning4j.nn.layers.OutputLayer in project deeplearning4j by deeplearning4j.

the class IterativeReduceFlatMapAdapter method call.

@Override
public Iterable<INDArray> call(Iterator<DataSet> dataSetIterator) throws Exception {
    if (!dataSetIterator.hasNext()) {
        return Collections.singletonList(Nd4j.zeros(params.value().shape()));
    }
    List<DataSet> collect = new ArrayList<>();
    while (dataSetIterator.hasNext()) {
        collect.add(dataSetIterator.next());
    }
    DataSet data = DataSet.merge(collect, false);
    log.debug("Training on " + data.labelCounts());
    NeuralNetConfiguration conf = NeuralNetConfiguration.fromJson(json);
    int numParams = conf.getLayer().initializer().numParams(conf);
    INDArray thisParams = Nd4j.create(1, numParams);
    Layer network = conf.getLayer().instantiate(conf, null, 0, thisParams, true);
    network.setBackpropGradientsViewArray(Nd4j.create(1, numParams));
    INDArray val = params.value().unsafeDuplication();
    if (val.length() != network.numParams())
        throw new IllegalStateException("Network did not have same number of parameters as the broadcast set parameters");
    network.setParams(val);
    if (network instanceof OutputLayer) {
        OutputLayer o = (OutputLayer) network;
        o.fit(data);
    } else
        network.fit(data.getFeatureMatrix());
    return Collections.singletonList(network.params());
}
Also used : OutputLayer(org.deeplearning4j.nn.layers.OutputLayer) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) ArrayList(java.util.ArrayList) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) OutputLayer(org.deeplearning4j.nn.layers.OutputLayer) Layer(org.deeplearning4j.nn.api.Layer)

Aggregations

OutputLayer (org.deeplearning4j.nn.layers.OutputLayer)8 Test (org.junit.Test)5 BackTrackLineSearch (org.deeplearning4j.optimize.solvers.BackTrackLineSearch)4 INDArray (org.nd4j.linalg.api.ndarray.INDArray)4 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)3 NegativeDefaultStepFunction (org.deeplearning4j.optimize.stepfunctions.NegativeDefaultStepFunction)3 Layer (org.deeplearning4j.nn.api.Layer)2 DataSet (org.nd4j.linalg.dataset.DataSet)2 ArrayList (java.util.ArrayList)1 IrisDataSetIterator (org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator)1 Evaluation (org.deeplearning4j.eval.Evaluation)1 DefaultStepFunction (org.deeplearning4j.optimize.stepfunctions.DefaultStepFunction)1 BaseSparkTest (org.deeplearning4j.spark.BaseSparkTest)1