Why use MultiLayerNetwork?

The MultiLayerNetwork class is the simplest network configuration API available in Eclipse Deeplearning4j. This class is useful for beginners or users who do not need a complex and branched network graph.

You will not want to use MultiLayerNetwork configuration if you are creating complex loss functions, using graph vertices, or doing advanced training such as a triplet network. This includes popular complex networks such as InceptionV4.

Usage

The example below shows how to build a simple linear classifier using DenseLayer (a basic multiperceptron layer).

  1. MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
  2. .seed(seed)
  3. .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
  4. .learningRate(learningRate)
  5. .updater(Updater.NESTEROVS).momentum(0.9)
  6. .list()
  7. .layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(numHiddenNodes)
  8. .weightInit(WeightInit.XAVIER)
  9. .activation("relu")
  10. .build())
  11. .layer(1, new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD)
  12. .weightInit(WeightInit.XAVIER)
  13. .activation("softmax").weightInit(WeightInit.XAVIER)
  14. .nIn(numHiddenNodes).nOut(numOutputs).build())
  15. .pretrain(false).backprop(true).build();

You can also create convolutional configurations:

  1. MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder()
  2. .seed(seed)
  3. .regularization(true).l2(0.0005)
  4. .learningRate(0.01)//.biasLearningRate(0.02)
  5. //.learningRateDecayPolicy(LearningRatePolicy.Inverse).lrPolicyDecayRate(0.001).lrPolicyPower(0.75)
  6. .weightInit(WeightInit.XAVIER)
  7. .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
  8. .updater(Updater.NESTEROVS).momentum(0.9)
  9. .list()
  10. .layer(0, new ConvolutionLayer.Builder(5, 5)
  11. //nIn and nOut specify depth. nIn here is the nChannels and nOut is the number of filters to be applied
  12. .nIn(nChannels)
  13. .stride(1, 1)
  14. .nOut(20)
  15. .activation("identity")
  16. .build())
  17. .layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
  18. .kernelSize(2,2)
  19. .stride(2,2)
  20. .build())
  21. .layer(2, new ConvolutionLayer.Builder(5, 5)
  22. //Note that nIn need not be specified in later layers
  23. .stride(1, 1)
  24. .nOut(50)
  25. .activation("identity")
  26. .build())
  27. .layer(3, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
  28. .kernelSize(2,2)
  29. .stride(2,2)
  30. .build())
  31. .layer(4, new DenseLayer.Builder().activation("relu")
  32. .nOut(500).build())
  33. .layer(5, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
  34. .nOut(outputNum)
  35. .activation("softmax")
  36. .build())
  37. .backprop(true).pretrain(false);

API