Implementation:Tensorflow Tfjs Merge Layers
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
The merge.ts module implements layers that combine multiple input tensors into a single output tensor. All merge layers extend the abstract Merge base class, which handles input validation, shape broadcasting, and the general call() logic including reshaping inputs of different ranks to make them broadcastable.
Seven concrete merge layers are provided: Add, Multiply, Average, Maximum, Minimum, Concatenate, and Dot. The first five perform element-wise operations with broadcasting, Concatenate joins tensors along a specified axis, and Dot computes the dot product of exactly two inputs along specified axes.
Code Reference
Source Location
tfjs-layers/src/layers/merge.ts (View on GitHub)
Key Signatures
export abstract class Merge extends Layer {
protected reshapeRequired: boolean;
protected mergeFunction(inputs: Tensor[]): Tensor;
build(inputShape: Shape | Shape[]): void;
call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[];
}
export class Add extends Merge {
static className = 'Add';
protected mergeFunction(inputs: Tensor[]): Tensor;
}
export class Multiply extends Merge {
static className = 'Multiply';
protected mergeFunction(inputs: Tensor[]): Tensor;
}
export class Average extends Merge {
static className = 'Average';
protected mergeFunction(inputs: Tensor[]): Tensor;
}
export class Maximum extends Merge {
static className = 'Maximum';
protected mergeFunction(inputs: Tensor[]): Tensor;
}
export class Minimum extends Merge {
static className = 'Minimum';
protected mergeFunction(inputs: Tensor[]): Tensor;
}
export class Concatenate extends Merge {
static className = 'Concatenate';
readonly axis: number;
constructor(args?: ConcatenateLayerArgs);
protected mergeFunction(inputs: Tensor[]): Tensor;
}
export class Dot extends Merge {
static className = 'Dot';
readonly axes: number | [number, number];
readonly normalize: boolean;
constructor(args: DotLayerArgs);
protected mergeFunction(inputs: Tensor[]): Tensor;
}
Import
import { Add, Multiply, Average, Maximum, Minimum, Concatenate, Dot } from './layers/merge';
Layer Details
| Layer | Operation | Input Count | Key Config |
|---|---|---|---|
Add |
Element-wise sum | >= 2 | None |
Multiply |
Element-wise product | >= 2 | None |
Average |
Element-wise mean | >= 2 | None |
Maximum |
Element-wise maximum | >= 2 | None |
Minimum |
Element-wise minimum | >= 2 | None |
Concatenate |
Join along axis | >= 2 | axis (default -1)
|
Dot |
Batch dot product | Exactly 2 | axes, normalize
|
I/O Contract
Input
All merge layers accept an array of at least 2 tensors. For element-wise layers (Add, Multiply, Average, Maximum, Minimum), inputs must be broadcastable to the same shape. For Concatenate, inputs must match on all axes except the concatenation axis. For Dot, exactly 2 inputs are required with ranks <= 3.
Output
- Element-wise layers: Output shape is the broadcast-compatible shape of all inputs.
- Concatenate: Output shape matches inputs on all axes except the concatenation axis, which is the sum of the input dimensions along that axis.
- Dot: Output shape is derived by removing the contracted axes from each input and concatenating the remaining dimensions. If
normalize=true, inputs are L2-normalized before dot product computation.
Reshape Handling
The Merge base class automatically handles inputs of different ranks by expanding dimensions at axis 1 until all inputs have the same rank (reshapeRequired flag).
Usage Examples
import * as tf from '@tensorflow/tfjs';
// Residual connection using Add
const input = tf.input({ shape: [32] });
const dense1 = tf.layers.dense({ units: 32, activation: 'relu' }).apply(input);
const added = tf.layers.add().apply([input, dense1]);
// Multi-input model with concatenation
const input1 = tf.input({ shape: [10] });
const input2 = tf.input({ shape: [20] });
const concatenated = tf.layers.concatenate({ axis: -1 }).apply([input1, input2]);
// Output shape: [null, 30]
// Dot product with normalization (cosine similarity)
const a = tf.input({ shape: [128] });
const b = tf.input({ shape: [128] });
const similarity = tf.layers.dot({ axes: -1, normalize: true }).apply([a, b]);
// Element-wise maximum for skip connections
const branch1 = tf.layers.dense({ units: 64 }).apply(input);
const branch2 = tf.layers.dense({ units: 64 }).apply(input);
const merged = tf.layers.maximum().apply([branch1, branch2]);
Related Pages
- Tensorflow_Tfjs_Exports_Layers - Public API factory functions (
tf.layers.add,tf.layers.concatenate, etc.) - Tensorflow_Tfjs_Wrapper_Layers - Bidirectional wrapper that merges forward/backward RNN outputs
- Tensorflow_Tfjs_Backend_Operations -
K.concatenateandK.expandDimsused internally