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


Java NeuralNetConfiguration.ListBuilder方法代码示例

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


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

示例1: testDeconvolution2DUnsupportedSameModeNetwork

import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test(expected = IllegalArgumentException.class)
public void testDeconvolution2DUnsupportedSameModeNetwork() {
    /*
     * When convolution mode Same is set for the network and a deconvolution layer is added
     * then only layer activation will fail. Suboptimal, but I don't think we want special
     * logic for NNC in this case.
     */
    NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
            .updater(new NoOp())
            .activation(Activation.SIGMOID)
            .convolutionMode(Same)
            .list()
            .layer(new Deconvolution2D.Builder().name("deconvolution")
                    .nIn(3).nOut(2).build());

    MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
            .activation(Activation.SOFTMAX).nOut(2).build())
            .setInputType(InputType.convolutionalFlat(7, 7, 3)).build();

    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();
    net.getLayer(0).activate(Nd4j.rand(10, 7 * 7 * 3));
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:24,代码来源:CNNGradientCheckTest.java

示例2: testMultiChannel

import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testMultiChannel() throws Exception {
    INDArray in = Nd4j.rand(new int[] {10, 3, 28, 28});
    INDArray labels = Nd4j.rand(10, 2);
    DataSet next = new DataSet(in, labels);

    NeuralNetConfiguration.ListBuilder builder = (NeuralNetConfiguration.ListBuilder) incompleteLFW();
    builder.setInputType(InputType.convolutional(28, 28, 3));
    MultiLayerConfiguration conf = builder.build();
    ConvolutionLayer layer2 = (ConvolutionLayer) conf.getConf(2).getLayer();
    assertEquals(6, layer2.getNIn());

    MultiLayerNetwork network = new MultiLayerNetwork(conf);
    network.init();
    network.fit(next);
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:17,代码来源:ConvolutionLayerSetupTest.java

示例3: testLRN

import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testLRN() throws Exception {
    List<String> labels = new ArrayList<>(Arrays.asList("Zico", "Ziwang_Xu"));
    String rootDir = new ClassPathResource("lfwtest").getFile().getAbsolutePath();

    RecordReader reader = new ImageRecordReader(28, 28, 3);
    reader.initialize(new FileSplit(new File(rootDir)));
    DataSetIterator recordReader = new RecordReaderDataSetIterator(reader, 10, 1, labels.size());
    labels.remove("lfwtest");
    NeuralNetConfiguration.ListBuilder builder = (NeuralNetConfiguration.ListBuilder) incompleteLRN();
    builder.setInputType(InputType.convolutional(28, 28, 3));

    MultiLayerConfiguration conf = builder.build();

    ConvolutionLayer layer2 = (ConvolutionLayer) conf.getConf(3).getLayer();
    assertEquals(6, layer2.getNIn());

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

示例4: getIrisMLPSimpleConfig

import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
/** Very simple back-prop config set up for Iris.
 * Learning Rate = 0.1
 * No regularization, no Adagrad, no momentum etc. One iteration.
 */
private static MultiLayerConfiguration getIrisMLPSimpleConfig(int[] hiddenLayerSizes,
                Activation activationFunction) {
    NeuralNetConfiguration.ListBuilder lb = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1))
                .seed(12345L).list();

    for (int i = 0; i < hiddenLayerSizes.length; i++) {
        int nIn = (i == 0 ? 4 : hiddenLayerSizes[i - 1]);
        lb.layer(i, new DenseLayer.Builder().nIn(nIn).nOut(hiddenLayerSizes[i]).weightInit(WeightInit.XAVIER)
                        .activation(activationFunction).build());
    }

    lb.layer(hiddenLayerSizes.length,
                    new OutputLayer.Builder(LossFunction.MCXENT).nIn(hiddenLayerSizes[hiddenLayerSizes.length - 1])
                                    .nOut(3).weightInit(WeightInit.XAVIER)
                                    .activation(activationFunction.equals(Activation.IDENTITY) ? Activation.IDENTITY
                                                    : Activation.SOFTMAX)
                                    .build());
    lb.pretrain(false).backprop(true);

    return lb.build();
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:26,代码来源:BackPropMLPTest.java

示例5: train

import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
public void train() throws InterruptedException, IOException {
    //Split data into training and testing sets:
    List<String> allLines = Files.readAllLines(dataPath.resolve(RANGES_FILENAME), StandardCharsets.UTF_8);
    int rows = allLines.size();
    int columns = 1;
    for (char c : allLines.get(0).toCharArray()) {
        columns += c == ',' ? 1 : 0;
    }
    Collections.shuffle(allLines, new Random(seed));
    int splitRow = 80 * rows / 100;
    Files.write(dataPath.resolve(TRAIN_FILENAME), allLines.subList(0, splitRow), StandardCharsets.UTF_8);
    Files.write(dataPath.resolve(TEST_FILENAME), allLines.subList(splitRow, rows), StandardCharsets.UTF_8);

    int numInputs = columns - 1;
    int numOutputs = 2;

    //Load the training data:
    RecordReader rr = new CSVRecordReader();
    rr.initialize(new FileSplit(dataPath.resolve(TRAIN_FILENAME).toFile()));
    DataSetIterator trainIter = new RecordReaderDataSetIterator(rr, batchSize, -1, 2);

    //Load the test/evaluation data:
    RecordReader rrTest = new CSVRecordReader();
    rrTest.initialize(new FileSplit(dataPath.resolve(TEST_FILENAME).toFile()));
    DataSetIterator testIter = new RecordReaderDataSetIterator(rrTest, batchSize, -1, 2);

    NeuralNetConfiguration.ListBuilder listBuilder = new NeuralNetConfiguration.Builder()
            .seed(seed)
            .iterations(1)
            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
            .learningRate(learningRate)
            .regularization(true).l2(l2Regularization)
            .updater(Updater.NESTEROVS).momentum(0.9)
            .list()
            .layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(numHiddenNodes)
                    .weightInit(WeightInit.XAVIER)
                    .activation(Activation.RELU)
                    .build());

    for (int i = 1; i < numLayers; i++) {
        listBuilder.layer(i, new DenseLayer.Builder().nIn(numHiddenNodes).nOut(numHiddenNodes)
                .weightInit(WeightInit.XAVIER)
                .activation(Activation.RELU)
                .build());
    }

    MultiLayerConfiguration conf = listBuilder
            .layer(numLayers, new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD)
                    .weightInit(WeightInit.XAVIER)
                    .activation(Activation.SOFTMAX)
                    .nIn(numHiddenNodes).nOut(numOutputs).build())
            .pretrain(false).backprop(true).build();

    model = new MultiLayerNetwork(conf);
    model.init();
    model.setListeners(new ScoreIterationListener(100));  //Print score every 100 parameter updates

    for (int n = 0; n < nEpochs; n++) {
        model.fit(trainIter);

        log.info("Evaluating model...");
        Evaluation eval = new Evaluation(numOutputs);
        while (testIter.hasNext()){
            DataSet ds = testIter.next();
            INDArray predicted = model.output(ds.getFeatureMatrix(), false);
            eval.eval(ds.getLabels(), predicted);
        }
        testIter.reset();

        String stats = eval.stats();

        //Print the evaluation statistics
        log.info(stats);

        Files.write(dataPath.resolve("eval_stats_" + n + ".txt" + suffix), stats.getBytes());
    }

    File f = dataPath.resolve(MODEL_FILENAME + suffix).toFile();
    log.info("Saving " + f);
    ModelSerializer.writeModel(model, f, true);
}
 
开发者ID:SkymindIO,项目名称:FetchKobuki,代码行数:82,代码来源:KobukiDetector.java

示例6: setConfig

import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
public TimeseriesClassifierNetworkBuilder setConfig(Config tsconfig, String networkConfigPrefix) {
            this.configuration = tsconfig;
            NeuralNetConfiguration.Builder networkConfigurationBuilder = new NeuralNetConfiguration.Builder()
                    .seed(configuration.getInt(networkConfigPrefix+".seed"))
                    .optimizationAlgo(OptimizationAlgorithm.valueOf(configuration.getString(networkConfigPrefix+".optimizationAlgo.type")))
                        .iterations(configuration.getInt(networkConfigPrefix+".optimizationAlgo.iterations"))
                    .weightInit(WeightInit.valueOf(
                            configuration.getString(networkConfigPrefix+".weightInit")))
                    .updater(Updater.valueOf(configuration.getString(networkConfigPrefix+".updater.type")))
                        .momentum(configuration.getDouble(networkConfigPrefix+".updater.momentum"))
                    .learningRate(configuration.getDouble(networkConfigPrefix+".learningRate"))
                    .gradientNormalization(GradientNormalization.valueOf(
                            configuration.getString(networkConfigPrefix+".gradientNormalization.type")))
                    .gradientNormalizationThreshold(
                            configuration.getDouble(networkConfigPrefix+".gradientNormalization.threshold"));

            ConfigList layers = configuration.getList(networkConfigPrefix+".layers");

            if(layers != null && layers.size()>0){
                NeuralNetConfiguration.ListBuilder networkConfigurationListBuilder =
                        networkConfigurationBuilder.list();

                for (int i = 0; i < layers.size(); i++) {
                    ConfigValue value = layers.get(i);
                    switch (value.valueType()) {
                        case OBJECT:

                            Config configObject = ((ConfigObject) value).toConfig();

                            networkConfigurationListBuilder
                                    .layer(configObject.getInt("number"),
                                            NetworkTypeFactory.makeLayer(configObject));

                            break;
                        case NULL:
                            throw new RuntimeException("LAYER LIST ITEM CANT BE NULL.");
                        default:
                            throw new RuntimeException("LAYER TYPE MUST BE A CONFIG OBJECT");
                    }
                }

                this.multiLayerConfiguration = networkConfigurationListBuilder
                        .pretrain(false).backprop(true).build();

            } else {
                throw new RuntimeException("NO NETWORK LIST FOUND.");
            }







            //            // ----- Configure the network -----
//            MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
//                    .seed(123)    //Random number generator seed for improved repeatability. Optional.
//                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1)
//                    .weightInit(WeightInit.XAVIER)
//                    .updater(Updater.NESTEROVS).momentum(0.85)
//                    .learningRate(0.02)
//                    .gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue)  //Not always required, but helps with this data set
//                    .gradientNormalizationThreshold(0.45)
//                    .list()
//                    .layer(0, new GravesLSTM.Builder().activation("tanh").nIn(1).nOut(10).build())
//                    .layer(1, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
//                            .activation("softmax").nIn(10).nOut(numLabelClasses).build())
//                    .pretrain(false).backprop(true).build();



            return this;
        }
 
