Created
December 30, 2015 02:00
-
-
Save sato-cloudian/91f3933f84de875cec05 to your computer and use it in GitHub Desktop.
MyCNNMnistExample experimentation 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.deeplearning4j.examples.convolution; | |
| import org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator; | |
| import org.deeplearning4j.eval.Evaluation; | |
| import org.deeplearning4j.nn.api.OptimizationAlgorithm; | |
| import org.deeplearning4j.nn.conf.GradientNormalization; | |
| import org.deeplearning4j.nn.conf.MultiLayerConfiguration; | |
| import org.deeplearning4j.nn.conf.NeuralNetConfiguration; | |
| import org.deeplearning4j.nn.conf.Updater; | |
| import org.deeplearning4j.nn.conf.layers.ConvolutionLayer; | |
| import org.deeplearning4j.nn.conf.layers.DenseLayer; | |
| import org.deeplearning4j.nn.conf.layers.OutputLayer; | |
| import org.deeplearning4j.nn.conf.layers.SubsamplingLayer; | |
| import org.deeplearning4j.nn.conf.layers.setup.ConvolutionLayerSetup; | |
| import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; | |
| import org.deeplearning4j.nn.weights.WeightInit; | |
| import org.deeplearning4j.optimize.api.IterationListener; | |
| import org.deeplearning4j.optimize.listeners.ScoreIterationListener; | |
| import org.deeplearning4j.ui.weights.HistogramIterationListener; | |
| import org.nd4j.linalg.api.ndarray.INDArray; | |
| import org.nd4j.linalg.dataset.DataSet; | |
| import org.nd4j.linalg.dataset.SplitTestAndTrain; | |
| import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; | |
| import org.nd4j.linalg.lossfunctions.LossFunctions; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import java.util.Random; | |
| /** | |
| * Created by willow on 5/11/15. | |
| */ | |
| public class MyCNNMnistExample { | |
| private static final Logger log = LoggerFactory.getLogger(MyCNNMnistExample.class); | |
| public static void main(String[] args) throws Exception { | |
| int numRows = 28; | |
| int numColumns = 28; | |
| int nChannels = 1; | |
| int outputNum = 10; | |
| int numSamples = 10000; | |
| int batchSize = 100; | |
| int iterations = 1; | |
| int splitTrainNum = (int) (batchSize*.8); | |
| int seed = 123; | |
| int listenerFreq = Math.max(iterations/10, 1); | |
| DataSet mnist; | |
| SplitTestAndTrain trainTest; | |
| DataSet trainInput; | |
| List<INDArray> testInput = new ArrayList<>(); | |
| List<INDArray> testLabels = new ArrayList<>(); | |
| log.info("Load data...."); | |
| DataSetIterator mnistIter = new MnistDataSetIterator(batchSize,numSamples, true); | |
| log.info("Build model...."); | |
| MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder() | |
| .seed(seed) | |
| .iterations(iterations) | |
| //.gradientNormalization(GradientNormalization.RenormalizeL2PerLayer) | |
| .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) | |
| .learningRate(0.01) // default | |
| .regularization(true) | |
| .list(4) | |
| .layer(0, new ConvolutionLayer.Builder(4, 4) // 28*28*1 => 14*14*10 | |
| .nIn(nChannels) | |
| .nOut(10) | |
| .padding(2, 2) | |
| .stride(2, 2) | |
| .weightInit(WeightInit.RELU) | |
| .activation("relu") | |
| .build()) | |
| /*.layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX, new int[] {2,2}) // 28*28*10 => 14*14*10 | |
| .stride(2, 2) | |
| .build())*/ | |
| .layer(1, new ConvolutionLayer.Builder(4, 4) // 14*14*10 => 7*7*20 | |
| .nIn(10) | |
| .nOut(20) | |
| .padding(2, 2) | |
| .stride(2, 2) | |
| .weightInit(WeightInit.RELU) | |
| .activation("relu") | |
| .build()) | |
| /*.layer(3, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX, new int[] {2,2}) // 14*14*20 => 7*7*20 | |
| .stride(2, 2) | |
| .build())*/ | |
| .layer(2, new DenseLayer.Builder().activation("relu") | |
| .nOut(200).build()) | |
| .layer(3, new OutputLayer.Builder(LossFunctions.LossFunction.RMSE_XENT) | |
| .nOut(outputNum) | |
| .weightInit(WeightInit.RELU) | |
| .activation("softmax") | |
| .updater(Updater.SGD) | |
| .build()) | |
| .backprop(true).pretrain(false); | |
| new ConvolutionLayerSetup(builder,numRows,numColumns,nChannels); | |
| MultiLayerConfiguration conf = builder.build(); | |
| MultiLayerNetwork model = new MultiLayerNetwork(conf); | |
| model.init(); | |
| log.info("Train model...."); | |
| model.setListeners(Arrays.asList((IterationListener) new ScoreIterationListener(listenerFreq), new HistogramIterationListener(listenerFreq))); | |
| while(mnistIter.hasNext()) { | |
| mnist = mnistIter.next(); | |
| trainTest = mnist.splitTestAndTrain(splitTrainNum, new Random(seed)); // train set that is the result | |
| trainInput = trainTest.getTrain(); // get feature matrix and labels for training | |
| testInput.add(trainTest.getTest().getFeatureMatrix()); | |
| testLabels.add(trainTest.getTest().getLabels()); | |
| model.fit(trainInput); | |
| } | |
| log.info("Evaluate weights...."); | |
| log.info("Evaluate model...."); | |
| Evaluation eval = new Evaluation(outputNum); | |
| for(int i = 0; i < testInput.size(); i++) { | |
| INDArray output = model.output(testInput.get(i)); | |
| eval.eval(testLabels.get(i), output); | |
| } | |
| INDArray output = model.output(testInput.get(0)); | |
| eval.eval(testLabels.get(0), output); | |
| log.info(eval.stats()); | |
| log.info("****************Example finished********************"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Examples labeled as 0 classified by model as 0: 193 times | |
| Examples labeled as 0 classified by model as 6: 5 times | |
| Examples labeled as 0 classified by model as 8: 4 times | |
| Examples labeled as 1 classified by model as 1: 209 times | |
| Examples labeled as 1 classified by model as 2: 1 times | |
| Examples labeled as 1 classified by model as 8: 1 times | |
| Examples labeled as 2 classified by model as 0: 7 times | |
| Examples labeled as 2 classified by model as 1: 4 times | |
| Examples labeled as 2 classified by model as 2: 159 times | |
| Examples labeled as 2 classified by model as 3: 2 times | |
| Examples labeled as 2 classified by model as 4: 4 times | |
| Examples labeled as 2 classified by model as 6: 6 times | |
| Examples labeled as 2 classified by model as 7: 1 times | |
| Examples labeled as 2 classified by model as 8: 3 times | |
| Examples labeled as 2 classified by model as 9: 2 times | |
| Examples labeled as 3 classified by model as 0: 5 times | |
| Examples labeled as 3 classified by model as 1: 12 times | |
| Examples labeled as 3 classified by model as 2: 8 times | |
| Examples labeled as 3 classified by model as 3: 166 times | |
| Examples labeled as 3 classified by model as 5: 6 times | |
| Examples labeled as 3 classified by model as 6: 3 times | |
| Examples labeled as 3 classified by model as 7: 5 times | |
| Examples labeled as 3 classified by model as 8: 6 times | |
| Examples labeled as 3 classified by model as 9: 6 times | |
| Examples labeled as 4 classified by model as 1: 7 times | |
| Examples labeled as 4 classified by model as 2: 2 times | |
| Examples labeled as 4 classified by model as 4: 169 times | |
| Examples labeled as 4 classified by model as 5: 1 times | |
| Examples labeled as 4 classified by model as 6: 9 times | |
| Examples labeled as 4 classified by model as 8: 5 times | |
| Examples labeled as 4 classified by model as 9: 14 times | |
| Examples labeled as 5 classified by model as 0: 5 times | |
| Examples labeled as 5 classified by model as 1: 6 times | |
| Examples labeled as 5 classified by model as 3: 11 times | |
| Examples labeled as 5 classified by model as 4: 1 times | |
| Examples labeled as 5 classified by model as 5: 120 times | |
| Examples labeled as 5 classified by model as 6: 7 times | |
| Examples labeled as 5 classified by model as 7: 2 times | |
| Examples labeled as 5 classified by model as 8: 8 times | |
| Examples labeled as 5 classified by model as 9: 2 times | |
| Examples labeled as 6 classified by model as 0: 2 times | |
| Examples labeled as 6 classified by model as 1: 2 times | |
| Examples labeled as 6 classified by model as 2: 1 times | |
| Examples labeled as 6 classified by model as 4: 1 times | |
| Examples labeled as 6 classified by model as 5: 2 times | |
| Examples labeled as 6 classified by model as 6: 212 times | |
| Examples labeled as 6 classified by model as 8: 1 times | |
| Examples labeled as 7 classified by model as 0: 6 times | |
| Examples labeled as 7 classified by model as 1: 10 times | |
| Examples labeled as 7 classified by model as 2: 3 times | |
| Examples labeled as 7 classified by model as 4: 3 times | |
| Examples labeled as 7 classified by model as 5: 1 times | |
| Examples labeled as 7 classified by model as 6: 2 times | |
| Examples labeled as 7 classified by model as 7: 186 times | |
| Examples labeled as 7 classified by model as 8: 3 times | |
| Examples labeled as 7 classified by model as 9: 11 times | |
| Examples labeled as 8 classified by model as 0: 7 times | |
| Examples labeled as 8 classified by model as 1: 10 times | |
| Examples labeled as 8 classified by model as 2: 3 times | |
| Examples labeled as 8 classified by model as 3: 6 times | |
| Examples labeled as 8 classified by model as 5: 3 times | |
| Examples labeled as 8 classified by model as 6: 5 times | |
| Examples labeled as 8 classified by model as 7: 1 times | |
| Examples labeled as 8 classified by model as 8: 157 times | |
| Examples labeled as 8 classified by model as 9: 6 times | |
| Examples labeled as 9 classified by model as 0: 3 times | |
| Examples labeled as 9 classified by model as 1: 5 times | |
| Examples labeled as 9 classified by model as 2: 2 times | |
| Examples labeled as 9 classified by model as 3: 1 times | |
| Examples labeled as 9 classified by model as 4: 3 times | |
| Examples labeled as 9 classified by model as 6: 2 times | |
| Examples labeled as 9 classified by model as 7: 7 times | |
| Examples labeled as 9 classified by model as 8: 7 times | |
| Examples labeled as 9 classified by model as 9: 159 times | |
| ==========================Scores======================================== | |
| Accuracy: 0.8564 | |
| Precision: 0.8617 | |
| Recall: 0.8534 | |
| F1 Score: 0.8575500676538873 | |
| =========================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment