0

I'm using deeplearning4j and I keep getting this error. I'm very very new to dl4j and AI in general. I'm trying to predict the next value given the 10 previous values. I am using an LSTM. I'm pretty sure I might have to use masking but I'm clueless and the dl4j community is kinda small. This is very tedious. Here's my code: // I previously normalized my data, as it ranges from about 3500-6500

 DataSetIterator trainData = new SequenceRecordReaderDataSetIterator(trainFeatures, trainLabels, miniBatchSize,
            1, true, SequenceRecordReaderDataSetIterator.AlignmentMode.EQUAL_LENGTH);
 DataSetIterator testData = new SequenceRecordReaderDataSetIterator(testFeatures, testLabels, miniBatchSize,
            1, true, SequenceRecordReaderDataSetIterator.AlignmentMode.EQUAL_LENGTH);

 MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(123)
            .weightInit(WeightInit.XAVIER)
            .updater(new Adam())
            .list()
            .layer(new LSTM.Builder().activation(Activation.TANH).nIn(1).nOut(10).build())
            .layer(new OutputLayer.Builder(LossFunctions.LossFunction.MSE)
                .activation(Activation.RELU).nIn(10).nOut(1).build())
            .build();

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();

This is the Error:

Caused by: java.lang.IllegalArgumentException: Labels and preOutput must have equal shapes: got shapes [5, 1, 1] vs [50, 1]

My data consists of single column time-series CSV files, each a sequence of 10. Labels are single values representing the very next value.

Heres my trainFeatures and trainLabels declaration

SequenceRecordReader trainFeatures = new CSVSequenceRecordReader(numLinesToSkip, delimiter);
        trainFeatures.initialize(new NumberedFileInputSplit(testAndTrainFeatures + "%d.csv",0,800));
        SequenceRecordReader trainLabels = new CSVSequenceRecordReader(numLinesToSkip, delimiter);
        trainLabels.initialize(new NumberedFileInputSplit(testAndTrainLabels + "%d.csv", 0, 800));
  • Ideally, you would ask questions about dl4j on its community site as it allows for a better back and forth than stackoverflow allows: https://community.konduit.ai/ Anyway, can you add the code that shows how you have defined `trainFeatures` and `trainLabels`? And can you add the `.setInputType(InputType.Recurrent(1))` configuration option, so it sets up all the necessary reshaping preprocessors (you also don't need the `nIn` configuration in that case)? – Paul Dubs Apr 14 '20 at 09:09
  • @PaulDubs Hey I added the declarations. I also added the .setInputType(...) and im still getting the same error :/ – user13308545 Apr 14 '20 at 21:58

1 Answers1

1

The problem you are facing is that you expect that your config means that you get the last timestep from the LSTM be the input for your dense layer. Instead all outputs of your LSTM are merged and fed into the dense layer.

To fix that, you should wrap the LSTM layer in a LastTimestep(...) so only what you expect actually happens.

However, you will run into a second problem then, and that is that your labels are treated as sequences, because that is how you load them. In order to make your labels non-sequential, you will have to apply a preprocessor on your dataset iterator like this:

trainData.setPreProcessor(new LabelLastTimeStepPreProcessor());

If after applying those changes you still have problems, feel free to ask questions on the community forums, where you will likely get an answer quicker.

Paul Dubs
  • 798
  • 4
  • 8