0

Help me please! I'm working on a project using deeplearning4j. The MNIST example works very well but I get an error with my dataset. My data set has two outputs.

int height = 45;
int width = 800;
int channels = 1;
int rngseed = 123;
Random randNumGen = new Random(rngseed);
int batchSize = 128;
int outputNum = 2;
int numEpochs = 15;
File trainData = new File("C:/Users/JHP/Desktop/learningData/training");
File testData = new File("C:/Users/JHP/Desktop/learningData/testing");
FileSplit train = new FileSplit(trainData, NativeImageLoader.ALLOWED_FORMATS, randNumGen);
FileSplit test = new FileSplit(testData, NativeImageLoader.ALLOWED_FORMATS, randNumGen);
ParentPathLabelGenerator labelMaker = new ParentPathLabelGenerator();

ImageRecordReader recordReader = new ImageRecordReader(height, width, channels, labelMaker);
ImageRecordReader recordReader2 = new ImageRecordReader(height, width, channels, labelMaker);
recordReader.initialize(train);
recordReader2.initialize(test);

DataSetIterator dataIter = new RecordReaderDataSetIterator(recordReader, batchSize, 1, outputNum);
DataSetIterator testIter = new RecordReaderDataSetIterator(recordReader2, batchSize, 1, outputNum);

DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
scaler.fit(dataIter);
dataIter.setPreProcessor(scaler);

System.out.println("Build model....");
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
        .seed(rngseed)
        .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
        .iterations(1)
        .learningRate(0.006)
        .updater(Updater.NESTEROVS).momentum(0.9)
        .regularization(true).l2(1e-4)
        .list()
        .layer(0,   new DenseLayer.Builder()
                .nIn(height * width)
                .nOut(1000)
                .activation(Activation.RELU)
                .weightInit(WeightInit.XAVIER)
                .build()
                )
        .layer(1, newOutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD)
                .nIn(1000)
                .nOut(outputNum)
                .activation(Activation.SOFTMAX)
                .weightInit(WeightInit.XAVIER)
                .build()
                )
        .pretrain(false).backprop(true)
        .build();

MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
model.setListeners(new ScoreIterationListener(1));

System.out.println("Train model....");
for (int i = 0; i < numEpochs; i++) {
    try {
        model.fit(dataIter);
    } catch (Exception e) {
        System.out.println(e);
    }
}

error is

org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 4 array with shape [128, 1, 45, 800]

TriV
  • 5,118
  • 2
  • 10
  • 18
  • I think it is necessary to change the DataSetIterator function to another function. In the case of the MNIST example, it is like importing data into a function. **DataSetIterator mnistTrain = new MnistDataSetIterator (batchSize, true, rngseed);** I do not know what function to use. – user7887249 Apr 19 '17 at 03:18
  • @TriV TriV Thank you very much for letting me know what to improve! I did not know because I was using stack overflow for the first time. thank you very much! – user7887249 Apr 19 '17 at 03:28

1 Answers1

1

You're initializing the neural net wrong. If you look closer at every cnn example in the dl4j examples repo (hint: this is the canonical source of where you should be pulling code from, anything else will likely be invalid or out of date: https://github.com/deeplearning4j/dl4j-examples) You'll notice in all of our examples we have an inputType config: https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/convolution/LenetMnistExample.java#L114

There are various types you should be using you should never set nIn manually. Just nOut.

For mnist, we use convolutional flat and convert it in to a 4d dataset automaticall for you.

Mnist starts off as a flat vector, but a cnn only understands 3d data. We do that transition and reshape for you.

Adam Gibson
  • 3,055
  • 1
  • 10
  • 12