In [6]:
import $ivy.`org.deeplearning4j:deeplearning4j-core:0.7.2`
import $ivy.`org.nd4j:nd4j-native-platform:0.7.2`
import org.nd4j.linalg.activations.Activation
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator
import org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator
import org.deeplearning4j.eval.Evaluation
import org.deeplearning4j.nn.api.OptimizationAlgorithm
import org.deeplearning4j.nn.conf.MultiLayerConfiguration
import org.deeplearning4j.nn.conf.NeuralNetConfiguration
import org.deeplearning4j.nn.conf.Updater
import org.deeplearning4j.nn.conf.layers.DenseLayer
import org.deeplearning4j.nn.conf.layers.OutputLayer
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork
import org.deeplearning4j.nn.weights.WeightInit
import org.deeplearning4j.optimize.listeners.ScoreIterationListener
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.dataset.DataSet
import org.nd4j.linalg.lossfunctions.LossFunctions.LossFunction
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Out[6]:
In [2]:
//number of rows and columns in the input pictures
val numRows = 28;
val numColumns = 28;
val outputNum = 10; // number of output classes
val batchSize = 64; // batch size for each epoch
val rngSeed = 123; // random number seed for reproducibility
val numEpochs = 15; // number of epochs to perform
val rate = 0.0015; // learning rate
//Get the DataSetIterators:
val mnistTrain = new MnistDataSetIterator(batchSize, true, rngSeed);
val mnistTest = new MnistDataSetIterator(batchSize, false, rngSeed);
Out[2]:
Create MLP model:
In [3]:
val builder = new NeuralNetConfiguration.Builder()
builder
.seed(rngSeed) //include a random seed for reproducibility
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) // use stochastic gradient descent as an optimization algorithm
.iterations(1)
.activation(Activation.RELU)
.weightInit(WeightInit.XAVIER)
.learningRate(rate) //specify the learning rate
.updater(Updater.NESTEROVS).momentum(0.98) //specify the rate of change of the learning rate.
.regularization(true).l2(rate * 0.005) // regularize learning model
val lstBuilder = builder.list()
lstBuilder.layer(0, new DenseLayer.Builder().nIn(numRows * numColumns).nOut(500).build())
lstBuilder.layer(1, new DenseLayer.Builder().nIn(500).nOut(100).build())
lstBuilder.layer(2, new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD)
.activation(Activation.SOFTMAX)
.nIn(100)
.nOut(outputNum)
.build())
lstBuilder.pretrain(false)
lstBuilder.backprop(true)
val conf = lstBuilder.build()
val model = new MultiLayerNetwork(conf)
model.init()
model.setListeners(new ScoreIterationListener(5)) //print the score with every iteration
Out[3]:
Train model:
In [4]:
println("Train model....");
(0 to numEpochs).foreach{i =>
println("Epoch " + i)
model.fit(mnistTrain)
}
Evaluate model:
In [5]:
val eval = new Evaluation(outputNum) //create an evaluation object with 10 possible classes
while(mnistTest.hasNext()){
val next = mnistTest.next()
val output = model.output(next.getFeatureMatrix()) //get the networks prediction
eval.eval(next.getLabels(), output) //check the prediction against the true class
}
println(eval.stats());
println("****************Example finished********************");
Out[5]:
In [ ]: