Search in sources :

Example 41 with NeuralNetConfiguration

use of org.deeplearning4j.nn.conf.NeuralNetConfiguration in project deeplearning4j by deeplearning4j.

the class TestSerialization method testModelSerde.

@Test
public void testModelSerde() throws Exception {
    ObjectMapper mapper = getMapper();
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().momentum(0.9f).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1000).learningRate(1e-1f).layer(new org.deeplearning4j.nn.conf.layers.AutoEncoder.Builder().nIn(4).nOut(3).corruptionLevel(0.6).sparsity(0.5).lossFunction(LossFunctions.LossFunction.RECONSTRUCTION_CROSSENTROPY).build()).build();
    DataSet d2 = new IrisDataSetIterator(150, 150).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.asList(new ScoreIterationListener(1), new HistogramIterationListener(1)), 0, params, true);
    da.setInput(input);
    ModelAndGradient g = new ModelAndGradient(da);
    String json = mapper.writeValueAsString(g);
    ModelAndGradient read = mapper.readValue(json, ModelAndGradient.class);
    assertEquals(g, read);
}
Also used : IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSet(org.nd4j.linalg.dataset.DataSet) ModelAndGradient(org.deeplearning4j.ui.weights.ModelAndGradient) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) HistogramIterationListener(org.deeplearning4j.ui.weights.HistogramIterationListener) INDArray(org.nd4j.linalg.api.ndarray.INDArray) AutoEncoder(org.deeplearning4j.nn.layers.feedforward.autoencoder.AutoEncoder) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 42 with NeuralNetConfiguration

use of org.deeplearning4j.nn.conf.NeuralNetConfiguration in project deeplearning4j by deeplearning4j.

the class TrainModule method getModelData.