开发者ID:claytantor,项目名称:blueweave,代码行数:74,代码来源:TimeseriesClassifierNetwork.java

示例7: getMultiLayerConfiguration

import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
/**
     * Configure a MultiLayerConfiguration from this Keras Sequential model configuration.
     *
     * @return          MultiLayerConfiguration
     */
    public MultiLayerConfiguration getMultiLayerConfiguration()
            throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
        if (!this.className.equals(config.getFieldClassNameSequential()))
            throw new InvalidKerasConfigurationException(
                    "Keras model class name " + this.className + " incompatible with MultiLayerNetwork");
        if (this.inputLayerNames.size() != 1)
            throw new InvalidKerasConfigurationException(
                    "MultiLayeNetwork expects only 1 input (found " + this.inputLayerNames.size() + ")");
        if (this.outputLayerNames.size() != 1)
            throw new InvalidKerasConfigurationException(
                    "MultiLayeNetwork expects only 1 output (found " + this.outputLayerNames.size() + ")");

        NeuralNetConfiguration.Builder modelBuilder = new NeuralNetConfiguration.Builder();
        NeuralNetConfiguration.ListBuilder listBuilder = modelBuilder.list();

        /* Add layers one at a time. */
        KerasLayer prevLayer = null;
        int layerIndex = 0;
        for (KerasLayer layer : this.layersOrdered) {
            if (layer.isLayer()) {
                int nbInbound = layer.getInboundLayerNames().size();
                if (nbInbound != 1)
                    throw new InvalidKerasConfigurationException(
                            "Layers in MultiLayerConfiguration must have exactly one inbound layer (found "
                                    + nbInbound + " for layer " + layer.getLayerName() + ")");
                if (prevLayer != null) {
                    InputType[] inputTypes = new InputType[1];
                    InputPreProcessor preprocessor;
                    if (prevLayer.isInputPreProcessor()) {
                        inputTypes[0] = this.outputTypes.get(prevLayer.getInboundLayerNames().get(0));
                        preprocessor = prevLayer.getInputPreprocessor(inputTypes);
                    } else {
                        inputTypes[0] = this.outputTypes.get(prevLayer.getLayerName());
                        preprocessor = layer.getInputPreprocessor(inputTypes);
                    }
                    if (preprocessor != null)
                        listBuilder.inputPreProcessor(layerIndex, preprocessor);
                }
                listBuilder.layer(layerIndex++, layer.getLayer());
                if (this.outputLayerNames.contains(layer.getLayerName()) && !(layer.getLayer() instanceof IOutputLayer)) {
                    // TODO: since this is always true right now, it just clutters the output.
//                    log.warn("Model cannot be trained: output layer " + layer.getLayerName()
//                            + " is not an IOutputLayer (no loss function specified)");
                }
            } else if (layer.getVertex() != null)
                throw new InvalidKerasConfigurationException("Cannot add vertex to MultiLayerConfiguration (class name "
                        + layer.getClassName() + ", layer name " + layer.getLayerName() + ")");
            prevLayer = layer;
        }

        InputType inputType = this.layersOrdered.get(0).getOutputType();
        if (inputType != null)
            listBuilder.setInputType(inputType);

        /* Whether to use standard backprop (or BPTT) or truncated BPTT. */
        if (this.useTruncatedBPTT && this.truncatedBPTT > 0)
            listBuilder.backpropType(BackpropType.TruncatedBPTT).tBPTTForwardLength(truncatedBPTT)
                    .tBPTTBackwardLength(truncatedBPTT);
        else
            listBuilder.backpropType(BackpropType.Standard);
        return listBuilder.build();
    }
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:68,代码来源:KerasSequentialModel.java

示例8: testDeconvolution2D

import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testDeconvolution2D() {
    int nOut = 2;

    int[] minibatchSizes = new int[]{1, 3, 1, 3, 1, 3, 1, 3};
    int[] kernelSizes = new int[]{1, 1, 3, 3, 1, 1, 3, 3};
    int[] strides = {1, 1, 1, 1, 2, 2, 2, 2};
    int[] dilation = {1, 2, 2, 1, 1, 1, 2, 2};
    Activation[] activations = new Activation[]{Activation.SIGMOID, Activation.TANH, Activation.TANH, Activation.TANH, Activation.TANH,  Activation.SIGMOID, Activation.SIGMOID, Activation.SIGMOID};
    ConvolutionMode[] cModes = new ConvolutionMode[]{Truncate, Truncate, Truncate, Truncate, Truncate, Truncate, Truncate, Truncate};
    int width = 7;
    int height = 7;
    int inputDepth = 3;

    Nd4j.getRandom().setSeed(12345);

    for (int i = 0; i < minibatchSizes.length; i++) {
        int minibatchSize = minibatchSizes[i];
        int k = kernelSizes[i];
        int s = strides[i];
        int d = dilation[i];
        ConvolutionMode cm = cModes[i];
        Activation act = activations[i];


        int w = d * width;
        int h = d * height;

        INDArray input = Nd4j.rand(minibatchSize, w * h * inputDepth);
        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
        for (int j = 0; j < minibatchSize; j++) {
            labels.putScalar(new int[]{j, j % nOut}, 1.0);
        }

        NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
                .updater(new NoOp())
                .activation(act)
                .list()
                .layer(new Deconvolution2D.Builder().name("deconvolution_2D_layer")
                        .kernelSize(k, k)
                        .stride(s, s)
                        .dilation(d, d)
                        .convolutionMode(cm)
                        .nIn(inputDepth).nOut(nOut).build());

        MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutionalFlat(h, w, inputDepth)).build();

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

        for (int j = 0; j < net.getLayers().length; j++) {
            System.out.println("nParams, layer " + j + ": " + net.getLayer(j).numParams());
        }

        String msg = " - mb=" + minibatchSize + ", k="
                + k + ", s=" + s + ", d=" + d + ", cm=" + cm;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);

        assertTrue(msg, gradOK);
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:67,代码来源:CNNGradientCheckTest.java

示例9: testSeparableConv2D

import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testSeparableConv2D() {
    int nOut = 2;

    int[] minibatchSizes = new int[] {1, 3};
    int width = 8;
    int height = 8;
    int inputDepth = 3;
    int[] kernelSizes = new int[]{2, 3};
    int[] strides = {1, 2};
    int[] dilation = {1, 2};
    ConvolutionMode[] cModes = new ConvolutionMode[]{ConvolutionMode.Truncate};

    Nd4j.getRandom().setSeed(12345);

    int[] ks = new int[]{1,3,1,3,1,3,1,3};
    int[] ss = new int[]{1,1,2,2,1,1,2,2};
    int[] ds = new int[]{1,1,1,1,2,2,2,2};
    ConvolutionMode[] cms = new ConvolutionMode[]{Truncate, Truncate, Truncate, Truncate, Truncate, Truncate, Truncate, Truncate};
    int[] mb = new int[]{1,1,3,3,3,1,3,3};

    for( int t=0; t<ks.length; t++ ){

        int k = ks[t];
        int s = ss[t];
        int d = ds[t];
        ConvolutionMode cm = cms[t];
        int minibatchSize = mb[t];

        //Use larger input with larger dilation values (to avoid invalid config)
        int w = d * width;
        int h = d * height;

        INDArray input = Nd4j.rand(minibatchSize, w * h * inputDepth);
        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
        for (int i = 0; i < minibatchSize; i++) {
            labels.putScalar(new int[]{i, i % nOut}, 1.0);
        }

        NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
                .updater(new NoOp())
                .activation(Activation.TANH)
                .convolutionMode(cm)
                .list()
                .layer(new SeparableConvolution2D.Builder().name("Separable conv 2D layer")
                        .kernelSize(k, k)
                        .stride(s, s)
                        .dilation(d, d)
                        .depthMultiplier(3)
                        .nIn(inputDepth).nOut(2).build());

        MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutionalFlat(h, w, inputDepth)).build();

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

        for (int i = 0; i < net.getLayers().length; i++) {
            System.out.println("nParams, layer " + i + ": " + net.getLayer(i).numParams());
        }

        String msg = " - mb=" + minibatchSize + ", k="
                + k + ", s=" + s + ", d=" + d + ", cm=" + cm;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);

        assertTrue(msg, gradOK);
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:73,代码来源:CNNGradientCheckTest.java

示例10: testCnnDilated

import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testCnnDilated() {
    int nOut = 2;

    int minibatchSize = 3;
    int width = 8;
    int height = 8;
    int inputDepth = 3;

    Nd4j.getRandom().setSeed(12345);

    boolean[] sub = new boolean[]{true,false,true,false,true,false,true,false};
    int[] stride = new int[]{1,1,2,2,1,1,2,2};
    int[] kernel = new int[]{2,2,2,2,3,3,3,3};
    int[] ds = new int[]{2,3,3,2,2,3,3,2};
    ConvolutionMode[] cms = new ConvolutionMode[]{Same, Same, Same, Truncate, Truncate, Truncate, Same, Truncate};


    for(int t=0; t<sub.length; t++ ){

        boolean subsampling = sub[t];
        int s = stride[t];
        int k = kernel[t];
        int d = ds[t];
        ConvolutionMode cm = cms[t];

        //Use larger input with larger dilation values (to avoid invalid config)
        int w = d * width;
        int h = d * height;

        INDArray input = Nd4j.rand(minibatchSize, w * h * inputDepth);
        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
        for (int i = 0; i < minibatchSize; i++) {
            labels.putScalar(new int[]{i, i % nOut}, 1.0);
        }

        NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
                .updater(new NoOp())
                .activation(Activation.TANH).convolutionMode(cm).list()
                .layer(new ConvolutionLayer.Builder().name("layer 0")
                        .kernelSize(k, k)
                        .stride(s, s)
                        .dilation(d, d)
                        .nIn(inputDepth).nOut(2).build());
        if (subsampling) {
            b.layer(new SubsamplingLayer.Builder()
                    .poolingType(SubsamplingLayer.PoolingType.MAX)
                    .kernelSize(k, k)
                    .stride(s, s)
                    .dilation(d, d)
                    .build());
        } else {
            b.layer(new ConvolutionLayer.Builder().nIn(2).nOut(2)
                    .kernelSize(k, k)
                    .stride(s, s)
                    .dilation(d, d)
                    .build());
        }

        MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutionalFlat(h, w, inputDepth)).build();

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

        for (int i = 0; i < net.getLayers().length; i++) {
            System.out.println("nParams, layer " + i + ": " + net.getLayer(i).numParams());
        }

        String msg = (subsampling ? "subsampling" : "conv") + " - mb=" + minibatchSize + ", k="
                + k + ", s=" + s + ", d=" + d + ", cm=" + cm;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);

        assertTrue(msg, gradOK);
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:81,代码来源:CNNGradientCheckTest.java

示例11: testRWInit

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

    for (boolean rwInit : new boolean[]{false, true}) {
        for (int i = 0; i < 3; i++) {

            NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder()
                    .weightInit(new UniformDistribution(0, 1))
                    .list();

            if(rwInit) {
                switch (i) {
                    case 0:
                        b.layer(new LSTM.Builder().nIn(10).nOut(10)
                                .weightInitRecurrent(new UniformDistribution(2, 3))
                                .build());
                        break;
                    case 1:
                        b.layer(new GravesLSTM.Builder().nIn(10).nOut(10)
                                .weightInitRecurrent(new UniformDistribution(2, 3))
                                .build());
                        break;
                    case 2:
                        b.layer(new SimpleRnn.Builder().nIn(10).nOut(10)
                                .weightInitRecurrent(new UniformDistribution(2, 3)).build());
                        break;
                    default:
                        throw new RuntimeException();
                }
            } else {
                switch (i) {
                    case 0:
                        b.layer(new LSTM.Builder().nIn(10).nOut(10).build());
                        break;
                    case 1:
                        b.layer(new GravesLSTM.Builder().nIn(10).nOut(10).build());
                        break;
                    case 2:
                        b.layer(new SimpleRnn.Builder().nIn(10).nOut(10).build());
                        break;
                    default:
                        throw new RuntimeException();
                }
            }

            MultiLayerNetwork net = new MultiLayerNetwork(b.build());
            net.init();

            INDArray rw = net.getParam("0_RW");
            double min = rw.minNumber().doubleValue();
            double max = rw.maxNumber().doubleValue();
            if(rwInit){
                assertTrue(String.valueOf(min), min >= 2.0);
                assertTrue(String.valueOf(max), max <= 3.0);
            } else {
                assertTrue(String.valueOf(min), min >= 0.0);
                assertTrue(String.valueOf(max), max <= 1.0);
            }
        }
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:62,代码来源:TestRecurrentWeightInit.java


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