{"record":{"id":"69abee5820d6f5dd","repo":"deeplearning4j/deeplearning4j","slug":"invalid-index-cannot-get-output-layer-total-n","errorCode":null,"errorMessage":"Invalid index: cannot get output layer {}, total number of network outputs = {}","messagePattern":"Invalid index: cannot get output layer (.+?), total number of network outputs = (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/ComputationGraph.java","lineNumber":2955,"sourceCode":"    }\n\n    /**\n     * Set the computationGraphUpdater for the network\n     */\n    public void setUpdater(ComputationGraphUpdater updater) {\n        if (solver == null) {\n            solver = new Solver.Builder().configure(conf()).listeners(getListeners()).model(this).build();\n        }\n        solver.getOptimizer().setUpdater(updater);\n    }\n\n    /**\n     * Get the specified output layer, by index. The index of the output\n     * layer may be 0 to {@link #getNumOutputArrays()}-1\n     */\n    public Layer getOutputLayer(int outputLayerIdx) {\n        if (outputLayerIdx >= numOutputArrays)\n            throw new IllegalArgumentException(\"Invalid index: cannot get output layer \" + outputLayerIdx\n                    + \", total number of network outputs = \" + numOutputArrays);\n        return getLayer(configuration.getNetworkOutputs().get(outputLayerIdx));\n    }\n\n    /**\n     * @deprecated To be removed. Use {@link #params()}\n     */\n    @Deprecated\n    public INDArray params(boolean backwardOnly) {\n        return params();\n    }\n\n    /**\n     * Sets the input and labels and returns a score for the prediction with respect to the true labels<br>\n     * This is equivalent to {@link #score(DataSet, boolean)} with training==true.<br>\n     * <b>NOTE:</b> this version of the score function can only be used with ComputationGraph networks that have\n     * a single input and a single output.\n     *","sourceCodeStart":2937,"sourceCodeEnd":2973,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/ComputationGraph.java#L2937-L2973","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Clamp/check the index against graph.getNumOutputArrays() before calling getOutputLayer","Use getOutputLayer(0) for single-output graphs","Iterate with for(int i=0;i<graph.getNumOutputArrays();i++) if you need all output layers","Alternatively fetch by name: getLayer(configuration.getNetworkOutputs().get(idx)) after bounds-checking"],"exampleFix":"// before\nLayer out = graph.getOutputLayer(1); // single-output graph\n// after\nLayer out = graph.getOutputLayer(0);","handlingStrategy":"type-guard","validationCode":"int idx = 1;\nif (idx >= 0 && idx < graph.getNumOutputArrays()) {\n    Layer out = graph.getOutputLayer(idx);\n}","typeGuard":"static boolean isValidOutputIndex(ComputationGraph g, int i) { return i >= 0 && i < g.getNumOutputArrays(); }","tryCatchPattern":"try { Layer l = graph.getOutputLayer(idx); }\ncatch (IllegalArgumentException e) { log.warn(\"bad output index {} of {}\", idx, graph.getNumOutputArrays()); l = graph.getOutputLayer(0); }","preventionTips":["Bound loops with getNumOutputArrays(), not layer counts","For single-output nets use index 0","Fetch by vertex name when the mapping matters more than the index"],"tags":["deeplearning4j","computation-graph","index","output-layer"],"backgroundTag":"index-out-of-range","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"}