deeplearning4j/deeplearning4j · error · DL4JException
Layer " " of type is set as network output (but isn't an…
Error message
Layer "{}" of type {} is set as network output (but isn't an IOutputLayer). Only IOutputLayer layers can be fit via backprop with a labels array. What it means
During backprop, if a vertex is marked as a network output, its layer is not an IOutputLayer, and labels were provided (with no external epsilons), DL4J throws DL4JException: only IOutputLayer subclasses know how to turn labels into gradients. A non-output layer marked as network output cannot consume labels, so fitting with labels is invalid for this graph.
Solutions
- Make the output vertex an IOutputLayer: use OutputLayer.Builder(...), RnnOutputLayer, or LossLayer with a loss function
- Remove the labels argument and use backpropGradient with external epsilons if you intend a non-output layer as terminal
- Check configuration.setOutputs(...) points at the intended IOutputLayer vertex name
Example fix
// before
.layer(2, new DenseLayer.Builder().nOut(numClasses).activation(Activation.SOFTMAX).build())
.setOutputs("layer2")
// after
.layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.nOut(numClasses).activation(Activation.SOFTMAX).build())
.setOutputs("layer2") Defensive patterns
Strategy: validation
Validate before calling
for (String name : graph.getConfiguration().getNetworkOutputs()) {
GraphVertex v = graph.getVertex(name);
if (!(v instanceof LayerVertex) || !(((LayerVertex) v).getLayer() instanceof IOutputLayer))
throw new IllegalArgumentException("output vertex '" + name + "' is not an IOutputLayer; cannot fit with labels");
} Type guard
static boolean outputIsIOutputLayer(ComputationGraph g, String name) {
GraphVertex v = g.getVertex(name);
return v instanceof LayerVertex && ((LayerVertex) v).getLayer() instanceof IOutputLayer;
} Try / catch
try { graph.fit(data); }
catch (DL4JException e) { log.error("output vertex is not an IOutputLayer: {}", e.getMessage()); throw e; } Prevention
- End label-based networks with OutputLayer/RnnOutputLayer/LossLayer
- After editing layer types, re-verify setOutputs(...) targets
- During transfer learning, replace removed output layers with an IOutputLayer
When it happens
Trigger: Calling fit(DataSet/MultiDataSet/INDArray labels...) on a ComputationGraph whose setOutputs(...) names a vertex whose layer is not an IOutputLayer (e.g. DenseLayer, LSTM marked as output) while supplying labels and no external epsilons.
Common situations: Forgetting to use OutputLayer/RnnOutputLayer/LossLayer as the final layer; changing the last layer's type during model edits but leaving setOutputs pointing at it; copying a config where the output layer was replaced by a plain activation layer for transfer learning.
Understand the failure class
Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.
Related errors
- Cannot calculate score: vertex
- Cannot perform backprop with external errors in conjunction…
- Invalid index: cannot get output layer
- Attempted conversion of types and was unable to
- Cannot calculate gradient and score with respect to labels…
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/d09f4494efe926a0.
Report an issue: GitHub.
Appendix: source
Thrown at deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/ComputationGraph.java:2692
if (current.isOutputVertex()) {
//Two reasons for a vertex to be an output vertex:
//(a) it's an output layer (i.e., instanceof IOutputLayer), or
//(b) it's a normal layer, but it has been marked as an output layer for use in external errors - for reinforcement learning, for example
int thisOutputNumber = configuration.getNetworkOutputs().indexOf(current.getVertexName());
Layer currentLayer = current.getLayer();
if (currentLayer instanceof FrozenLayerWithBackprop) {
currentLayer = ((FrozenLayerWithBackprop) currentLayer).getInsideLayer();
}
if (currentLayer instanceof IOutputLayer) {
IOutputLayer outputLayer = (IOutputLayer) currentLayer;
INDArray currLabels = labels[thisOutputNumber];
outputLayer.setLabels(currLabels);
} else {
if ((externalEpsilons == null || externalEpsilons.length == 0)
&& labels[thisOutputNumber] != null) {
throw new DL4JException("Layer \"" + current.getVertexName() + "\" of type "
+ current.getLayer().getClass().getSimpleName()
+ " is set as network output "
+ "(but isn't an IOutputLayer). Only IOutputLayer layers can be fit via backprop with"
+ " a labels array. ");
}
current.setEpsilon(externalEpsilons[thisOutputNumber]);
setVertexEpsilon[topologicalOrder[i]] = true;
}
}
//Actually execute backprop for the specified vertex
//First: Open the relevant workspace for the activations.
//Note that this will be closed only once the current vertex's activations have been consumed
MemoryWorkspace wsActivationGrads = workspaceMgr.notifyScopeEntered(ArrayType.ACTIVATION_GRAD);
openActivationsWorkspaces.put(wsActivationGrads, workspaceMgr);
//Note that because we're opening activation gradient workspaces not in any defined order (i.e., workspace
// use isn't simply nested), we'll manually override the previous workspace setting. Otherwise, when weView on GitHub (pinned to 4c22ac5fe4)