ComputationGraph
The ComputationGraph API — multi-input, multi-output, skip connections, and arbitrary DAG topologies
Overview
ComputationGraph is DL4J's general-purpose network class. Unlike MultiLayerNetwork, which connects layers in a fixed chain, ComputationGraph supports arbitrary directed acyclic graph (DAG) topologies:
Multiple network inputs
Multiple network outputs (mixed classification and regression)
Skip connections and residual connections
Branching and merging of activation streams
Siamese, multi-task, and encoder-decoder architectures
Everything MultiLayerNetwork can do, ComputationGraph can also do — though configuration is slightly more verbose.
When to Use ComputationGraph vs. MultiLayerNetwork
Single input, single output
Yes
Yes
Multiple inputs
No
Yes
Multiple outputs
No
Yes
Skip / residual connections
No
Yes
Complex loss combinations
No
Yes
Siamese / shared-weight sub-networks
No
Yes
Simpler configuration
Yes (preferred)
More verbose
Graph Building API
Configuration starts the same way as MultiLayerNetwork — with NeuralNetConfiguration.Builder — but instead of calling .list() you call .graphBuilder().
addInputs(String...)
Declares one or more named network inputs. The order determines which position in the INDArray[] passed to fit() and output() corresponds to which input.
addLayer(String, Layer, String...)
Adds a layer vertex to the graph.
First argument: unique name for this layer.
Second argument: layer configuration.
Remaining arguments: names of this layer's inputs (other layers or declared inputs).
addVertex(String, GraphVertex, String...)
Adds a non-layer vertex (merge, element-wise op, subset, etc.):
setOutputs(String...)
Declares which vertices produce the network's outputs. The order determines the position of output arrays in output() return values and in MultiDataSet label arrays.
setInputTypes(InputType...)
Enables automatic nIn inference and automatic insertion of pre-processors between mismatched layer types (e.g., CNN -> Dense):
Types of Graph Vertices
LayerVertex
Standard neural network layer. Added via addLayer(). Supports all layer types available in MultiLayerNetwork.
InputVertex
Created automatically when you call addInputs(). One InputVertex per named input.
MergeVertex
Concatenates activations from two or more inputs along the feature dimension. Use this to combine branches.
For CNN activations, merging happens along the channel dimension. For RNN activations, along the feature dimension.
ElementWiseVertex
Applies an element-wise operation to inputs of identical shape:
Supported ops: Add, Subtract, Product, Average, Max.
SubsetVertex
Extracts a range of features from a vertex's output:
PreProcessorVertex
Applies an InputPreProcessor as a standalone graph node (without attaching it to a layer):
Example 1: Recurrent Network with Skip Connection
The nIn on the output layer is 64 + 32 = 96 because MergeVertex-style concatenation happens implicitly when a layer lists multiple inputs and no explicit vertex is added.
Example 2: Multiple Inputs with a Merge Vertex
Two separate input streams (e.g., image features and tabular features) are processed independently and then merged.
Example 3: Multi-Task Learning
One shared trunk feeds two independent output heads — one for classification and one for regression.
Training with MultiDataSet (required for multiple outputs):
Example 4: Residual Block
A residual (skip) connection adds the layer input directly to the layer output via ElementWiseVertex:
Example 5: Siamese Network
A Siamese network uses two identical subnetworks (shared weights) to compare two inputs. In DL4J this is done with ComputationGraph by routing both inputs through the same named layer — but note that DL4J does not natively support weight sharing between separate named layers. The typical approach is to use two separate layer definitions with identical configurations, or to compute the representations separately in preprocessing and use a single-input graph for the comparison head.
For a simplified Siamese distance-based network where two feature vectors are compared:
Training Data
DataSet / DataSetIterator
Use when the graph has a single input and single output. Same as MultiLayerNetwork.
MultiDataSet / MultiDataSetIterator
Required for multiple inputs or multiple outputs.
RecordReaderMultiDataSetIterator
Inference
Evaluation
Saving and Loading
Key API Reference
graphBuilder()
Returns a ComputationGraphConfiguration.GraphBuilder
addInputs(String...)
Declare network input names
addLayer(String, Layer, String...)
Add a layer vertex with named inputs
addVertex(String, GraphVertex, String...)
Add a non-layer vertex
setOutputs(String...)
Declare which vertices are outputs
setInputTypes(InputType...)
Enable automatic shape inference
init()
Initialize parameters
fit(DataSetIterator)
Train (single input/output)
fit(MultiDataSetIterator)
Train (multiple inputs/outputs)
output(INDArray...)
Run forward pass, return output arrays
outputSingle(INDArray)
Convenience method: single input/output
evaluate(DataSetIterator)
Returns Evaluation for single-output graph
summary()
Print graph topology and parameter counts
Last updated
Was this helpful?