deeplearning4j/deeplearning4j · error · UnsupportedOperationException
Cannot calculate score: vertex
Error message
Cannot calculate score: vertex "{}" is not an output layer What it means
ComputationGraph.score() computes the loss by iterating over every vertex listed in the network's output configuration. Each such vertex must hold a Layer implementing IOutputLayer (e.g. LossLayer, OutputLayer) so labels can be set. If any configured output vertex is not an IOutputLayer, score calculation is impossible and this UnsupportedOperationException is thrown.
Solutions
- Ensure every vertex name in .setOutputs(...) corresponds to a vertex holding an OutputLayer or LossLayer (an IOutputLayer).
- In ComputationGraphConfiguration.Builder use .addLayer(name, new OutputLayer.Builder(...)....build()) for each listed output vertex.
- Verify with graph.getConfiguration().getNetworkOutputs() and cross-check graph.getVertex(name).getLayer() instanceof IOutputLayer before calling score().
- If you only want activations, call feedForward()/output() instead of score().
Example fix
// before
.setOutputs("dense2") // dense2 is a plain DenseLayer, not IOutputLayer
// after
.addLayer("out", new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.nIn(64).nOut(10).activation(Activation.SOFTMAX).build(), "dense2")
.setOutputs("out") Defensive patterns
Strategy: validation
Validate before calling
boolean ok = graph.getConfiguration().getNetworkOutputs().stream()
.allMatch(s -> {
Layer l = graph.getVertex(s).getLayer();
return l != null && l instanceof IOutputLayer;
});
if (!ok) throw new IllegalStateException("Every output vertex must be an IOutputLayer"); Type guard
boolean isOutputVertex(String s) {
Layer l = graph.getVertex(s).getLayer();
return l instanceof IOutputLayer;
} Try / catch
try {
double score = graph.score();
} catch (UnsupportedOperationException e) {
// graph output vertices are not IOutputLayers; fix configuration
} Prevention
- Always add an explicit OutputLayer/LossLayer for each name in setOutputs(...)
- Validate the configuration with .validateBackWays()/build() before instantiating
- Check getVertex(name).getLayer() instanceof IOutputLayer in tests
When it happens
Trigger: Calling score() (or fit paths that compute a score) on a ComputationGraph whose Configuration.getNetworkOutputs() names a vertex whose layer is null or does not implement IOutputLayer — e.g. the output vertex is a regular feed-forward layer, a verticesToOutput marked wrong vertex, or the graph was built without an OutputLayer/LossLayer at that vertex.
Common situations: Configuring .setOutputs("someHiddenVertex") by mistake instead of a dedicated OutputLayer vertex; building an autoencoder-style graph where the output vertex is a plain DenseLayer; adding vertices programmatically and forgetting to make the final one an output layer; using setInputs/setLabelArrays then score() on a validation-only graph.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Cannot calculate gradients based on epsilon with OutputLayer
- Cannot perform backprop with external errors in conjunction…
- Cannot pretrain ComputationGraph with single INDArray
- Cannot return single input: ComputationGraph has multiple…
- Invalid index: cannot get output layer
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/d766153e3b563cb9.
Report an issue: GitHub.
Appendix: source
Thrown at deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/ComputationGraph.java:3158
setInputs(dataSet.getFeatures());
//Need to feed forward, but not the output layers
//TODO maybe optimize? We only need *some* of the activations in the WS...
//mgr.keepOpen(ArrayType.values());
ffToLayerActivationsInWS(false, vertices.length - 1, getOutputLayerIndices(), FwdPassType.STANDARD, false,
dataSet.getFeatures(), dataSet.getFeaturesMaskArrays(), dataSet.getLabelsMaskArrays(), false);
INDArray[] labels = dataSet.getLabels();
setLabels(labels);
double r = (addRegularizationTerms ? calcRegularizationScore(true) : 0.0);
int i = 0;
for (String s : configuration.getNetworkOutputs()) {
GraphVertex gv = verticesMap.get(s);
Layer outLayer = gv.getLayer();
if (outLayer == null || !(outLayer instanceof IOutputLayer)) {
throw new UnsupportedOperationException(
"Cannot calculate score: vertex \"" + s + "\" is not an output layer");
}
IOutputLayer ol = (IOutputLayer) outLayer;
ol.setLabels(labels[i++]);
INDArray scoreCurrLayer;;
scoreCurrLayer =((LayerVertex) gv).computeScoreForExamples(r, mgr);
if (out == null)
out = scoreCurrLayer.detach();
else
out.addi(scoreCurrLayer);
//Only want to add l1/l2 once...
r = 0.0;
}View on GitHub (pinned to 4c22ac5fe4)