deeplearning4j/deeplearning4j

DL4JInvalidInputException when Using Convolutional Layers and Dense Layers

lunalobos opened this issue · 1 comments

I am currently working on a deep learning model using Deeplearning4j that involves convolutional layers followed by a dense layer. However, when training the model, I encounter the following error:

org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 4 array with shape [65851, 14, 8, 8]. Missing preprocessor or wrong input type? (layer name: layer2, layer index: 2, layer type: DenseLayer)

The issue seems to be related to the incorrect shape of the input data being provided to the dense layer. The model includes convolutional layers that output tensors with shape [batch_size, channels, height, width].

Here is the code snippet for the configuration of the model:

MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
        .seed(123)
        .weightInit(WeightInit.XAVIER)
        .updater(new Adam(0.001))
        .list()
        .layer(0, new ConvolutionLayer.Builder().nIn(14).nOut(14).kernelSize(3, 3).stride(1, 1).padding(1, 1)
                .activation(Activation.RELU).build())
        .layer(1, new ConvolutionLayer.Builder().nIn(14).nOut(14).kernelSize(3, 3).stride(1, 1).padding(1, 1)
                .activation(Activation.RELU).build())
        .layer(3, new DenseLayer.Builder().nIn(14 * 8 * 8).nOut(14).activation(Activation.RELU).build())
        .layer(4, new OutputLayer.Builder(LossFunctions.LossFunction.MEAN_SQUARED_LOGARITHMIC_ERROR).nIn(14)
                .nOut(1).activation(Activation.IDENTITY).build())
        .build();

The expected behavior is that the preprocessor of the DenseLayer should correctly reshape the data according to the specified nIn value, which in this case is set to 14 * 8 * 8. This should ensure that the input data is flattened and compatible with the dense layer.

I would appreciate any guidance on how to correctly preprocess the data and configure the layers to avoid this error. If I'm missing something in the configuration or preprocessing steps, please let me know.

Thank you for your assistance.

Environment:

Deeplearning4j Version: 1.0.0-M2.1
Java Version: 17
Operating System: Windows 10

@lunalobos please use the community forums for learning the framework: https://community.konduit.ai/ We separate issue trackers and the forums to make it easier to find help topics. Thanks!