Convert 2d into 3d for using LSTM

Hello,

i would like to do some experiment with LSTM.
Attached at the end a pseudocode of my experiment.

I basically create a 2d array, with feature(s) that i generate in some way (generally speaking some forecast api for weather like rapid api dark sky)

i don’t understand how to make it 3d (with meaningful information of course) to be able to exploit LSTM and make some esperiment with that.

thanks for helping!

—here the code—

public static void main(String args[]) throws Throwable {
    int features = 5;
    int classes = 2;

    double[][] featureMatrix = new double[100][features];
    double[][] labelMatrix = new double[100][classes];

    for (int i = 0; i < 100; i++) {
        for (int a = 0; a < features; a++) {
            featureMatrix[i][a] = ...; //get data for index I via rapidapi 
        }
        labelMatrix[i] = new double[classes];
        labelMatrix[i][...] =1;
    }
    INDArray trainingIn = Nd4j.create(featureMatrix);
    INDArray label = Nd4j.create(labelMatrix);

    DataSet myData = new DataSet(trainingIn, label);

}

The format for RNN type input is [batchSize, featureSize, stepCount]. If I understood your code correctly, you’ve got 100 steps with 5 features and 2 labels and effectively 1 example sequence.

Just reshape (or expandDims) your INDArrays to have the shape [1, 5, 100] and you’ll have the correct shape.

Hi treo,

thanks for your help, and yes, it’s a 100 step 5 feature 2 labels (1 sequence only)

There is any sample code for that task?

i am not confident of my solution. i did something like

   double[][][] featureMatrix = new double[1][5][100];
    double[][][] labelMatrix = new double[1][2][100];

    for (int q = 0; q < 100; q++) {
        for (int attr = 0; attr < 5; attr++) {
            featureMatrix[0][attr][q] = 1; //FETCH FROM ONLINE
        }
        labelMatrix[0] = new double[Config.classVal.size()][100];
        int index = 0; //fetch from online
        labelMatrix[0][index][q] = 1;
    }

but the result if i pass to LSTM it’s not very good, i just want to check if the data it’s correct and i have to investigate on LSTM network or if i did something wrong in advance.

What confuse me it’s that the matrix it’s inveted from before.
before i have
new double[100][features]
now
new double[1][features][100] → features & size are swapped? it’s that right?

Ideally, you would create a custom SequenceRecordReader implementation for your case, and then use the regular datavec methods. (see also this post and the replies to it, for a way that can be used if the data fits in memor, I don’t quite recommend it, but sometimes that way can be faster to get started Training CNN error / CNN text classification - #8 by treo).

You can print out your data after conversion to take another look at it. Take also a look at https://deeplearning4j.konduit.ai/models/recurrent#data-for-rnns for more information on how to structure data for recurrent networks.

It is easy to select hyper parameter values where the network isn’t going to learn anything, so that may be a reason