private Result getModelData(String str) {
    Long lastUpdateTime = lastUpdateForSession.get(currentSessionID);
    if (lastUpdateTime == null)
        lastUpdateTime = -1L;
    //TODO validation
    int layerIdx = Integer.parseInt(str);
    I18N i18N = I18NProvider.getInstance();
    //Model info for layer
    boolean noData = currentSessionID == null;
    //First pass (optimize later): query all data...
    StatsStorage ss = (noData ? null : knownSessionIDs.get(currentSessionID));
    String wid = getWorkerIdForIndex(currentWorkerIdx);
    if (wid == null) {
        noData = true;
    }
    Map<String, Object> result = new HashMap<>();
    result.put("updateTimestamp", lastUpdateTime);
    Triple<MultiLayerConfiguration, ComputationGraphConfiguration, NeuralNetConfiguration> conf = getConfig();
    if (conf == null) {
        return ok(Json.toJson(result));
    }
    TrainModuleUtils.GraphInfo gi = getGraphInfo();
    if (gi == null) {
        return ok(Json.toJson(result));
    }
    // Get static layer info
    String[][] layerInfoTable = getLayerInfoTable(layerIdx, gi, i18N, noData, ss, wid);
    result.put("layerInfo", layerInfoTable);
    //First: get all data, and subsample it if necessary, to avoid returning too many points...
    List<Persistable> updates = (noData ? null : ss.getAllUpdatesAfter(currentSessionID, StatsListener.TYPE_ID, wid, 0));
    List<Integer> iterationCounts = null;
    boolean needToHandleLegacyIterCounts = false;
    if (updates != null && updates.size() > maxChartPoints) {
        int subsamplingFrequency = updates.size() / maxChartPoints;
        List<Persistable> subsampled = new ArrayList<>();
        iterationCounts = new ArrayList<>();
        int pCount = -1;
        int lastUpdateIdx = updates.size() - 1;
        int lastIterCount = -1;
        for (Persistable p : updates) {
            if (!(p instanceof StatsReport))
                continue;
            ;
            StatsReport sr = (StatsReport) p;
            pCount++;
            int iterCount = sr.getIterationCount();
            if (iterCount <= lastIterCount) {
                needToHandleLegacyIterCounts = true;
            }
            lastIterCount = iterCount;
            if (pCount > 0 && subsamplingFrequency > 1 && pCount % subsamplingFrequency != 0) {
                //Skip this to subsample the data
                if (pCount != lastUpdateIdx)
                    //Always keep the most recent value
                    continue;
            }
            subsampled.add(p);
            iterationCounts.add(iterCount);
        }
        updates = subsampled;
    } else if (updates != null) {
        int offset = 0;
        iterationCounts = new ArrayList<>(updates.size());
        int lastIterCount = -1;
        for (Persistable p : updates) {
            if (!(p instanceof StatsReport))
                continue;
            ;
            StatsReport sr = (StatsReport) p;
            int iterCount = sr.getIterationCount();
            if (iterCount <= lastIterCount) {
                needToHandleLegacyIterCounts = true;
            }
            iterationCounts.add(iterCount);
        }
    }
    //Now, it should use the proper iteration counts
    if (needToHandleLegacyIterCounts) {
        cleanLegacyIterationCounts(iterationCounts);
    }
    //Get mean magnitudes line chart
    ModelType mt;
    if (conf.getFirst() != null)
        mt = ModelType.MLN;
    else if (conf.getSecond() != null)
        mt = ModelType.CG;
    else
        mt = ModelType.Layer;
    MeanMagnitudes mm = getLayerMeanMagnitudes(layerIdx, gi, updates, iterationCounts, mt);
    Map<String, Object> mmRatioMap = new HashMap<>();
    mmRatioMap.put("layerParamNames", mm.getRatios().keySet());
    mmRatioMap.put("iterCounts", mm.getIterations());
    mmRatioMap.put("ratios", mm.getRatios());
    mmRatioMap.put("paramMM", mm.getParamMM());
    mmRatioMap.put("updateMM", mm.getUpdateMM());
    result.put("meanMag", mmRatioMap);
    //Get activations line chart for layer
    Triple<int[], float[], float[]> activationsData = getLayerActivations(layerIdx, gi, updates, iterationCounts);
    Map<String, Object> activationMap = new HashMap<>();
    activationMap.put("iterCount", activationsData.getFirst());
    activationMap.put("mean", activationsData.getSecond());
    activationMap.put("stdev", activationsData.getThird());
    result.put("activations", activationMap);
    //Get learning rate vs. time chart for layer
    Map<String, Object> lrs = getLayerLearningRates(layerIdx, gi, updates, iterationCounts, mt);
    result.put("learningRates", lrs);
    //Parameters histogram data
    Persistable lastUpdate = (updates != null && updates.size() > 0 ? updates.get(updates.size() - 1) : null);
    Map<String, Object> paramHistograms = getHistograms(layerIdx, gi, StatsType.Parameters, lastUpdate);
    result.put("paramHist", paramHistograms);
    //Updates histogram data
    Map<String, Object> updateHistograms = getHistograms(layerIdx, gi, StatsType.Updates, lastUpdate);
    result.put("updateHist", updateHistograms);
    return ok(Json.toJson(result));
}
Also used : Persistable(org.deeplearning4j.api.storage.Persistable) StatsReport(org.deeplearning4j.ui.stats.api.StatsReport) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) StatsStorage(org.deeplearning4j.api.storage.StatsStorage) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration)

Example 43 with NeuralNetConfiguration

use of org.deeplearning4j.nn.conf.NeuralNetConfiguration in project deeplearning4j by deeplearning4j.

the class LayerBuilderTest method checkSerialization.

private void checkSerialization(Layer layer) throws Exception {
    NeuralNetConfiguration confExpected = new NeuralNetConfiguration.Builder().layer(layer).build();
    NeuralNetConfiguration confActual;
    // check Java serialization
    byte[] data;
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(bos)) {
        out.writeObject(confExpected);
        data = bos.toByteArray();
    }
    try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
        ObjectInput in = new ObjectInputStream(bis)) {
        confActual = (NeuralNetConfiguration) in.readObject();
    }
    assertEquals("unequal Java serialization", confExpected.getLayer(), confActual.getLayer());
    // check JSON
    String json = confExpected.toJson();
    confActual = NeuralNetConfiguration.fromJson(json);
    assertEquals("unequal JSON serialization", confExpected.getLayer(), confActual.getLayer());
    // check YAML
    String yaml = confExpected.toYaml();
    confActual = NeuralNetConfiguration.fromYaml(yaml);
    assertEquals("unequal YAML serialization", confExpected.getLayer(), confActual.getLayer());
    // check the layer's use of callSuper on equals method
    confActual.getLayer().setDropOut(new java.util.Random().nextDouble());
    assertNotEquals("broken equals method (missing callSuper?)", confExpected.getLayer(), confActual.getLayer());
}
Also used : NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration)

