当前位置: 首页>>代码示例>>Java>>正文


Java MultiLayerNetwork.setParams方法代码示例

本文整理汇总了Java中org.deeplearning4j.nn.multilayer.MultiLayerNetwork.setParams方法的典型用法代码示例。如果您正苦于以下问题:Java MultiLayerNetwork.setParams方法的具体用法?Java MultiLayerNetwork.setParams怎么用?Java MultiLayerNetwork.setParams使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.deeplearning4j.nn.multilayer.MultiLayerNetwork的用法示例。


在下文中一共展示了MultiLayerNetwork.setParams方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testScoringDataSet

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testScoringDataSet() {
    ComputationGraphConfiguration configuration = getIrisGraphConfiguration();
    ComputationGraph graph = new ComputationGraph(configuration);
    graph.init();

    MultiLayerConfiguration mlc = getIrisMLNConfiguration();
    MultiLayerNetwork net = new MultiLayerNetwork(mlc);
    net.init();

    DataSetIterator iris = new IrisDataSetIterator(150, 150);
    DataSet ds = iris.next();

    //Now: set parameters of both networks to be identical. Then feedforward, and check we get the same score
    Nd4j.getRandom().setSeed(12345);
    int nParams = getNumParams();
    INDArray params = Nd4j.rand(1, nParams);
    graph.setParams(params.dup());
    net.setParams(params.dup());

    double scoreMLN = net.score(ds, false);
    double scoreCG = graph.score(ds, false);

    assertEquals(scoreMLN, scoreCG, 1e-4);
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:26,代码来源:TestComputationGraphNetwork.java

示例2: getInitialModel

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Override
public MultiLayerNetwork getInitialModel() {
    // This method will be called ONLY once, in master thread
    NetBroadcastTuple tuple = broadcastModel.getValue();
    if (tuple.getConfiguration() != null) {
        MultiLayerConfiguration conf = tuple.getConfiguration();
        MultiLayerNetwork network = new MultiLayerNetwork(conf);
        network.init();

        if (tuple.getParameters() != null)
            network.setParams(tuple.getParameters());

        // we can assign properly, without
        if (tuple.getUpdaterState() != null)
            network.getUpdater().getStateViewArray().assign(tuple.getUpdaterState());

        return network;
    } else
        return null;
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:21,代码来源:SharedTrainingWorker.java

示例3: testForwardBasicIris

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testForwardBasicIris() {

    ComputationGraphConfiguration configuration = getIrisGraphConfiguration();
    ComputationGraph graph = new ComputationGraph(configuration);
    graph.init();

    MultiLayerConfiguration mlc = getIrisMLNConfiguration();
    MultiLayerNetwork net = new MultiLayerNetwork(mlc);
    net.init();

    DataSetIterator iris = new IrisDataSetIterator(150, 150);
    DataSet ds = iris.next();

    graph.setInput(0, ds.getFeatureMatrix());
    Map<String, INDArray> activations = graph.feedForward(false);
    assertEquals(3, activations.size()); //2 layers + 1 input node
    assertTrue(activations.containsKey("input"));
    assertTrue(activations.containsKey("firstLayer"));
    assertTrue(activations.containsKey("outputLayer"));

    //Now: set parameters of both networks to be identical. Then feedforward, and check we get the same outputs
    Nd4j.getRandom().setSeed(12345);
    int nParams = getNumParams();
    INDArray params = Nd4j.rand(1, nParams);
    graph.setParams(params.dup());
    net.setParams(params.dup());

    List<INDArray> mlnAct = net.feedForward(ds.getFeatureMatrix(), false);
    activations = graph.feedForward(ds.getFeatureMatrix(), false);

    assertEquals(mlnAct.get(0), activations.get("input"));
    assertEquals(mlnAct.get(1), activations.get("firstLayer"));
    assertEquals(mlnAct.get(2), activations.get("outputLayer"));
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:36,代码来源:TestComputationGraphNetwork.java

示例4: testIrisFit

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testIrisFit() {

    ComputationGraphConfiguration configuration = getIrisGraphConfiguration();
    ComputationGraph graph = new ComputationGraph(configuration);
    graph.init();

    MultiLayerConfiguration mlnConfig = getIrisMLNConfiguration();
    MultiLayerNetwork net = new MultiLayerNetwork(mlnConfig);
    net.init();

    Nd4j.getRandom().setSeed(12345);
    int nParams = getNumParams();
    INDArray params = Nd4j.rand(1, nParams);

    graph.setParams(params.dup());
    net.setParams(params.dup());


    DataSetIterator iris = new IrisDataSetIterator(75, 150);

    net.fit(iris);
    iris.reset();

    graph.fit(iris);

    //Check that parameters are equal for both models after fitting:
    INDArray paramsMLN = net.params();
    INDArray paramsGraph = graph.params();

    assertNotEquals(params, paramsGraph);
    assertEquals(paramsMLN, paramsGraph);
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:34,代码来源:TestComputationGraphNetwork.java

示例5: testEvalSplitting

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testEvalSplitting(){
    //Test for "tbptt-like" functionality

    for(WorkspaceMode ws : WorkspaceMode.values()) {
        System.out.println("Starting test for workspace mode: " + ws);

        int nIn = 4;
        int layerSize = 5;
        int nOut = 6;
        int tbpttLength = 10;
        int tsLength = 5 * tbpttLength + tbpttLength / 2;

        MultiLayerConfiguration conf1 = new NeuralNetConfiguration.Builder()
                .seed(12345)
                .trainingWorkspaceMode(ws)
                .inferenceWorkspaceMode(ws)
                .list()
                .layer(new LSTM.Builder().nIn(nIn).nOut(layerSize).build())
                .layer(new RnnOutputLayer.Builder().nIn(layerSize).nOut(nOut)
                        .activation(Activation.SOFTMAX)
                        .build())
                .build();

        MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder()
                .seed(12345)
                .trainingWorkspaceMode(ws)
                .inferenceWorkspaceMode(ws)
                .list()
                .layer(new LSTM.Builder().nIn(nIn).nOut(layerSize).build())
                .layer(new RnnOutputLayer.Builder().nIn(layerSize).nOut(nOut)
                        .activation(Activation.SOFTMAX).build())
                .tBPTTLength(10)
                .backpropType(BackpropType.TruncatedBPTT)
                .build();

        MultiLayerNetwork net1 = new MultiLayerNetwork(conf1);
        net1.init();

        MultiLayerNetwork net2 = new MultiLayerNetwork(conf2);
        net2.init();

        net2.setParams(net1.params());

        for(boolean useMask : new boolean[]{false, true}) {

            INDArray in1 = Nd4j.rand(new int[]{3, nIn, tsLength});
            INDArray out1 = TestUtils.randomOneHotTimeSeries(3, nOut, tsLength);

            INDArray in2 = Nd4j.rand(new int[]{5, nIn, tsLength});
            INDArray out2 = TestUtils.randomOneHotTimeSeries(5, nOut, tsLength);

            INDArray lMask1 = null;
            INDArray lMask2 = null;
            if(useMask){
                lMask1 = Nd4j.create(3, tsLength);
                lMask2 = Nd4j.create(5, tsLength);
                Nd4j.getExecutioner().exec(new BernoulliDistribution(lMask1, 0.5));
                Nd4j.getExecutioner().exec(new BernoulliDistribution(lMask2, 0.5));
            }

            List<DataSet> l = Arrays.asList(new DataSet(in1, out1, null, lMask1), new DataSet(in2, out2, null, lMask2));
            DataSetIterator iter = new ExistingDataSetIterator(l);

            System.out.println("Net 1 eval");
            IEvaluation[] e1 = net1.doEvaluation(iter, new Evaluation(), new ROCMultiClass(), new RegressionEvaluation());
            System.out.println("Net 2 eval");
            IEvaluation[] e2 = net2.doEvaluation(iter, new Evaluation(), new ROCMultiClass(), new RegressionEvaluation());

            assertEquals(e1[0], e2[0]);
            assertEquals(e1[1], e2[1]);
            assertEquals(e1[2], e2[2]);
        }
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:76,代码来源:EvalTest.java

示例6: simpleFineTune

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void simpleFineTune() {

    long rng = 12345L;
    DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));
    //original conf
    NeuralNetConfiguration.Builder confToChange =
                    new NeuralNetConfiguration.Builder().seed(rng).optimizationAlgo(OptimizationAlgorithm.LBFGS)
                                    .updater(new Nesterovs(0.01, 0.99));

    MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(confToChange.list()
                    .layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
                    .layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                    LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                    .build())
                    .build());
    modelToFineTune.init();

    //model after applying changes with transfer learning
    MultiLayerNetwork modelNow =
            new TransferLearning.Builder(modelToFineTune)
                    .fineTuneConfiguration(new FineTuneConfiguration.Builder().seed(rng)
                            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                            .updater(new RmsProp(0.5)) //Intent: override both weight and bias LR, unless bias LR is manually set also
                            .l2(0.4).build())
                    .build();

    for (org.deeplearning4j.nn.api.Layer l : modelNow.getLayers()) {
        BaseLayer bl = ((BaseLayer) l.conf().getLayer());
        assertEquals(new RmsProp(0.5), bl.getIUpdater());
    }


    NeuralNetConfiguration.Builder confSet = new NeuralNetConfiguration.Builder().seed(rng)
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                    .updater(new RmsProp(0.5)).l2(0.4);

    MultiLayerNetwork expectedModel = new MultiLayerNetwork(confSet.list()
                    .layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
                    .layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                    LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                    .build())
                    .build());
    expectedModel.init();
    expectedModel.setParams(modelToFineTune.params().dup());

    assertEquals(expectedModel.params(), modelNow.params());

    //Check json
    MultiLayerConfiguration expectedConf = expectedModel.getLayerWiseConfigurations();
    assertEquals(expectedConf.toJson(), modelNow.getLayerWiseConfigurations().toJson());

    //Check params after fit
    modelNow.fit(randomData);
    expectedModel.fit(randomData);

    assertEquals(modelNow.score(), expectedModel.score(), 1e-6);
    INDArray pExp = expectedModel.params();
    INDArray pNow = modelNow.params();
    assertEquals(pExp, pNow);
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:62,代码来源:TransferLearningMLNTest.java

示例7: testNoutChanges

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testNoutChanges() {
    DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 2));

    NeuralNetConfiguration.Builder equivalentConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1));
    FineTuneConfiguration overallConf = new FineTuneConfiguration.Builder().updater(new Sgd(0.1))
                    .build();

    MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(equivalentConf.list()
                    .layer(0, new DenseLayer.Builder().nIn(4).nOut(5).build())
                    .layer(1, new DenseLayer.Builder().nIn(3).nOut(2).build())
                    .layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
                    .layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                    LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                    .build())
                    .build());
    modelToFineTune.init();
    MultiLayerNetwork modelNow = new TransferLearning.Builder(modelToFineTune).fineTuneConfiguration(overallConf)
                    .nOutReplace(3, 2, WeightInit.XAVIER, WeightInit.XAVIER)
                    .nOutReplace(0, 3, WeightInit.XAVIER, new NormalDistribution(1, 1e-1)).build();

    MultiLayerNetwork modelExpectedArch = new MultiLayerNetwork(equivalentConf.list()
                    .layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
                    .layer(1, new DenseLayer.Builder().nIn(3).nOut(2).build())
                    .layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
                    .layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                    LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(2)
                                                    .build())
                    .build());
    modelExpectedArch.init();

    //Will fail - expected because of dist and weight init changes
    //assertEquals(modelExpectedArch.getLayerWiseConfigurations().toJson(), modelNow.getLayerWiseConfigurations().toJson());

    BaseLayer bl0 = ((BaseLayer) modelNow.getLayerWiseConfigurations().getConf(0).getLayer());
    BaseLayer bl1 = ((BaseLayer) modelNow.getLayerWiseConfigurations().getConf(1).getLayer());
    BaseLayer bl3 = ((BaseLayer) modelNow.getLayerWiseConfigurations().getConf(3).getLayer());
    assertEquals(bl0.getWeightInit(), WeightInit.XAVIER);
    assertEquals(bl0.getDist(), null);
    assertEquals(bl1.getWeightInit(), WeightInit.DISTRIBUTION);
    assertEquals(bl1.getDist(), new NormalDistribution(1, 1e-1));
    assertEquals(bl3.getWeightInit(), WeightInit.XAVIER);

    //modelNow should have the same architecture as modelExpectedArch
    assertArrayEquals(modelExpectedArch.params().shape(), modelNow.params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(0).params().shape(), modelNow.getLayer(0).params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(1).params().shape(), modelNow.getLayer(1).params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(2).params().shape(), modelNow.getLayer(2).params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(3).params().shape(), modelNow.getLayer(3).params().shape());

    modelNow.setParams(modelExpectedArch.params());
    //fit should give the same results
    modelExpectedArch.fit(randomData);
    modelNow.fit(randomData);
    assertEquals(modelExpectedArch.score(), modelNow.score(), 0.000001);
    assertEquals(modelExpectedArch.params(), modelNow.params());
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:58,代码来源:TransferLearningMLNTest.java

