deeplearning4j/deeplearning4j · error · IllegalArgumentException

Invalid index: cannot get output layer

Error message

Invalid index: cannot get output layer {}, total number of network outputs = {}

What it means

getOutputLayer(int outputLayerIdx) retrieves the ith declared output layer, valid only for indices 0..numOutputArrays-1. Passing an index >= numOutputArrays (or negative, wrapping in indexOf semantics) throws IllegalArgumentException naming the bad index and the total output count.

Solutions

  1. Clamp/check the index against graph.getNumOutputArrays() before calling getOutputLayer
  2. Use getOutputLayer(0) for single-output graphs
  3. Iterate with for(int i=0;i<graph.getNumOutputArrays();i++) if you need all output layers
  4. Alternatively fetch by name: getLayer(configuration.getNetworkOutputs().get(idx)) after bounds-checking

Example fix

// before
Layer out = graph.getOutputLayer(1); // single-output graph
// after
Layer out = graph.getOutputLayer(0);
Defensive patterns

Strategy: type-guard

Validate before calling

int idx = 1;
if (idx >= 0 && idx < graph.getNumOutputArrays()) {
    Layer out = graph.getOutputLayer(idx);
}

Type guard

static boolean isValidOutputIndex(ComputationGraph g, int i) { return i >= 0 && i < g.getNumOutputArrays(); }

Try / catch

try { Layer l = graph.getOutputLayer(idx); }
catch (IllegalArgumentException e) { log.warn("bad output index {} of {}", idx, graph.getNumOutputArrays()); l = graph.getOutputLayer(0); }

Prevention

When it happens

Trigger: Calling getOutputLayer(i) where i is greater than or equal to the number of outputs declared via setOutputs(...) — e.g. getOutputLayer(1) on a single-output graph, or using a loop bound of layers.size() instead of numOutputArrays.

Common situations: Assuming output index equals layer index in the Sequential configuration; iterating with the wrong bound when the graph has fewer outputs than layers; changes to the config reducing output count without updating access code.

Related errors


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

Appendix: source

Thrown at deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/ComputationGraph.java:2955

    }

    /**
     * Set the computationGraphUpdater for the network
     */
    public void setUpdater(ComputationGraphUpdater updater) {
        if (solver == null) {
            solver = new Solver.Builder().configure(conf()).listeners(getListeners()).model(this).build();
        }
        solver.getOptimizer().setUpdater(updater);
    }

    /**
     * Get the specified output layer, by index. The index of the output
     * layer may be 0 to {@link #getNumOutputArrays()}-1
     */
    public Layer getOutputLayer(int outputLayerIdx) {
        if (outputLayerIdx >= numOutputArrays)
            throw new IllegalArgumentException("Invalid index: cannot get output layer " + outputLayerIdx
                    + ", total number of network outputs = " + numOutputArrays);
        return getLayer(configuration.getNetworkOutputs().get(outputLayerIdx));
    }

    /**
     * @deprecated To be removed. Use {@link #params()}
     */
    @Deprecated
    public INDArray params(boolean backwardOnly) {
        return params();
    }

    /**
     * Sets the input and labels and returns a score for the prediction with respect to the true labels<br>
     * This is equivalent to {@link #score(DataSet, boolean)} with training==true.<br>
     * <b>NOTE:</b> this version of the score function can only be used with ComputationGraph networks that have
     * a single input and a single output.
     *

View on GitHub (pinned to 4c22ac5fe4)