For the complete documentation index, see llms.txt. This page is also available as Markdown.

Graph Vertices

Vertex types for ComputationGraph — Merge, ElementWise, Subset, Stack, Reshape, and custom vertices

In Eclipse Deeplearning4j a vertex is a node in a ComputationGraph that can accept multiple inputs and produce one or more outputs. Vertices enable complex topologies that MultiLayerNetwork cannot express: multi-input merging, inception modules, siamese networks, highway layers, and more.

Vertices are added to a ComputationGraphConfiguration using addVertex(String name, GraphVertex vertex, String... inputs).


MergeVertex

Concatenates two or more input activations along the feature dimension (axis 1 for 2D, axis 1 for 4D CNN feature maps). The output size equals the sum of the input sizes.

Common use: Combining outputs from parallel branches (e.g., inception modules).

import org.deeplearning4j.nn.graph.vertex.impl.MergeVertex;

ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder()
    .graphBuilder()
    .addInputs("input")
    // Branch 1: 1x1 convolution
    .addLayer("branch1", new ConvolutionLayer.Builder(1, 1)
        .nIn(64).nOut(32).build(), "input")
    // Branch 2: 3x3 convolution
    .addLayer("branch2", new ConvolutionLayer.Builder(3, 3)
        .nIn(64).nOut(32).stride(1,1).convolutionMode(ConvolutionMode.Same).build(), "input")
    // Concatenate both branches: output has 64 channels
    .addVertex("merged", new MergeVertex(), "branch1", "branch2")
    .addLayer("output", new CnnLossLayer.Builder().build(), "merged")
    .setOutputs("output")
    .build();

ElementWiseVertex

Applies an element-wise operation across two or more inputs of the same shape. All inputs must have identical dimensions.

Operations

Operation constant
Behaviour

ElementWiseVertex.Op.Add

Element-wise sum of all inputs

ElementWiseVertex.Op.Subtract

Element-wise difference (input0 - input1)

ElementWiseVertex.Op.Product

Element-wise product (Hadamard)

ElementWiseVertex.Op.Average

Element-wise mean of all inputs

ElementWiseVertex.Op.Max

Element-wise maximum across all inputs

Common use: Residual connections (Add), gating mechanisms (Product).


SubsetVertex

Selects a contiguous range of columns (features) from a 2D input (shape [batch, features]). Useful for splitting the output of a layer into separate streams.


StackVertex and UnstackVertex

These vertices work as a pair to allow shared-weight processing across multiple inputs.

StackVertex

Stacks multiple inputs along dimension 0 (the batch dimension), producing a single output with a larger batch size. This enables a single shared layer to process multiple inputs without duplicating weights.

Common use: Siamese networks, triplet embedding where the same encoder processes anchor, positive, and negative inputs.

UnstackVertex

Reverses a StackVertex by extracting a single slice from dimension 0. Parameters:

  • index — which example to extract (0-based).

  • stackSize — the total number of examples stacked (used to compute the step/stride).


ReshapeVertex

Reshapes the activation tensor to a new shape, enabling transitions between 2D (fully connected) and 4D (convolutional) representations within a ComputationGraph.

The first argument is the array ordering ('c' for C order, 'f' for Fortran order). Use -1 for the batch dimension (it is inferred automatically). ReshapeVertex validates that the reshaping is compatible during both forward and backward passes.


L2NormalizeVertex

Performs L2 normalisation on its single input, so that each example's feature vector lies on the unit hypersphere. The output has the same shape as the input.

Common use: Metric learning, face verification, embedding spaces where cosine distance is used.


L2Vertex

Computes the L2 (Euclidean) distance between exactly two inputs of the same shape. The output is a scalar (or batch of scalars).

Common use: Triplet loss networks — compute distance between anchor-positive pair and anchor-negative pair, then feed both scalars into a loss layer.


ScaleVertex

Multiplies the activations of a single input by a scalar constant. Gradients are scaled by the same factor during backpropagation.

Common use: Scaling residual branch outputs (e.g., multiplying by 0.1 in very deep networks to stabilise variance), or implementing highway networks.


ShiftVertex

Adds a scalar constant to all activations of a single input element-wise.

Common use: Adding a bias offset after a layer, or computing (1 - sigmoid(x)) in a highway network:


PreprocessorVertex

Wraps an InputPreProcessor as a ComputationGraph vertex. This allows inserting preprocessing steps (e.g., CnnToFeedForwardPreProcessor, FeedForwardToCnnPreProcessor) between layers in a graph where the automatic preprocessor insertion does not apply.


ReverseTimeSeriesVertex

Reverses the time axis of a sequence input. Useful for building bidirectional RNN variants manually, where one branch processes the sequence forward and another processes it backwards.

Masked time steps (padding) are handled correctly: only the present (mask = 1) time steps are reversed in place; padding (mask = 0) remains at the end of the reversed sequence.


PoolHelperVertex

A specialised vertex for removing the first row and column from a 4D CNN activation tensor. Originally designed to aid importing Caffe's GoogLeNet architecture where the pooling layer produces an output that is one pixel larger than expected.


Custom Vertices

Implement org.deeplearning4j.nn.graph.vertex.GraphVertex (or extend org.deeplearning4j.nn.graph.vertex.BaseGraphVertex) to create a custom vertex.

Key methods to override:

Register the vertex using the standard addVertex API with an instance of your class.

Last updated

Was this helpful?