# Incorrect regression output data

**URL:** <https://community.konduit.ai/t/incorrect-regression-output-data/3252>\
**Category:** DL4J\
**Created:** [September 19, 2024, 3:16am UTC](https://community.konduit.ai/t/incorrect-regression-output-data/3252 "2024-09-19T03:16:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![st331man](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@st331man](https://community.konduit.ai/u/st331man)\
**Post date:** [September 19, 2024, 3:16am UTC](https://community.konduit.ai/t/incorrect-regression-output-data/3252/1 "2024-09-19T03:16:27Z")

</div>

Hello! Please tell me what the problem might be.  
I have a recurrent network for regression based on a sequence of 100 time steps.  
An example of one step in the sequence:  
`[98.99920226267315, 98.74175067082459, 99.00645442019002, 98.69461164696499, 183762.0]`  
The network should predict the next time step, so I specify one step of the sequence as the label.  
After applying the revert with the normalizer

```auto
INDArray timeSeriesOutput = net.output(finalData);
normalizer.revertFeatures(timeSeriesOutput);
normalizer.revertLabels(timeSeriesOutput);

```

I get various data like this:  
`[827.3333, 882.7206, 846.4208, 873.9750, 3.9037e6]`  
Which doesn’t even closely resemble the input of the 4 columns around the number 100.  
Moreover, depending on the number of epochs, different numbers are always returned, sometimes exceeding the predicted values by several times. I have tried various configurations of the output layer, but the output data has never even been close to 100.  
Can you suggest what I might be doing wrong?

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 19, 2024, 4:04am UTC](https://community.konduit.ai/t/incorrect-regression-output-data/3252/2 "2024-09-19T04:04:18Z")

</div>

@st331man yeah those numbers look weird. Can you setup a standalone example that’s easier to look at? That usually indicates that the internal distribution statistics are off somehow.

It also could mean your network wasn’t very accurate though.

---

<div class="post-metadata">

**Author:** ![st331man](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@st331man](https://community.konduit.ai/u/st331man)\
**Post date:** [September 19, 2024, 5:28pm UTC](https://community.konduit.ai/t/incorrect-regression-output-data/3252/3 "2024-09-19T17:28:21Z")

</div>

@agibsonccc My code looks like this:

```auto
public class RegressionExperimental_ex {
    private static final String NEURAL_NAME = "data-d86660f4";
    private static final String DATASET_NAME = "/regression-datasets/" + NEURAL_NAME;
    public static File datasetName = new File(Configs.baseDir, DATASET_NAME + File.separator);
    private static File featuresDir = new File(datasetName, "features");
    private static File labelsDir = new File(datasetName, "labels");

    private static final int EPOCHS = 15;

    public static void main(String[] args) throws Exception {
        var files = Files.list(featuresDir.toPath()).collect(Collectors.toList());
        int filesSize = files.size();
        int eightyPrecents = (int) (filesSize * 0.80);

        //Train data
        SequenceRecordReader trainFeatures = new CSVSequenceRecordReader(0, ";");
        trainFeatures.initialize(new NumberedFileInputSplit(featuresDir.getAbsolutePath() + "/%d.csv", 1, eightyPrecents));
        SequenceRecordReader trainLabels = new CSVSequenceRecordReader(0, ";");
        trainLabels.initialize(new NumberedFileInputSplit(labelsDir.getAbsolutePath() + "/%d.csv", 1, eightyPrecents)); //17999

        boolean regression = true;
        int miniBatchSize = 30;
        int numLabelClasses = 1;
        SequenceRecordReaderDataSetIterator trainData = new SequenceRecordReaderDataSetIterator(trainFeatures, trainLabels, miniBatchSize, numLabelClasses,
                regression, SequenceRecordReaderDataSetIterator.AlignmentMode.ALIGN_END);

        //Normalize data
        NormalizerStandardize normalizer = new NormalizerStandardize();
        normalizer.fit(trainData);
        trainData.setPreProcessor(normalizer);
        trainData.reset();

        SequenceRecordReader testFeatures = new CSVSequenceRecordReader(0, ";");
        testFeatures.initialize(new NumberedFileInputSplit(featuresDir.getAbsolutePath() + "/%d.csv", eightyPrecents + 1, filesSize - 1)); //18000 - 21640
        SequenceRecordReader testLabels = new CSVSequenceRecordReader(0, ";");
        testLabels.initialize(new NumberedFileInputSplit(labelsDir.getAbsolutePath() + "/%d.csv", eightyPrecents + 1, filesSize - 1));

        SequenceRecordReaderDataSetIterator testData = new SequenceRecordReaderDataSetIterator(testFeatures, testLabels, miniBatchSize, numLabelClasses,
                regression, SequenceRecordReaderDataSetIterator.AlignmentMode.ALIGN_END);
        testData.setPreProcessor(normalizer); 

        // -- Final eval --
        SequenceRecordReader finalFeatures = new CSVSequenceRecordReader(0, ";");
        finalFeatures.initialize(new NumberedFileInputSplit(featuresDir.getAbsolutePath() + "/%d.csv", filesSize, filesSize));
        SequenceRecordReader finalLabels = new CSVSequenceRecordReader(0, ";");
        finalLabels.initialize(new NumberedFileInputSplit(labelsDir.getAbsolutePath() + "/%d.csv", filesSize, filesSize));

        SequenceRecordReaderDataSetIterator finalData = new SequenceRecordReaderDataSetIterator(finalFeatures, finalLabels, miniBatchSize, numLabelClasses,
                regression, SequenceRecordReaderDataSetIterator.AlignmentMode.ALIGN_END);
        finalData.setPreProcessor(normalizer);

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .seed(123)
                .weightInit(WeightInit.XAVIER)
                .updater(new Nadam())
                .list()
                .layer(new LSTM.Builder()
                        .activation(Activation.TANH)
                        .nIn(5)
                        .nOut(30)
                        .build())
                .layer(new LSTM.Builder()
                        .activation(Activation.TANH)
                        .nIn(30)
                        .nOut(14)
                        .build())
                .layer(new RnnOutputLayer.Builder(LossFunctions.LossFunction.MSE)
                        .activation(Activation.IDENTITY)
                        .nIn(30)
                        .nOut(5)
                        .build())
                .build();

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();
        System.out.printf("Started: miniBatchSize: %s, epohs: %s %n", miniBatchSize, EPOCHS);
        for (int i = 0; i < EPOCHS; i++) {
            long time = System.nanoTime();
            net.fit(trainData);
            var eval = net.evaluateRegression(testData);
            System.out.println(eval);
            System.out.println(eval.stats());

            time = System.nanoTime() - time;
            System.out.println(DATASET_NAME + " Time for epoch № " + (i + 1) + ": " + SECONDS.convert(time, TimeUnit.NANOSECONDS) + " sec");
        }
        System.out.println("Final:");
        RegressionEvaluation regEval = net.evaluateRegression(trainData);
        System.out.println(regEval);
        System.out.println("---------->");
        INDArray timeSeriesOutput = net.output(finalData);
        normalizer.revertFeatures(timeSeriesOutput);
        normalizer.revertLabels(timeSeriesOutput);
        System.out.println(timeSeriesOutput);
        long timeSeriesLength = timeSeriesOutput.size(2);
        INDArray lastTimeStepProbabilities = timeSeriesOutput.get(NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.point(timeSeriesLength - 1));
        System.out.println("Result: " + lastTimeStepProbabilities);
        double[] results = lastTimeStepProbabilities.toDoubleVector();
        System.out.println("Double arr: " + Arrays.toString(results));
        System.out.println("== End ==");
    }

```

And my dataset: [data-d86660f4.zip - Google Drive](https://drive.google.com/file/d/1EDQHrYm2hXi11AQfChNgfjoeN8cF9mWC/view?usp=drive_link)

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 27, 2024, 11:49am UTC](https://community.konduit.ai/t/incorrect-regression-output-data/3252/4 "2024-09-27T11:49:34Z")

</div>

@st331man sorry pleas e ping me if I don’t reply. I get busy.

Looking at this again, ensure that you set preprocessing the labels to true. Label normalization is important.

Add this:  
myNormalizer.fitLabel(true);

We don’t do that by default due to classifiers not normally needing that.

---

<div class="post-metadata">

**Author:** ![st331man](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@st331man](https://community.konduit.ai/u/st331man)\
**Post date:** [September 28, 2024, 5:41am UTC](https://community.konduit.ai/t/incorrect-regression-output-data/3252/5 "2024-09-28T05:41:17Z")

</div>

@agibsonccc Thank you for the hint; I didn’t know that I had to do it this way. Nevertheless, the problem turned out to be something else.  
Apparently, the issue lies in the fact that I am incorrectly inputting data into the neural network, or I am doing it with an incorrect number of dimensions.  
Each sequence is in a separate file, and each label is also in a separate file. Currently, I am using only one time step in the label, but in the future, I want to enable forecasting for several steps ahead.  
It seems that I am somehow incorrectly feeding the data into the iterator, resulting in all the data mixing together, and ultimately yielding incorrect results. I noticed that the output somehow ends up containing identical data. For some reason, the network also outputs a sequence that contains repeated data, while I only need the label.

![image](https://canada1.discourse-cdn.com/flex035/uploads/konduit/original/2X/c/c9d2e3304cc20d07484f8ef82a912add7341fc7f.png)