示例8: testRemoveAndAdd

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testRemoveAndAdd() {
    DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));

    NeuralNetConfiguration.Builder equivalentConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1));
    FineTuneConfiguration overallConf = new FineTuneConfiguration.Builder().updater(new Sgd(0.1)).build();

    MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(//overallConf.list()
                    equivalentConf.list().layer(0, new DenseLayer.Builder().nIn(4).nOut(5).build())
                                    .layer(1, new DenseLayer.Builder().nIn(5).nOut(2).build())
                                    .layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
                                    .layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                                    LossFunctions.LossFunction.MCXENT)
                                                                    .activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                                    .build())
                                    .build());
    modelToFineTune.init();

    MultiLayerNetwork modelNow =
                    new TransferLearning.Builder(modelToFineTune).fineTuneConfiguration(overallConf)
                                    .nOutReplace(0, 7, WeightInit.XAVIER, WeightInit.XAVIER)
                                    .nOutReplace(2, 5, WeightInit.XAVIER).removeOutputLayer()
                                    .addLayer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(5)
                                                    .nOut(3).updater(new Sgd(0.5)).activation(Activation.SOFTMAX)
                                                    .build())
                                    .build();

    MultiLayerNetwork modelExpectedArch = new MultiLayerNetwork(equivalentConf.list()
                    .layer(0, new DenseLayer.Builder().nIn(4).nOut(7).build())
                    .layer(1, new DenseLayer.Builder().nIn(7).nOut(2).build())
                    .layer(2, new DenseLayer.Builder().nIn(2).nOut(5).build())
                    .layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                    LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX)
                                                    .updater(new Sgd(0.5)).nIn(5).nOut(3).build())
                    .build());
    modelExpectedArch.init();

    //modelNow should have the same architecture as modelExpectedArch
    assertArrayEquals(modelExpectedArch.params().shape(), modelNow.params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(0).params().shape(), modelNow.getLayer(0).params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(1).params().shape(), modelNow.getLayer(1).params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(2).params().shape(), modelNow.getLayer(2).params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(3).params().shape(), modelNow.getLayer(3).params().shape());

    modelNow.setParams(modelExpectedArch.params());
    //fit should give the same results
    modelExpectedArch.fit(randomData);
    modelNow.fit(randomData);
    assertTrue(modelExpectedArch.score() == modelNow.score());
    assertEquals(modelExpectedArch.params(), modelNow.params());
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:52,代码来源:TransferLearningMLNTest.java

示例9: testChangeLrMLNSchedule

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testChangeLrMLNSchedule(){
    //First: Set LR for a *single* layer and compare vs. equivalent net config
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .activation(Activation.TANH)
            .seed(12345)
            .updater(new Adam(0.1))
            .list()
            .layer(new DenseLayer.Builder().nIn(10).nOut(10).build())
            .layer(new DenseLayer.Builder().nIn(10).nOut(10).build())
            .layer(new OutputLayer.Builder().nIn(10).nOut(10).build())
            .build();

    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();

    for( int i=0; i<10; i++ ){
        net.fit(Nd4j.rand(10,10), Nd4j.rand(10,10));
    }


    MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder()
            .activation(Activation.TANH)
            .seed(12345)
            .updater(new Adam(new ExponentialSchedule(ScheduleType.ITERATION, 0.5, 0.8 )))
            .list()
            .layer(new DenseLayer.Builder().nIn(10).nOut(10).build())
            .layer(new DenseLayer.Builder().nIn(10).nOut(10).build())
            .layer(new OutputLayer.Builder().nIn(10).nOut(10).build())
            .build();
    MultiLayerNetwork net2 = new MultiLayerNetwork(conf2);
    net2.init();
    net2.getUpdater().getStateViewArray().assign(net.getUpdater().getStateViewArray());
    conf2.setIterationCount(conf.getIterationCount());
    net2.setParams(net.params().dup());

    net.setLearningRate(new ExponentialSchedule(ScheduleType.ITERATION, 0.5, 0.8 ));  //Set LR for layer 0 to 0.5

    assertEquals(conf, conf2);
    assertEquals(conf.toJson(), conf2.toJson());

    assertEquals(net.getUpdater().getStateViewArray(), net2.getUpdater().getStateViewArray());

    //Perform some parameter updates - check things are actually in sync...
    for( int i=0; i<3; i++ ){
        INDArray in = Nd4j.rand(10, 10);
        INDArray l = Nd4j.rand(10, 10);

        net.fit(in, l);
        net2.fit(in, l);
    }

    assertEquals(net.params(), net2.params());
    assertEquals(net.getUpdater().getStateViewArray(), net2.getUpdater().getStateViewArray());
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:56,代码来源:TestLrChanges.java

示例10: testCompareRnnOutputRnnLoss

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testCompareRnnOutputRnnLoss(){
    Nd4j.getRandom().setSeed(12345);

    int timeSeriesLength = 4;
    int nIn = 5;
    int layerSize = 6;
    int nOut = 6;
    int miniBatchSize = 3;

    MultiLayerConfiguration conf1 =
            new NeuralNetConfiguration.Builder().seed(12345L)
                    .updater(new NoOp())
                    .list()
                    .layer(new LSTM.Builder().nIn(nIn).nOut(layerSize).activation(Activation.TANH)
                            .weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 1.0))
                            .updater(new NoOp()).build())
                    .layer(new DenseLayer.Builder().nIn(layerSize).nOut(nOut).activation(Activation.IDENTITY).build())
                    .layer(new RnnLossLayer.Builder(LossFunction.MCXENT)
                            .activation(Activation.SOFTMAX)
                            .build())
                    .pretrain(false).backprop(true).build();

    MultiLayerNetwork mln = new MultiLayerNetwork(conf1);
    mln.init();


    MultiLayerConfiguration conf2 =
            new NeuralNetConfiguration.Builder().seed(12345L)
                    .updater(new NoOp())
                    .list()
                    .layer(new LSTM.Builder().nIn(nIn).nOut(layerSize).activation(Activation.TANH)
                            .weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 1.0))
                            .updater(new NoOp()).build())
                    .layer(new org.deeplearning4j.nn.conf.layers.RnnOutputLayer.Builder(LossFunction.MCXENT)
                            .activation(Activation.SOFTMAX)
                            .nIn(layerSize).nOut(nOut)
                            .build())
                    .pretrain(false).backprop(true).build();

    MultiLayerNetwork mln2 = new MultiLayerNetwork(conf2);
    mln2.init();

    mln2.setParams(mln.params());

    INDArray in = Nd4j.rand(new int[]{miniBatchSize, nIn, timeSeriesLength});

    INDArray out1 = mln.output(in);
    INDArray out2 = mln.output(in);

    assertEquals(out1, out2);

    Random r = new Random(12345);
    INDArray labels = Nd4j.create(miniBatchSize, nOut, timeSeriesLength);
    for( int i=0; i<miniBatchSize; i++ ){
        for( int j=0; j<timeSeriesLength; j++ ){
            labels.putScalar(i, r.nextInt(nOut), j, 1.0);
        }
    }

    mln.setInput(in);
    mln.setLabels(labels);

    mln2.setInput(in);
    mln2.setLabels(labels);

    mln.computeGradientAndScore();
    mln2.computeGradientAndScore();

    assertEquals(mln.gradient().gradient(), mln2.gradient().gradient());
    assertEquals(mln.score(), mln2.score(), 1e-6);

    TestUtils.testModelSerialization(mln);
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:75,代码来源:OutputLayerTest.java

示例11: testEmbeddingForwardPass

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testEmbeddingForwardPass() {
    //With the same parameters, embedding layer should have same activations as the equivalent one-hot representation
    // input with a DenseLayer

    int nClassesIn = 10;

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().activation(Activation.TANH).list()
                    .layer(0, new EmbeddingLayer.Builder().hasBias(true).nIn(nClassesIn).nOut(5).build())
                    .layer(1, new OutputLayer.Builder().nIn(5).nOut(4).build()).pretrain(false).backprop(true)
                    .build();
    MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder().activation(Activation.TANH).list()
                    .layer(0, new DenseLayer.Builder().nIn(nClassesIn).nOut(5).build())
                    .layer(1, new OutputLayer.Builder().nIn(5).nOut(4).build()).pretrain(false).backprop(true)
                    .build();

    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    MultiLayerNetwork net2 = new MultiLayerNetwork(conf2);
    net.init();
    net2.init();

    net2.setParams(net.params().dup());

    int batchSize = 3;
    INDArray inEmbedding = Nd4j.create(batchSize, 1);
    INDArray inOneHot = Nd4j.create(batchSize, nClassesIn);

    Random r = new Random(12345);
    for (int i = 0; i < batchSize; i++) {
        int classIdx = r.nextInt(nClassesIn);
        inEmbedding.putScalar(i, classIdx);
        inOneHot.putScalar(new int[] {i, classIdx}, 1.0);
    }

    List<INDArray> activationsEmbedding = net.feedForward(inEmbedding, false);
    List<INDArray> activationsDense = net2.feedForward(inOneHot, false);
    for (int i = 1; i < 3; i++) {
        INDArray actE = activationsEmbedding.get(i);
        INDArray actD = activationsDense.get(i);
        assertEquals(actE, actD);
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:43,代码来源:EmbeddingLayerTest.java

示例12: testEmbeddingBackwardPass

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testEmbeddingBackwardPass() {
    //With the same parameters, embedding layer should have same activations as the equivalent one-hot representation
    // input with a DenseLayer

    int nClassesIn = 10;

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().activation(Activation.TANH).list()
                    .layer(0, new EmbeddingLayer.Builder().hasBias(true).nIn(nClassesIn).nOut(5).build()).layer(1,
                                    new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(5).nOut(4)
                                                    .activation(Activation.SOFTMAX).build())
                    .pretrain(false).backprop(true).build();
    MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder().activation(Activation.TANH)
                    .weightInit(WeightInit.XAVIER).list()
                    .layer(0, new DenseLayer.Builder().nIn(nClassesIn).nOut(5).build()).layer(1,
                                    new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(5).nOut(4)
                                                    .activation(Activation.SOFTMAX).build())
                    .pretrain(false).backprop(true).build();

    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    MultiLayerNetwork net2 = new MultiLayerNetwork(conf2);
    net.init();
    net2.init();

    net2.setParams(net.params().dup());

    int batchSize = 3;
    INDArray inEmbedding = Nd4j.create(batchSize, 1);
    INDArray inOneHot = Nd4j.create(batchSize, nClassesIn);
    INDArray outLabels = Nd4j.create(batchSize, 4);

    Random r = new Random(12345);
    for (int i = 0; i < batchSize; i++) {
        int classIdx = r.nextInt(nClassesIn);
        inEmbedding.putScalar(i, classIdx);
        inOneHot.putScalar(new int[] {i, classIdx}, 1.0);

        int labelIdx = r.nextInt(4);
        outLabels.putScalar(new int[] {i, labelIdx}, 1.0);
    }

    net.setInput(inEmbedding);
    net2.setInput(inOneHot);
    net.setLabels(outLabels);
    net2.setLabels(outLabels);

    net.computeGradientAndScore();
    net2.computeGradientAndScore();

    System.out.println(net.score() + "\t" + net2.score());
    assertEquals(net2.score(), net.score(), 1e-6);

    Map<String, INDArray> gradient = net.gradient().gradientForVariable();
    Map<String, INDArray> gradient2 = net2.gradient().gradientForVariable();
    assertEquals(gradient.size(), gradient2.size());

    for (String s : gradient.keySet()) {
        assertEquals(gradient2.get(s), gradient.get(s));
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:61,代码来源:EmbeddingLayerTest.java

示例13: testEmbeddingLayerRNN

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testEmbeddingLayerRNN() {

    int nClassesIn = 10;

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().activation(Activation.TANH).list()
                    .layer(0, new EmbeddingLayer.Builder().hasBias(true).nIn(nClassesIn).nOut(5).build())
                    .layer(1, new GravesLSTM.Builder().nIn(5).nOut(7).activation(Activation.SOFTSIGN).build())
                    .layer(2, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(7).nOut(4)
                                    .activation(Activation.SOFTMAX).build())
                    .inputPreProcessor(0, new RnnToFeedForwardPreProcessor())
                    .inputPreProcessor(1, new FeedForwardToRnnPreProcessor()).pretrain(false).backprop(true)
                    .build();
    MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder().activation(Activation.TANH)
                    .weightInit(WeightInit.XAVIER).list()
                    .layer(0, new DenseLayer.Builder().nIn(nClassesIn).nOut(5).build())
                    .layer(1, new GravesLSTM.Builder().nIn(5).nOut(7).activation(Activation.SOFTSIGN).build())
                    .layer(2, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(7).nOut(4)
                                    .activation(Activation.SOFTMAX).build())
                    .inputPreProcessor(0, new RnnToFeedForwardPreProcessor())
                    .inputPreProcessor(1, new FeedForwardToRnnPreProcessor()).pretrain(false).backprop(true)
                    .build();

    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    MultiLayerNetwork net2 = new MultiLayerNetwork(conf2);
    net.init();
    net2.init();

    net2.setParams(net.params().dup());

    int batchSize = 3;
    int timeSeriesLength = 8;
    INDArray inEmbedding = Nd4j.create(batchSize, 1, timeSeriesLength);
    INDArray inOneHot = Nd4j.create(batchSize, nClassesIn, timeSeriesLength);
    INDArray outLabels = Nd4j.create(batchSize, 4, timeSeriesLength);

    Random r = new Random(12345);
    for (int i = 0; i < batchSize; i++) {
        for (int j = 0; j < timeSeriesLength; j++) {
            int classIdx = r.nextInt(nClassesIn);
            inEmbedding.putScalar(new int[] {i, 0, j}, classIdx);
            inOneHot.putScalar(new int[] {i, classIdx, j}, 1.0);

            int labelIdx = r.nextInt(4);
            outLabels.putScalar(new int[] {i, labelIdx, j}, 1.0);
        }
    }

    net.setInput(inEmbedding);
    net2.setInput(inOneHot);
    net.setLabels(outLabels);
    net2.setLabels(outLabels);

    net.computeGradientAndScore();
    net2.computeGradientAndScore();

    System.out.println(net.score() + "\t" + net2.score());
    assertEquals(net2.score(), net.score(), 1e-6);

    Map<String, INDArray> gradient = net.gradient().gradientForVariable();
    Map<String, INDArray> gradient2 = net2.gradient().gradientForVariable();
    assertEquals(gradient.size(), gradient2.size());

    for (String s : gradient.keySet()) {
        assertEquals(gradient2.get(s), gradient.get(s));
    }

}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:69,代码来源:EmbeddingLayerTest.java

示例14: testBackwardIrisBasic

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testBackwardIrisBasic() {
    ComputationGraphConfiguration configuration = getIrisGraphConfiguration();
    ComputationGraph graph = new ComputationGraph(configuration);
    graph.init();

    MultiLayerConfiguration mlc = getIrisMLNConfiguration();
    MultiLayerNetwork net = new MultiLayerNetwork(mlc);
    net.init();

    DataSetIterator iris = new IrisDataSetIterator(150, 150);
    DataSet ds = iris.next();

    //Now: set parameters of both networks to be identical. Then feedforward, and check we get the same outputs
    Nd4j.getRandom().setSeed(12345);
    int nParams = (4 * 5 + 5) + (5 * 3 + 3);
    INDArray params = Nd4j.rand(1, nParams);
    graph.setParams(params.dup());
    net.setParams(params.dup());

    INDArray input = ds.getFeatureMatrix();
    INDArray labels = ds.getLabels();
    graph.setInput(0, input.dup());
    graph.setLabel(0, labels.dup());

    net.setInput(input.dup());
    net.setLabels(labels.dup());

    //Compute gradients
    net.computeGradientAndScore();
    Pair<Gradient, Double> netGradScore = net.gradientAndScore();

    graph.computeGradientAndScore();
    Pair<Gradient, Double> graphGradScore = graph.gradientAndScore();

    assertEquals(netGradScore.getSecond(), graphGradScore.getSecond(), 1e-3);

    //Compare gradients
    Gradient netGrad = netGradScore.getFirst();
    Gradient graphGrad = graphGradScore.getFirst();

    assertNotNull(graphGrad);
    assertEquals(netGrad.gradientForVariable().size(), graphGrad.gradientForVariable().size());

    assertEquals(netGrad.getGradientFor("0_W"), graphGrad.getGradientFor("firstLayer_W"));
    assertEquals(netGrad.getGradientFor("0_b"), graphGrad.getGradientFor("firstLayer_b"));
    assertEquals(netGrad.getGradientFor("1_W"), graphGrad.getGradientFor("outputLayer_W"));
    assertEquals(netGrad.getGradientFor("1_b"), graphGrad.getGradientFor("outputLayer_b"));
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:50,代码来源:TestComputationGraphNetwork.java

示例15: testGetSetParams

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testGetSetParams() {

    MultiLayerNetwork net = getCNNMLNConfig(true, false);

    INDArray paramsOrig = net.params().dup();
    net.setParams(paramsOrig);

    INDArray params2 = net.params();

    assertEquals(paramsOrig, params2);
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:13,代码来源:ConvolutionLayerTest.java


注:本文中的org.deeplearning4j.nn.multilayer.MultiLayerNetwork.setParams方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。