deeplearning4j/deeplearning4j · error · IllegalStateException

Invalid input type (layer index = " + layerIndex + ", layer…

Error message

Invalid input type (layer index = " + layerIndex + ", layer name="" + getLayerName() + ""): expected FeedForward input type. Got: " + inputType

What it means

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.

Solutions

  1. 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).
  2. 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.
  3. Ensure the layer receiving this error is meant to be feed-forward; otherwise swap in a layer that accepts the actual input type.
  4. If calling getOutputType manually, pass a valid InputType.ff(...) or InputType.convolutionalFlat(...).

Example fix

// before
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
    .list()
    .layer(0, new ConvolutionLayer.Builder(3,3).nIn(1).nOut(16).build())
    .layer(1, new DenseLayer.Builder().nOut(128).build()) // got CNN, expects FF/CNNFlat
    .build(); // no setInputTypes -> no auto preprocessor

// after
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
    .list()
    .layer(0, new ConvolutionLayer.Builder(3,3).nIn(1).nOut(16).build())
    .layer(1, new DenseLayer.Builder().nOut(128).build())
    .setInputTypes(InputType.convolutional(28,28,1)) // auto-inserts flattening preprocessor
    .build();
Defensive patterns

Strategy: validation

Validate before calling

InputType in = /* previous layer's output type */;
if (in == null || (in.getType() != InputType.Type.FF && in.getType() != InputType.Type.CNNFlat)) {
    throw new IllegalArgumentException("FeedForwardLayer needs FF or CNNFlat input, got: " + in + "; insert flattening preprocessor");
}

Type guard

boolean acceptsFeedForwardInput(InputType t) {
    return t != null && (t.getType() == InputType.Type.FF || t.getType() == InputType.Type.CNNFlat);
}

Try / catch

try {
    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();
} catch (IllegalStateException e) {
    if (e.getMessage().contains("expected FeedForward input type")) {
        throw new ConfigurationException("Add setInputTypes / CnnToFeedForwardPreProcessor before dense layers", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07). Data as JSON: /api/errors/53c91e7a82c093ec. Report an issue: GitHub.

Appendix: source

Thrown at deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/FeedForwardLayer.java:60

@EqualsAndHashCode(callSuper = true)
public abstract class FeedForwardLayer extends BaseLayer {

    protected long nIn;
    protected long nOut;
    protected DataFormat timeDistributedFormat;

    public FeedForwardLayer(Builder builder) {
        super(builder);
        this.nIn = builder.nIn;
        this.nOut = builder.nOut;
    }


    @Override
    public InputType getOutputType(int layerIndex, InputType inputType) {
        if (inputType == null || (inputType.getType() != InputType.Type.FF
                && inputType.getType() != InputType.Type.CNNFlat)) {
            throw new IllegalStateException("Invalid input type (layer index = " + layerIndex + ", layer name=\""
                    + getLayerName() + "\"): expected FeedForward input type. Got: " + inputType);
        }

        return InputType.feedForward(nOut, timeDistributedFormat);
    }

    @Override
    public void setNIn(InputType inputType, boolean override) {
        if (inputType == null || (inputType.getType() != InputType.Type.FF
                && inputType.getType() != InputType.Type.CNNFlat && inputType.getType() != InputType.Type.RNN)) {
            throw new IllegalStateException("Invalid input type (layer name=\"" + getLayerName()
                    + "\"): expected FeedForward input type. Got: " + inputType);
        }

        if (nIn <= 0 || override) {
            if (inputType.getType() == InputType.Type.FF) {
                InputType.InputTypeFeedForward f = (InputType.InputTypeFeedForward) inputType;
                this.nIn = f.getSize();

View on GitHub (pinned to 4c22ac5fe4)