javadl4jnd4j

ND4J DL4J Getting data into fit method


I have an INDArray of data which is the return value of x as follows:

    private static INDArray createDataSet(String path)throws Exception {

    List<String> lines = IOUtils.readLines(new FileInputStream(path), StandardCharsets.UTF_8);

    double[] position = new double[lines.size()];
    double[] year = new double[lines.size()];
    double[] month = new double[lines.size()];
    double[] day = new double[lines.size()];
    double[] close = new double[lines.size()];

    int linecount = 0;
    Iterator<String> it = lines.iterator();
    while(it.hasNext()) {
        String line = it.next();

            String[] parts = line.split(",");

            position[linecount] = linecount;
            year[linecount] =  Double.valueOf(parts[0]);
            month[linecount] =  Double.valueOf(parts[1]);
            day[linecount] =  Double.valueOf(parts[2]);
            close[linecount] = Double.valueOf(parts[5]);

            linecount++;
    }//endloop


    double[][] arr2D = new double[][] {position, year, month, day, close};
    INDArray x = Nd4j.createFromArray(arr2D);

    return x;

}

I'm trying to copy the csvplotter example and perform a linear regression with a single in/out network.

How would I load line the arrays row(0) as feature and array row(4) as label?

A little more info:

    System.out.println(ds.rank());
    long[] l  = ds.shape();
    System.out.println(l[0] + " , " + l[1] + "  -  " + l.length);
    System.out.println(ds.length());

Results in:

2,
5, 1260 -2
6300

Here is my problem just to be clear:

       for (int i = 0; i < nEpochs; i++) {

       net.fit(d);
    }

Results in a variety of errors depending on how I try to add data


Solution

  • Although I have not got the answer working I realized my problem. Based on on the comments in the csv plotter example I assumed that the rows of an indarray are delivered to the inputs. However it is the columns that are actually delivered to the inputs.

    By transposing the INDArray and adding the two columns I needed the network processed the data.

    INDArray ds;
         ds = ds.transpose();
            DataSet ddd = new DataSet();
            ddd.setFeatures(ds.getColumn(0, true)); //true maintains matrix instead of vector
            ddd.setLabels(ds.getColumn(4, true));
            ddd.dataSetBatches(500);
            System.out.println(ddd);
    

    My printout:

    ===========INPUT===================
    [[0], 
     [1.0000], 
     [2.0000], 
      ..., 
     [1257.0000], 
     [1258.0000], 
     [1259.0000]]
    =================OUTPUT==================
    [[540.3100], 
     [536.7000], 
     [533.3300], 
      ..., 
     [1431.8199], 
     [1439.2200], 
     [1436.3800]]
    

    Although the training was unsuccessful this does answer my original question.