Example 44 with NeuralNetConfiguration

use of org.deeplearning4j.nn.conf.NeuralNetConfiguration in project deeplearning4j by deeplearning4j.

the class OutputLayerTest method testWeightsDifferent.

@Test
public void testWeightsDifferent() {
    Nd4j.MAX_ELEMENTS_PER_SLICE = Integer.MAX_VALUE;
    Nd4j.MAX_SLICES_TO_PRINT = Integer.MAX_VALUE;
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).miniBatch(false).seed(123).iterations(1000).learningRate(1e-1).layer(new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder().nIn(4).nOut(3).weightInit(WeightInit.XAVIER).updater(Updater.ADAGRAD).lossFunction(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).activation(Activation.SOFTMAX).build()).build();
    int numParams = conf.getLayer().initializer().numParams(conf);
    INDArray params = Nd4j.create(1, numParams);
    OutputLayer o = (OutputLayer) conf.getLayer().instantiate(conf, null, 0, params, true);
    o.setBackpropGradientsViewArray(Nd4j.create(1, params.length()));
    int numSamples = 150;
    int batchSize = 150;
    DataSetIterator iter = new IrisDataSetIterator(batchSize, numSamples);
    // Loads data into generator and format consumable for NN
    DataSet iris = iter.next();
    iris.normalizeZeroMeanZeroUnitVariance();
    o.setListeners(new ScoreIterationListener(1));
    SplitTestAndTrain t = iris.splitTestAndTrain(0.8);
    o.fit(t.getTrain());
    log.info("Evaluate model....");
    Evaluation eval = new Evaluation(3);
    eval.eval(t.getTest().getLabels(), o.output(t.getTest().getFeatureMatrix(), true));
    log.info(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) 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 45 with NeuralNetConfiguration

use of org.deeplearning4j.nn.conf.NeuralNetConfiguration in project deeplearning4j by deeplearning4j.

the class SeedTest method testAutoEncoderSeed.

@Test
public void testAutoEncoderSeed() {
    AutoEncoder layerType = new AutoEncoder.Builder().nIn(4).nOut(3).corruptionLevel(0.0).activation(Activation.SIGMOID).build();
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().iterations(1).layer(layerType).seed(123).build();
    int numParams = conf.getLayer().initializer().numParams(conf);
    INDArray params = Nd4j.create(1, numParams);
    Layer layer = conf.getLayer().instantiate(conf, null, 0, params, true);
    layer.fit(data.getFeatureMatrix());
    layer.computeGradientAndScore();
    double score = layer.score();
    INDArray parameters = layer.params();
    layer.setParams(parameters);
    layer.computeGradientAndScore();
    double score2 = layer.score();
    assertEquals(parameters, layer.params());
    assertEquals(score, score2, 1e-4);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) AutoEncoder(org.deeplearning4j.nn.conf.layers.AutoEncoder) Layer(org.deeplearning4j.nn.api.Layer) Test(org.junit.Test)

Aggregations

NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)83 INDArray (org.nd4j.linalg.api.ndarray.INDArray)65 Test (org.junit.Test)55 Layer (org.deeplearning4j.nn.api.Layer)29 Gradient (org.deeplearning4j.nn.gradient.Gradient)26 DenseLayer (org.deeplearning4j.nn.conf.layers.DenseLayer)24 Updater (org.deeplearning4j.nn.api.Updater)22 OutputLayer (org.deeplearning4j.nn.conf.layers.OutputLayer)21 DefaultGradient (org.deeplearning4j.nn.gradient.DefaultGradient)21 DataSet (org.nd4j.linalg.dataset.DataSet)14 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)11 ScoreIterationListener (org.deeplearning4j.optimize.listeners.ScoreIterationListener)9 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)8 IrisDataSetIterator (org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator)6 UniformDistribution (org.deeplearning4j.nn.conf.distribution.UniformDistribution)6 RnnOutputLayer (org.deeplearning4j.nn.layers.recurrent.RnnOutputLayer)6 MnistDataFetcher (org.deeplearning4j.datasets.fetchers.MnistDataFetcher)4 Evaluation (org.deeplearning4j.eval.Evaluation)4 Model (org.deeplearning4j.nn.api.Model)4 ConvolutionLayer (org.deeplearning4j.nn.conf.layers.ConvolutionLayer)4