{"record":{"id":"53c91e7a82c093ec","repo":"deeplearning4j/deeplearning4j","slug":"invalid-input-type-layer-index-layerindex","errorCode":null,"errorMessage":"Invalid input type (layer index = \" + layerIndex + \", layer name=\"\" + getLayerName() + \"\"): expected FeedForward input type. Got: \" + inputType","messagePattern":"Invalid input type \\(layer index = \" \\+ layerIndex \\+ \", layer name=\"\" \\+ getLayerName\\(\\) \\+ \"\"\\): expected FeedForward input type\\. Got: \" \\+ inputType","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/FeedForwardLayer.java","lineNumber":60,"sourceCode":"@EqualsAndHashCode(callSuper = true)\npublic abstract class FeedForwardLayer extends BaseLayer {\n\n    protected long nIn;\n    protected long nOut;\n    protected DataFormat timeDistributedFormat;\n\n    public FeedForwardLayer(Builder builder) {\n        super(builder);\n        this.nIn = builder.nIn;\n        this.nOut = builder.nOut;\n    }\n\n\n    @Override\n    public InputType getOutputType(int layerIndex, InputType inputType) {\n        if (inputType == null || (inputType.getType() != InputType.Type.FF\n                && inputType.getType() != InputType.Type.CNNFlat)) {\n            throw new IllegalStateException(\"Invalid input type (layer index = \" + layerIndex + \", layer name=\\\"\"\n                    + getLayerName() + \"\\\"): expected FeedForward input type. Got: \" + inputType);\n        }\n\n        return InputType.feedForward(nOut, timeDistributedFormat);\n    }\n\n    @Override\n    public void setNIn(InputType inputType, boolean override) {\n        if (inputType == null || (inputType.getType() != InputType.Type.FF\n                && inputType.getType() != InputType.Type.CNNFlat && inputType.getType() != InputType.Type.RNN)) {\n            throw new IllegalStateException(\"Invalid input type (layer name=\\\"\" + getLayerName()\n                    + \"\\\"): expected FeedForward input type. Got: \" + inputType);\n        }\n\n        if (nIn <= 0 || override) {\n            if (inputType.getType() == InputType.Type.FF) {\n                InputType.InputTypeFeedForward f = (InputType.InputTypeFeedForward) inputType;\n                this.nIn = f.getSize();","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/FeedForwardLayer.java#L42-L78","documentation":"FeedForwardLayer.getOutputType (base class for DenseLayer, OutputLayer, EmbeddingLayer, etc.) requires the incoming InputType to be FF or CNNFlat, because these layers expect a flat/feature vector input of known size. If inputType is null, or is a CNN/RNN/other type, it throws IllegalStateException naming the layer index, layer name, and actual type. It fires during network shape inference when a feed-forward layer is wired after a layer whose output it cannot consume directly.","triggerScenarios":"Placing a DenseLayer/OutputLayer directly after a CNN layer without a flattening preprocessor (expected FF or CNNFlat, got InputType.InputTypeConvolutional); feeding RNN output type to a dense layer without time-distributed handling; calling getOutputType(layerIndex, null) directly; declaring .setInputTypes(InputType.recurrent(...)) for a network starting with a DenseLayer.","commonSituations":"CNN-to-dense transitions missing the CnnToFeedForwardPreProcessor (usually inserted automatically when input types are declared — omitting setInputTypes breaks this); LSTM outputs piped into dense layers without proper recurrent handling in MultiLayerNetwork configs; switching a network's first layer to feed-forward while the declared input remains recurrent/convolutional.","solutions":["Call .setInputTypes(...) on the network configuration with the true input type so DL4J auto-inserts the correct preprocessor (e.g. CnnToFeedForwardPreProcessor flattens CNN to CNNFlat/FF).","Insert an explicit flattening layer/preprocessor between CNN output and the DenseLayer, or reshape RNN output (e.g. use a RnnOutputLayer or last-time-step pooling) before dense layers.","Ensure the layer receiving this error is meant to be feed-forward; otherwise swap in a layer that accepts the actual input type.","If calling getOutputType manually, pass a valid InputType.ff(...) or InputType.convolutionalFlat(...)."],"exampleFix":"// before\nMultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()\n    .list()\n    .layer(0, new ConvolutionLayer.Builder(3,3).nIn(1).nOut(16).build())\n    .layer(1, new DenseLayer.Builder().nOut(128).build()) // got CNN, expects FF/CNNFlat\n    .build(); // no setInputTypes -> no auto preprocessor\n\n// after\nMultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()\n    .list()\n    .layer(0, new ConvolutionLayer.Builder(3,3).nIn(1).nOut(16).build())\n    .layer(1, new DenseLayer.Builder().nOut(128).build())\n    .setInputTypes(InputType.convolutional(28,28,1)) // auto-inserts flattening preprocessor\n    .build();","handlingStrategy":"validation","validationCode":"InputType in = /* previous layer's output type */;\nif (in == null || (in.getType() != InputType.Type.FF && in.getType() != InputType.Type.CNNFlat)) {\n    throw new IllegalArgumentException(\"FeedForwardLayer needs FF or CNNFlat input, got: \" + in + \"; insert flattening preprocessor\");\n}","typeGuard":"boolean acceptsFeedForwardInput(InputType t) {\n    return t != null && (t.getType() == InputType.Type.FF || t.getType() == InputType.Type.CNNFlat);\n}","tryCatchPattern":"try {\n    MultiLayerNetwork net = new MultiLayerNetwork(conf);\n    net.init();\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"expected FeedForward input type\")) {\n        throw new ConfigurationException(\"Add setInputTypes / CnnToFeedForwardPreProcessor before dense layers\", e);\n    }\n    throw e;\n}","preventionTips":["Always declare .setInputTypes(...) so CNN-to-dense flattening preprocessors are inserted automatically.","Never wire DenseLayer/OutputLayer directly after un-flattened CNN or RNN layers.","For RNN output into dense layers, use last-time-step pooling or RnnOutputLayer appropriately."],"tags":["deeplearning4j","java","input-type","feedforward-layer","shape-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"4c22ac5fe4a8350d05d224e7f4499429f7f69c93","analyzedAt":"2026-09-07T05:27:04.714Z","contentChangedAt":"2026-09-07T05:27:04.714Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}