How it works...

In step 1, we have used MnistDataSetIterator to extract and load MNIST data in one place. DL4J comes with this specialized iterator to load MNIST data without having to worry about downloading the data on your own. You might notice that MNIST data on the official website follows the ubyte format. This is certainly not the desired format, and we need to extract all the images separately to load them properly on the neural network.

Therefore, it is very convenient to have an MNIST iterator implementation such as MnistDataSetIterator in DL4J. It simplifies the typical task of handling MNIST data in the ubyte format. MNIST data has a total of 60,000 training digits, 10,000 test digits, and 10 labels. Digit images have a dimension of 28 x 28, the shape of the data is in a flattened format: [minibatch, 784]. MnistDataSetIterator internally uses the MnistDataFetcher and MnistManager classes to fetch the MNIST data and load them into the proper format. In step 1, binarizetrue or false indicates whether to binarize the MNIST data.

Note that in step 2, numHoldOut indicates the number of samples to be held for training. If miniBatchSize is 100 and numHoldOut is 80, then the remaining 20 samples are meant for testing and evaluation. We can use DataSetIteratorSplitter instead of SplitTestAndTrain for splitting of data, as mentioned in step 2. 

In step 3, we created lists to maintain the features and labels with respect to training and testing. We need them for the training and evaluation stages, respectively. We also created a list to store labels from the test set to map the outliers with labels during the test and evaluation phases. These lists are populated once in every occurrence of a batch. For example, in the case of featuresTrain or featuresTest, a batch of features (after data splitting) is represented by an INDArray item. We have also used an argMax() function from ND4J. This converts the labels array into a one-dimensional array. MNIST labels from 0 to 9 effectively need just one-dimensional space for representation. 

In the following code, 1 denotes the dimension:

Nd4j.argMax(dsTest.getLabels(),1);

Also, note that we use the labels for mapping outliers to labels and not for training.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset