org.deeplearning4j.nn.conf.MultiLayerConfiguration Java Examples
The following examples show how to use
org.deeplearning4j.nn.conf.MultiLayerConfiguration.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LayerConfigTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testUpdaterAdamParamsLayerwiseOverride() { MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .updater(new Adam(1.0, 0.5, 0.5, 1e-8)) .list() .layer(0, new DenseLayer.Builder().nIn(2).nOut(2).build()) .layer(1, new DenseLayer.Builder().nIn(2).nOut(2).updater(new Adam(1.0, 0.6, 0.7, 1e-8)).build()) .build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); assertEquals(0.5, ((Adam) ((BaseLayer) conf.getConf(0).getLayer()).getIUpdater()).getBeta1(), 0.0); assertEquals(0.6, ((Adam) ((BaseLayer) conf.getConf(1).getLayer()).getIUpdater()).getBeta1(), 0.0); assertEquals(0.5, ((Adam) ((BaseLayer) conf.getConf(0).getLayer()).getIUpdater()).getBeta2(), 0.0); assertEquals(0.7, ((Adam) ((BaseLayer) conf.getConf(1).getLayer()).getIUpdater()).getBeta2(), 0.0); }
Example #2
Source File: TestSameDiffDense.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testSameDiffDenseBasic() { int nIn = 3; int nOut = 4; MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .list() .layer(new SameDiffDense.Builder().nIn(nIn).nOut(nOut).build()) .build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); Map<String, INDArray> pt1 = net.getLayer(0).paramTable(); assertNotNull(pt1); assertEquals(2, pt1.size()); assertNotNull(pt1.get(DefaultParamInitializer.WEIGHT_KEY)); assertNotNull(pt1.get(DefaultParamInitializer.BIAS_KEY)); assertArrayEquals(new long[]{nIn, nOut}, pt1.get(DefaultParamInitializer.WEIGHT_KEY).shape()); assertArrayEquals(new long[]{1, nOut}, pt1.get(DefaultParamInitializer.BIAS_KEY).shape()); }
Example #3
Source File: RegressionMathFunctions.java From dl4j-tutorials with MIT License | 6 votes |
/** Returns the network configuration, 2 hidden DenseLayers of size 50. */ private static MultiLayerConfiguration getDeepDenseLayerNetworkConfiguration() { final int numHiddenNodes = 100; return new NeuralNetConfiguration.Builder() .seed(seed) .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) .weightInit(WeightInit.XAVIER) .updater(new Nesterovs(learningRate, 0.9)) .list() .layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(numHiddenNodes) .activation(Activation.RELU).build()) .layer(1, new DenseLayer.Builder().nIn(numHiddenNodes).nOut(numHiddenNodes) .activation(Activation.RELU).build()) .layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MSE) .activation(Activation.IDENTITY) .nIn(numHiddenNodes).nOut(numOutputs).build()) .pretrain(false).backprop(true).build(); }
Example #4
Source File: ConvolutionLayerSetupTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
public MultiLayerConfiguration mnistLenet() { MultiLayerConfiguration builder = new NeuralNetConfiguration.Builder().seed(3) .optimizationAlgo(OptimizationAlgorithm.CONJUGATE_GRADIENT).list() .layer(0, new org.deeplearning4j.nn.conf.layers.ConvolutionLayer.Builder( new int[] {5, 5}).nIn(1).nOut(6).build()) .layer(1, new org.deeplearning4j.nn.conf.layers.SubsamplingLayer.Builder( new int[] {5, 5}, new int[] {2, 2}).build()) .layer(2, new org.deeplearning4j.nn.conf.layers.ConvolutionLayer.Builder( new int[] {5, 5}).nIn(1).nOut(6).build()) .layer(3, new org.deeplearning4j.nn.conf.layers.SubsamplingLayer.Builder( new int[] {5, 5}, new int[] {2, 2}).build()) .layer(4, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder( LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nIn(150) .nOut(10).build()) .build(); return builder; }
Example #5
Source File: ConvolutionLayerSetupTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testDeconv2D() { MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().list() //out = stride * (in-1) + filter - 2*pad -> 2 * (28-1) + 2 - 0 = 56 -> 56x56x3 .layer( new Deconvolution2D.Builder(2, 2) .padding(0, 0) .stride(2, 2).nIn(1).nOut(3).build()) //(56-2+2*1)/2+1 = 29 -> 29x29x3 .layer( new SubsamplingLayer.Builder().kernelSize(2, 2).padding(1, 1).stride(2, 2).build()) .layer(2, new OutputLayer.Builder().nOut(3).activation(Activation.SOFTMAX).build()) .setInputType(InputType.convolutional(28, 28, 1)); MultiLayerConfiguration conf = builder.build(); assertNotNull(conf.getInputPreProcess(2)); assertTrue(conf.getInputPreProcess(2) instanceof CnnToFeedForwardPreProcessor); CnnToFeedForwardPreProcessor proc = (CnnToFeedForwardPreProcessor) conf.getInputPreProcess(2); assertEquals(29, proc.getInputHeight()); assertEquals(29, proc.getInputWidth()); assertEquals(3, proc.getNumChannels()); assertEquals(29 * 29 * 3, ((FeedForwardLayer) conf.getConf(2).getLayer()).getNIn()); }
Example #6
Source File: TestMultiLayerSpace.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testGlobalPoolingBasic() { MultiLayerConfiguration expected = new NeuralNetConfiguration.Builder().updater(new Sgd(0.005)).seed(12345).list() .layer(0, new GravesLSTM.Builder().nIn(10).nOut(10).build()) .layer(1, new GlobalPoolingLayer.Builder().poolingType(PoolingType.SUM).pnorm(7).build()) .layer(2, new OutputLayer.Builder().lossFunction(LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(10).nOut(5).build()) .build(); MultiLayerSpace mls = new MultiLayerSpace.Builder().updater(new Sgd(0.005)).seed(12345) .addLayer(new GravesLSTMLayerSpace.Builder().nIn(10).nOut(10).build()) .addLayer(new GlobalPoolingLayerSpace.Builder().poolingType(PoolingType.SUM) .pNorm(7).build()) .addLayer(new OutputLayerSpace.Builder().lossFunction(LossFunction.MCXENT) .activation(Activation.SOFTMAX) .nIn(10).nOut(5).build()) .build(); int nParams = mls.numParameters(); assertEquals(0, nParams); MultiLayerConfiguration conf = mls.getValue(new double[0]).getMultiLayerConfiguration(); assertEquals(expected, conf); }
Example #7
Source File: TestRemoteReceiver.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test @Ignore public void testRemoteFull() throws Exception { //Use this in conjunction with startRemoteUI() MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).list() .layer(0, new DenseLayer.Builder().activation(Activation.TANH).nIn(4).nOut(4).build()) .layer(1, new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT) .activation(Activation.SOFTMAX).nIn(4).nOut(3).build()) .build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); try(RemoteUIStatsStorageRouter ssr = new RemoteUIStatsStorageRouter("http://localhost:9000")) { net.setListeners(new StatsListener(ssr), new ScoreIterationListener(1)); DataSetIterator iter = new IrisDataSetIterator(150, 150); for (int i = 0; i < 500; i++) { net.fit(iter); // Thread.sleep(100); Thread.sleep(100); } } }
Example #8
Source File: TestSimpleRnn.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testBiasInit(){ Nd4j.getRandom().setSeed(12345); int nIn = 5; int layerSize = 6; MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .updater(new NoOp()) .weightInit(WeightInit.XAVIER) .activation(Activation.TANH) .list() .layer(new SimpleRnn.Builder().nIn(nIn).nOut(layerSize).dataFormat(rnnDataFormat) .biasInit(100) .build()) .build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); INDArray bArr = net.getParam("0_b"); assertEquals(Nd4j.valueArrayOf(new long[]{1,layerSize}, 100.0f), bArr); }
Example #9
Source File: VaeReconstructionProbWithKeyFunction.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Override public VariationalAutoencoder getVaeLayer() { MultiLayerNetwork network = new MultiLayerNetwork(MultiLayerConfiguration.fromJson((String) jsonConfig.getValue())); network.init(); INDArray val = ((INDArray) params.value()).unsafeDuplication(); if (val.length() != network.numParams(false)) throw new IllegalStateException( "Network did not have same number of parameters as the broadcast set parameters"); network.setParameters(val); Layer l = network.getLayer(0); if (!(l instanceof VariationalAutoencoder)) { throw new RuntimeException( "Cannot use VaeReconstructionProbWithKeyFunction on network that doesn't have a VAE " + "layer as layer 0. Layer type: " + l.getClass()); } return (VariationalAutoencoder) l; }
Example #10
Source File: ConvolutionLayerSetupTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testSubSamplingWithPadding() { MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().list() .layer(0, new ConvolutionLayer.Builder(2, 2).padding(0, 0).stride(2, 2).nIn(1).nOut(3).build()) //(28-2+0)/2+1 = 14 .layer(1, new SubsamplingLayer.Builder().kernelSize(2, 2).padding(1, 1).stride(2, 2).build()) //(14-2+2)/2+1 = 8 -> 8x8x3 .layer(2, new OutputLayer.Builder().nOut(3).activation(Activation.SOFTMAX).build()) .setInputType(InputType.convolutional(28, 28, 1)); MultiLayerConfiguration conf = builder.build(); assertNotNull(conf.getInputPreProcess(2)); assertTrue(conf.getInputPreProcess(2) instanceof CnnToFeedForwardPreProcessor); CnnToFeedForwardPreProcessor proc = (CnnToFeedForwardPreProcessor) conf.getInputPreProcess(2); assertEquals(8, proc.getInputHeight()); assertEquals(8, proc.getInputWidth()); assertEquals(3, proc.getNumChannels()); assertEquals(8 * 8 * 3, ((FeedForwardLayer) conf.getConf(2).getLayer()).getNIn()); }
Example #11
Source File: NeuralNetworkModel.java From jstarcraft-rns with Apache License 2.0 | 6 votes |
@Override protected void doPractice() { MultiLayerConfiguration configuration = getNetworkConfiguration(); network = new MultiLayerNetwork(configuration); network.init(); for (int epocheIndex = 0; epocheIndex < epocheSize; epocheIndex++) { totalError = 0F; network.fit(inputData, inputData); totalError = (float) network.score(); if (isConverged(epocheIndex) && isConverged) { break; } currentError = totalError; } outputData = network.output(inputData); }
Example #12
Source File: ModelSerializerTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testWriteMLNModel() throws Exception { int nIn = 5; int nOut = 6; MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).l1(0.01) .l2(0.01).updater(new Sgd(0.1)).activation(Activation.TANH).weightInit(WeightInit.XAVIER).list() .layer(0, new DenseLayer.Builder().nIn(nIn).nOut(20).build()) .layer(1, new DenseLayer.Builder().nIn(20).nOut(30).build()).layer(2, new OutputLayer.Builder() .lossFunction(LossFunctions.LossFunction.MSE).nIn(30).nOut(nOut).build()) .build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); File tempFile = tempDir.newFile(); ModelSerializer.writeModel(net, tempFile, true); MultiLayerNetwork network = ModelSerializer.restoreMultiLayerNetwork(tempFile); assertEquals(network.getLayerWiseConfigurations().toJson(), net.getLayerWiseConfigurations().toJson()); assertEquals(net.params(), network.params()); assertEquals(net.getUpdater().getStateViewArray(), network.getUpdater().getStateViewArray()); }
Example #13
Source File: TestOptimizers.java From deeplearning4j with Apache License 2.0 | 5 votes |
private static MultiLayerConfiguration getMLPConfigIris(OptimizationAlgorithm oa) { MultiLayerConfiguration c = new NeuralNetConfiguration.Builder().optimizationAlgo(oa) .updater(new AdaGrad(1e-1)).seed(12345L) .list().layer(0, new DenseLayer.Builder().nIn(4).nOut(3).weightInit(WeightInit.XAVIER) .activation(Activation.RELU) .build()) .layer(1, new OutputLayer.Builder(LossFunction.MCXENT).nIn(3).nOut(3) .weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).build()) .build(); return c; }
Example #14
Source File: ImageUtils.java From AILibs with GNU Affero General Public License v3.0 | 5 votes |
/** * Returns a symmetric max pooling networking consisting only of a max pooling * layer. * * @param kernelSize Kernel size of the max pooling layer * @param stride Stride of the max pooling layer * @return Returns the constructed and initialized network */ static MultiLayerNetwork getMaxPoolNetworkSymmetricWithCustomKernelStride(final int kernelSize, final int stride) { MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(0).list() .layer(0, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX) .kernelSize(kernelSize, kernelSize).stride(stride, stride).build()) .build(); MultiLayerNetwork mln = new MultiLayerNetwork(conf); mln.init(); return mln; }
Example #15
Source File: ManualTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testCNNActivationsFrozen() throws Exception { int nChannels = 1; int outputNum = 10; int batchSize = 64; int nEpochs = 10; int seed = 123; log.info("Load data...."); DataSetIterator mnistTrain = new MnistDataSetIterator(batchSize, true, 12345); log.info("Build model...."); MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().seed(seed) .l2(0.0005) .weightInit(WeightInit.XAVIER) .updater(new Nesterovs(0.01, 0.9)).list() .layer(0, new FrozenLayer(new ConvolutionLayer.Builder(5, 5) //nIn and nOut specify depth. nIn here is the nChannels and nOut is the number of filters to be applied .nIn(nChannels).stride(1, 1).nOut(20).activation(Activation.IDENTITY).build())) .layer(1, new FrozenLayer(new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2) .stride(2, 2).build())) .layer(2, new FrozenLayer(new DenseLayer.Builder().activation(Activation.RELU).nOut(500).build())) .layer(3, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nOut(outputNum).activation(Activation.SOFTMAX).build()) .setInputType(InputType.convolutionalFlat(28, 28, nChannels)); MultiLayerConfiguration conf = builder.build(); MultiLayerNetwork model = new MultiLayerNetwork(conf); model.init(); log.info("Train model...."); model.setListeners(new ConvolutionalIterationListener(1)); for (int i = 0; i < nEpochs; i++) { model.fit(mnistTrain); } }
Example #16
Source File: TestUpdaters.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testDivisionByMinibatch1(){ //No batch norm - should be single INDArray equal to flattened gradient view MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .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).activation(Activation.SOFTMAX).build()) .build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); net.fit(Nd4j.create(1,10), Nd4j.create(1,10)); BaseMultiLayerUpdater u = (BaseMultiLayerUpdater) net.getUpdater(); List<INDArray> l = u.getGradientsForMinibatchDivision(); assertNotNull(l); assertEquals(1, l.size()); INDArray arr = l.get(0); assertEquals(3 * (10 * 10 + 10), arr.length()); assertEquals(net.getFlattenedGradients(), arr); }
Example #17
Source File: ScoreFlatMapFunction.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public Iterator<Tuple2<Integer, Double>> call(Iterator<DataSet> dataSetIterator) throws Exception { if (!dataSetIterator.hasNext()) { return Collections.singletonList(new Tuple2<>(0, 0.0)).iterator(); } DataSetIterator iter = new IteratorDataSetIterator(dataSetIterator, minibatchSize); //Does batching where appropriate MultiLayerNetwork network = new MultiLayerNetwork(MultiLayerConfiguration.fromJson(json)); network.init(); INDArray val = params.value().unsafeDuplication(); //.value() object will be shared by all executors on each machine -> OK, as params are not modified by score function if (val.length() != network.numParams(false)) throw new IllegalStateException( "Network did not have same number of parameters as the broadcast set parameters"); network.setParameters(val); List<Tuple2<Integer, Double>> out = new ArrayList<>(); while (iter.hasNext()) { DataSet ds = iter.next(); double score = network.score(ds, false); val numExamples = (int) ds.getFeatures().size(0); out.add(new Tuple2<>(numExamples, score * numExamples)); } Nd4j.getExecutioner().commit(); return out.iterator(); }
Example #18
Source File: CDAEModel.java From jstarcraft-rns with Apache License 2.0 | 5 votes |
@Override protected MultiLayerConfiguration getNetworkConfiguration() { NeuralNetConfiguration.ListBuilder factory = new NeuralNetConfiguration.Builder().seed(6) // .gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue) // .gradientNormalizationThreshold(1.0) .updater(new Nesterovs(learnRatio, momentum)).weightInit(WeightInit.XAVIER).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).l2(weightRegularization).list(); factory.layer(0, new CDAEConfiguration.Builder().nIn(inputDimension).nOut(hiddenDimension).activation(Activation.fromString(hiddenActivation)).setNumUsers(userSize).build()); factory.layer(1, new OutputLayer.Builder().nIn(hiddenDimension).nOut(inputDimension).lossFunction(LossFunctions.LossFunction.SQUARED_LOSS).activation(Activation.fromString(outputActivation)).build()); factory.pretrain(false).backprop(true); MultiLayerConfiguration configuration = factory.build(); return configuration; }
Example #19
Source File: ActivationLayerTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testActivationInheritance() { MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123) .weightInit(WeightInit.XAVIER) .activation(Activation.RATIONALTANH) .list() .layer(new DenseLayer.Builder().nIn(10).nOut(10).build()) .layer(new ActivationLayer()) .layer(new ActivationLayer.Builder().build()) .layer(new ActivationLayer.Builder().activation(Activation.ELU).build()) .layer(new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(LossFunctions.LossFunction.MCXENT) .activation(Activation.SOFTMAX).nIn(10).nOut(10).build()) .build(); MultiLayerNetwork network = new MultiLayerNetwork(conf); network.init(); assertNotNull(((ActivationLayer)network.getLayer(1).conf().getLayer()).getActivationFn()); assertTrue(((DenseLayer)network.getLayer(0).conf().getLayer()).getActivationFn() instanceof ActivationRationalTanh); assertTrue(((ActivationLayer)network.getLayer(1).conf().getLayer()).getActivationFn() instanceof ActivationRationalTanh); assertTrue(((ActivationLayer)network.getLayer(2).conf().getLayer()).getActivationFn() instanceof ActivationRationalTanh); assertTrue(((ActivationLayer)network.getLayer(3).conf().getLayer()).getActivationFn() instanceof ActivationELU); assertTrue(((OutputLayer)network.getLayer(4).conf().getLayer()).getActivationFn() instanceof ActivationSoftmax); }
Example #20
Source File: TestEarlyStoppingSpark.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testBadTuning() { //Test poor tuning (high LR): should terminate on MaxScoreIterationTerminationCondition Nd4j.getRandom().setSeed(12345); MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345) .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) .updater(new Sgd(10.0)) //Intentionally huge LR .weightInit(WeightInit.XAVIER).list() .layer(0, new OutputLayer.Builder().nIn(4).nOut(3).activation(Activation.IDENTITY) .lossFunction(LossFunctions.LossFunction.MSE).build()) .build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.setListeners(new ScoreIterationListener(5)); JavaRDD<DataSet> irisData = getIris(); EarlyStoppingModelSaver<MultiLayerNetwork> saver = new InMemoryModelSaver<>(); EarlyStoppingConfiguration<MultiLayerNetwork> esConf = new EarlyStoppingConfiguration.Builder<MultiLayerNetwork>() .epochTerminationConditions(new MaxEpochsTerminationCondition(5000)) .iterationTerminationConditions( new MaxTimeIterationTerminationCondition(2, TimeUnit.MINUTES), new MaxScoreIterationTerminationCondition(7.5)) //Initial score is ~2.5 .scoreCalculator(new SparkDataSetLossCalculator(irisData, true, sc.sc())) .modelSaver(saver).build(); IEarlyStoppingTrainer<MultiLayerNetwork> trainer = new SparkEarlyStoppingTrainer(getContext().sc(), new ParameterAveragingTrainingMaster(true, 4, 1, 150 / 4, 1, 0), esConf, net, irisData); EarlyStoppingResult result = trainer.fit(); assertTrue(result.getTotalEpochs() < 5); assertEquals(EarlyStoppingResult.TerminationReason.IterationTerminationCondition, result.getTerminationReason()); String expDetails = new MaxScoreIterationTerminationCondition(7.5).toString(); assertEquals(expDetails, result.getTerminationDetails()); }
Example #21
Source File: ModelValidatorTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
public static MultiLayerNetwork getSimpleNet(){ MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .seed(12345) .updater(new Adam(0.01)) .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(); return net; }
Example #22
Source File: LayerConfigValidationTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testNesterovsNotSetGlobal() { // Warnings only thrown Map<Integer, Double> testMomentumAfter = new HashMap<>(); testMomentumAfter.put(0, 0.1); MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().updater(new Nesterovs(1.0, new MapSchedule(ScheduleType.ITERATION, testMomentumAfter))).list() .layer(0, new DenseLayer.Builder().nIn(2).nOut(2).build()) .layer(1, new DenseLayer.Builder().nIn(2).nOut(2).build()).build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); }
Example #23
Source File: TestVAE.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testInitialization() { MultiLayerConfiguration mlc = new NeuralNetConfiguration.Builder().list() .layer(0, new org.deeplearning4j.nn.conf.layers.variational.VariationalAutoencoder.Builder() .nIn(10).nOut(5).encoderLayerSizes(12).decoderLayerSizes(13) .build()) .build(); NeuralNetConfiguration c = mlc.getConf(0); org.deeplearning4j.nn.conf.layers.variational.VariationalAutoencoder vae = (org.deeplearning4j.nn.conf.layers.variational.VariationalAutoencoder) c.getLayer(); long allParams = vae.initializer().numParams(c); // Encoder Encoder -> p(z|x) Decoder //p(x|z) int expNumParams = (10 * 12 + 12) + (12 * (2 * 5) + (2 * 5)) + (5 * 13 + 13) + (13 * (2 * 10) + (2 * 10)); assertEquals(expNumParams, allParams); MultiLayerNetwork net = new MultiLayerNetwork(mlc); net.init(); System.out.println("Exp num params: " + expNumParams); assertEquals(expNumParams, net.getLayer(0).params().length()); Map<String, INDArray> paramTable = net.getLayer(0).paramTable(); int count = 0; for (INDArray arr : paramTable.values()) { count += arr.length(); } assertEquals(expNumParams, count); assertEquals(expNumParams, net.getLayer(0).numParams()); }
Example #24
Source File: TestVAE.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testPretrainSimple() { int inputSize = 3; MultiLayerConfiguration mlc = new NeuralNetConfiguration.Builder().list() .layer(0, new org.deeplearning4j.nn.conf.layers.variational.VariationalAutoencoder.Builder() .nIn(inputSize).nOut(4).encoderLayerSizes(5).decoderLayerSizes(6).build()) .build(); NeuralNetConfiguration c = mlc.getConf(0); org.deeplearning4j.nn.conf.layers.variational.VariationalAutoencoder vae = (org.deeplearning4j.nn.conf.layers.variational.VariationalAutoencoder) c.getLayer(); long allParams = vae.initializer().numParams(c); MultiLayerNetwork net = new MultiLayerNetwork(mlc); net.init(); net.initGradientsView(); //TODO this should happen automatically Map<String, INDArray> paramTable = net.getLayer(0).paramTable(); Map<String, INDArray> gradTable = ((org.deeplearning4j.nn.layers.variational.VariationalAutoencoder) net.getLayer(0)) .getGradientViews(); assertEquals(paramTable.keySet(), gradTable.keySet()); for (String s : paramTable.keySet()) { assertEquals(paramTable.get(s).length(), gradTable.get(s).length()); assertArrayEquals(paramTable.get(s).shape(), gradTable.get(s).shape()); } System.out.println("Num params: " + net.numParams()); INDArray data = Nd4j.rand(1, inputSize); net.pretrainLayer(0, data); }
Example #25
Source File: BatchNormalizationTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testBatchNormRecurrentCnn1d() { //Simple sanity check on CNN1D and RNN layers for (boolean rnn : new boolean[]{true, false}) { MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .seed(12345) .weightInit(WeightInit.XAVIER) .convolutionMode(ConvolutionMode.Same) .list() .layer(rnn ? new LSTM.Builder().nOut(3).build() : new Convolution1DLayer.Builder().kernelSize(3).stride(1).nOut(3).build()) .layer(new BatchNormalization()) .layer(new RnnOutputLayer.Builder().nOut(3).activation(Activation.TANH).lossFunction(LossFunctions.LossFunction.MSE).build()) .setInputType(InputType.recurrent(3)) .build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); INDArray in = Nd4j.rand(new int[]{1, 3, 5}); INDArray label = Nd4j.rand(new int[]{1, 3, 5}); INDArray out = net.output(in); assertArrayEquals(new long[]{1, 3, 5}, out.shape()); net.fit(in, label); log.info("OK: {}", (rnn ? "rnn" : "cnn1d")); } }
Example #26
Source File: RegressionTest050.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void regressionTestMLP2() throws Exception { File f = Resources.asFile("regression_testing/050/050_ModelSerializer_Regression_MLP_2.zip"); MultiLayerNetwork net = ModelSerializer.restoreMultiLayerNetwork(f, true); MultiLayerConfiguration conf = net.getLayerWiseConfigurations(); assertEquals(2, conf.getConfs().size()); DenseLayer l0 = (DenseLayer) conf.getConf(0).getLayer(); assertTrue(l0.getActivationFn() instanceof ActivationLReLU); assertEquals(3, l0.getNIn()); assertEquals(4, l0.getNOut()); assertEquals(new WeightInitDistribution(new NormalDistribution(0.1, 1.2)), l0.getWeightInitFn()); assertEquals(new RmsProp(0.15, 0.96, RmsProp.DEFAULT_RMSPROP_EPSILON), l0.getIUpdater()); assertEquals(0.15, ((RmsProp)l0.getIUpdater()).getLearningRate(), 1e-6); assertEquals(new Dropout(0.6), l0.getIDropout()); assertEquals(0.1, TestUtils.getL1(l0), 1e-6); assertEquals(new WeightDecay(0.2, false), TestUtils.getWeightDecayReg(l0)); OutputLayer l1 = (OutputLayer) conf.getConf(1).getLayer(); assertEquals("identity", l1.getActivationFn().toString()); assertTrue(l1.getLossFn() instanceof LossMSE); assertEquals(4, l1.getNIn()); assertEquals(5, l1.getNOut()); assertEquals(new WeightInitDistribution(new NormalDistribution(0.1, 1.2)), l0.getWeightInitFn()); assertEquals(new RmsProp(0.15, 0.96, RmsProp.DEFAULT_RMSPROP_EPSILON), l1.getIUpdater()); assertEquals(0.15, ((RmsProp)l1.getIUpdater()).getLearningRate(), 1e-6); assertEquals(new Dropout(0.6), l1.getIDropout()); assertEquals(0.1, TestUtils.getL1(l1), 1e-6); assertEquals(new WeightDecay(0.2, false), TestUtils.getWeightDecayReg(l1)); int numParams = (int)net.numParams(); assertEquals(Nd4j.linspace(1, numParams, numParams, Nd4j.dataType()).reshape(1,numParams), net.params()); int updaterSize = (int) new RmsProp().stateSize(numParams); assertEquals(Nd4j.linspace(1, updaterSize, updaterSize, Nd4j.dataType()).reshape(1,numParams), net.getUpdater().getStateViewArray()); }
Example #27
Source File: TestSparkMultiLayerParameterAveraging.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test @Ignore //Ignored 2019/04/09 - low priority: https://github.com/deeplearning4j/deeplearning4j/issues/6656 public void testVaePretrainSimple() { //Simple sanity check on pretraining int nIn = 8; Nd4j.getRandom().setSeed(12345); MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).updater(new RmsProp()) .weightInit(WeightInit.XAVIER).list() .layer(0, new VariationalAutoencoder.Builder().nIn(8).nOut(10).encoderLayerSizes(12) .decoderLayerSizes(13).reconstructionDistribution( new GaussianReconstructionDistribution(Activation.IDENTITY)) .build()) .build(); //Do training on Spark with one executor, for 3 separate minibatches int rddDataSetNumExamples = 10; int totalAveragings = 5; int averagingFrequency = 3; ParameterAveragingTrainingMaster tm = new ParameterAveragingTrainingMaster.Builder(rddDataSetNumExamples) .averagingFrequency(averagingFrequency).batchSizePerWorker(rddDataSetNumExamples) .saveUpdater(true).workerPrefetchNumBatches(0).build(); Nd4j.getRandom().setSeed(12345); SparkDl4jMultiLayer sparkNet = new SparkDl4jMultiLayer(sc, conf.clone(), tm); List<DataSet> trainData = new ArrayList<>(); int nDataSets = numExecutors() * totalAveragings * averagingFrequency; for (int i = 0; i < nDataSets; i++) { trainData.add(new DataSet(Nd4j.rand(rddDataSetNumExamples, nIn), null)); } JavaRDD<DataSet> data = sc.parallelize(trainData); sparkNet.fit(data); }
Example #28
Source File: RegressionTest080.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void regressionTestLSTM1() throws Exception { File f = Resources.asFile("regression_testing/080/080_ModelSerializer_Regression_LSTM_1.zip"); MultiLayerNetwork net = ModelSerializer.restoreMultiLayerNetwork(f, true); MultiLayerConfiguration conf = net.getLayerWiseConfigurations(); assertEquals(3, conf.getConfs().size()); GravesLSTM l0 = (GravesLSTM) conf.getConf(0).getLayer(); assertTrue(l0.getActivationFn() instanceof ActivationTanH); assertEquals(3, l0.getNIn()); assertEquals(4, l0.getNOut()); assertEquals(GradientNormalization.ClipElementWiseAbsoluteValue, l0.getGradientNormalization()); assertEquals(1.5, l0.getGradientNormalizationThreshold(), 1e-5); GravesBidirectionalLSTM l1 = (GravesBidirectionalLSTM) conf.getConf(1).getLayer(); assertTrue(l1.getActivationFn() instanceof ActivationSoftSign); assertEquals(4, l1.getNIn()); assertEquals(4, l1.getNOut()); assertEquals(GradientNormalization.ClipElementWiseAbsoluteValue, l1.getGradientNormalization()); assertEquals(1.5, l1.getGradientNormalizationThreshold(), 1e-5); RnnOutputLayer l2 = (RnnOutputLayer) conf.getConf(2).getLayer(); assertEquals(4, l2.getNIn()); assertEquals(5, l2.getNOut()); assertTrue(l2.getActivationFn() instanceof ActivationSoftmax); assertTrue(l2.getLossFn() instanceof LossMCXENT); }
Example #29
Source File: NetBroadcastTuple.java From deeplearning4j with Apache License 2.0 | 5 votes |
public NetBroadcastTuple(MultiLayerConfiguration configuration, ComputationGraphConfiguration graphConfiguration, INDArray parameters, INDArray updaterState, AtomicInteger counter) { this.configuration = configuration; this.graphConfiguration = graphConfiguration; this.parameters = parameters; this.updaterState = updaterState; this.counter = counter; }
Example #30
Source File: RegressionTest060.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void regressionTestLSTM1() throws Exception { File f = Resources.asFile("regression_testing/060/060_ModelSerializer_Regression_LSTM_1.zip"); MultiLayerNetwork net = ModelSerializer.restoreMultiLayerNetwork(f, true); MultiLayerConfiguration conf = net.getLayerWiseConfigurations(); assertEquals(3, conf.getConfs().size()); GravesLSTM l0 = (GravesLSTM) conf.getConf(0).getLayer(); assertEquals("tanh", l0.getActivationFn().toString()); assertEquals(3, l0.getNIn()); assertEquals(4, l0.getNOut()); assertEquals(GradientNormalization.ClipElementWiseAbsoluteValue, l0.getGradientNormalization()); assertEquals(1.5, l0.getGradientNormalizationThreshold(), 1e-5); GravesBidirectionalLSTM l1 = (GravesBidirectionalLSTM) conf.getConf(1).getLayer(); assertEquals("softsign", l1.getActivationFn().toString()); assertEquals(4, l1.getNIn()); assertEquals(4, l1.getNOut()); assertEquals(GradientNormalization.ClipElementWiseAbsoluteValue, l1.getGradientNormalization()); assertEquals(1.5, l1.getGradientNormalizationThreshold(), 1e-5); RnnOutputLayer l2 = (RnnOutputLayer) conf.getConf(2).getLayer(); assertEquals(4, l2.getNIn()); assertEquals(5, l2.getNOut()); assertEquals("softmax", l2.getActivationFn().toString()); assertTrue(l2.getLossFn() instanceof LossMCXENT); }