How to convert CSV to the input of 1D convolutional layer

My problem is “fitting a 1D signal”. the signals are stored in a CSV file with the size of 1000 * 2069 (1:2048 -> features, 2049:2069 -> targets/labels).
I loaded a CSV file using this piece of code:

    RecordReader recordReader = new CSVRecordReader(0,',');
    recordReader.initialize(new FileSplit(new File("D:\\deep fitting\\Labels.csv")));
    DataSetIterator iterator = new RecordReaderDataSetIterator.Builder(recordReader,1000)
        .regression(2048, 2068)
        .build();
    DataSet allData = iterator.next();

However, the input of my network is 1D Conv layer:

.layer(new Convolution1DLayer.Builder().kernelSize(5).convolutionMode(ConvolutionMode.Same)
                .nIn(1).nOut(16).build())

when i run my code, i encountered this Error:

Invalid input: expect CNN activations with rank 4 (received input with shape [800, 2048])

which is logical, so I convert the shape of features matrix.

train.setFeatures(train.getFeatures().reshape(800,2048,1,1));

(I do not know it is okay or not) however this time i got this error:

Exception in thread "main" java.lang.IllegalStateException: Invalid input, does not match configuration: expected [minibatch, numChannels=1, inputHeight=1, inputWidth=2048] but got input array ofshape [800, 2048, 1, 1]

By reading the message:

expected [minibatch, numChannels=1, inputHeight=1, inputWidth=2048] but got input array ofshape [800, 2048, 1, 1]

I think it is expecting shape [800, 1, 1, 2048] but got [800, 2048, 1, 1].

Maybe you have to change your reshape from:

train.setFeatures(train.getFeatures().reshape(800,2048,1,1));

To this:

train.setFeatures(train.getFeatures().reshape(800,1,1,2048));

Can you please share how exactly you’ve configured your network and the full stacktrace?

Thanks @StoicProgrammer I did it; however, It did not work

yes, sure. you can find my network

private static long seed = 123L;

public static void main(String[] args) throws IOException, InterruptedException {
    MultiLayerConfiguration configuration = new NeuralNetConfiguration.Builder()
        .seed(seed)
        .updater(new AdaDelta())
        .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
        .weightInit(WeightInit.XAVIER)
        .list()
        .layer(new Convolution1DLayer.Builder().kernelSize(5).convolutionMode(ConvolutionMode.Same)
            .nIn(1).nOut(16).build())
        .layer(new Subsampling1DLayer.Builder().kernelSize(2).stride(2).poolingType(SubsamplingLayer.PoolingType.MAX).build())
        .layer(new Convolution1DLayer.Builder().kernelSize(5).convolutionMode(ConvolutionMode.Same).activation(Activation.LEAKYRELU)
            .nOut(32).build())
        .layer(new Subsampling1DLayer.Builder().kernelSize(2).stride(2).build())
        .layer(new Convolution1DLayer.Builder().kernelSize(5).convolutionMode(ConvolutionMode.Same).activation(Activation.LEAKYRELU)
            .nOut(64).build())
        .layer(new Subsampling1DLayer.Builder().kernelSize(2).stride(2).build())
        .layer(new Convolution1DLayer.Builder().kernelSize(3).convolutionMode(ConvolutionMode.Same).activation(Activation.LEAKYRELU)
            .nOut(128).build())
        .layer(new Subsampling1DLayer.Builder().kernelSize(2).stride(2).build())
        .layer(new Convolution1DLayer.Builder().kernelSize(3).convolutionMode(ConvolutionMode.Same).activation(Activation.LEAKYRELU)
            .nOut(256).build())
        .layer(new Subsampling1DLayer.Builder().kernelSize(2).stride(2).build())
        .layer(new Convolution1DLayer.Builder().kernelSize(3).convolutionMode(ConvolutionMode.Same).activation(Activation.LEAKYRELU)
            .nOut(512).build())
        .layer(new Subsampling1DLayer.Builder().kernelSize(2).stride(2).build())
        .layer(new DropoutLayer(0.2))
        .layer(new DenseLayer.Builder().nOut(21).build())
        .layer(new OutputLayer.Builder(LossFunctions.LossFunction.MSE)
            .name("output")
            .nOut(21)
            .activation(Activation.IDENTITY).build())
        .setInputType(InputType.convolutional(1,2048,1))
        .build();

    MultiLayerNetwork network = new MultiLayerNetwork(configuration);
    network.init();
    UIServer uiServer = UIServer.getInstance();
    StatsStorage statsStorage = new FileStatsStorage(new File(System.getProperty("java.io.tmpdir"), "ui-stats.dl4j"));
    uiServer.attach(statsStorage);

    RecordReader recordReader = new CSVRecordReader(0,',');
    recordReader.initialize(new FileSplit(new File("D:\\deep fitting\\Labels.csv")));
    DataSetIterator iterator = new RecordReaderDataSetIterator.Builder(recordReader,1000)
        .regression(2048, 2068)
        .build();
    DataSet allData = iterator.next();
    allData.shuffle();
    SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.8);
    DataSet train = testAndTrain.getTrain();

    train.setFeatures(train.getFeatures().reshape(800,2048,1,1));

   // train.setLabels(train.getLabels().reshape(800,1,21,1));
    DataSet test = testAndTrain.getTest();
    network.setListeners(new StatsListener( statsStorage), new ScoreIterationListener(1), new EvaluativeListener(iterator, 1, InvocationType.EPOCH_END));
    network.fit(train);

and the stacktrace:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid input: expect CNN activations with rank 4 (received input with shape [800, 2048])
	at org.deeplearning4j.nn.conf.preprocessor.CnnToRnnPreProcessor.preProcess(CnnToRnnPreProcessor.java:74)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.ffToLayerActivationsInWs(MultiLayerNetwork.java:1122)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:2750)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:2708)
	at org.deeplearning4j.optimize.solvers.BaseOptimizer.gradientAndScore(BaseOptimizer.java:170)
	at org.deeplearning4j.optimize.solvers.StochasticGradientDescent.optimize(StochasticGradientDescent.java:63)
	at org.deeplearning4j.optimize.Solver.optimize(Solver.java:52)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fitHelper(MultiLayerNetwork.java:2309)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fit(MultiLayerNetwork.java:2267)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fit(MultiLayerNetwork.java:2330)
	at org.deeplearning4j.examples.convolution.oneDConv.main(oneDConv.java:90)

What you can do, is add an input preprocessor that will do the proper reshaping for you.

You do this by adding this line to your model configuration:

.inputPreProcessor(0,  new FeedForwardToCnnPreProcessor(2048, 1))

This turns your input into a 2048 wide and 1 high matrix with a single channel.

You will also have to change your .setInputType line to the following:

        .setInputType(InputType.feedforward(2048))

That way, you don’t need to do any manual reshaping.

Thank you very much @treo. It works. It should be mentioned that I changed the network layers from 1D to 2D; because after I changed setInputType to feedforward, I got an error in which stated Conv1D needs RNN input.