deeplearning4j/deeplearning4j · error · UnsupportedOperationException
Cannot merge time series with mask rank
Error message
Cannot merge time series with mask rank
What it means
When merging time series that need masks (different lengths or explicit masks present), DataSetUtil.mergeTimeSeries supports only rank-2 (per-example, per-time-step) and rank-3 (per-output) masks. Any other mask rank reaches the final else branch and an UnsupportedOperationException 'Cannot merge time series with mask rank N' is thrown. Note rank-3 feature masks are documented as unsupported, so unusual mask construction is typically the cause.
Solutions
- Confirm each mask is rank 2 ([mb, timeSteps]) or rank 3 ([mb, numOutputs, timeSteps]); reshape it before merging.
- Replace rank-1 masks with their 2d equivalent via mask.reshape(mb, timeSteps).
- If the masks come from a custom layer/output, regenerate them in the standard per-time-step (2d) format used by RNN layers.
- If the mask is genuinely unsupported (e.g. per-input 3d feature masks), drop it (pass null) and handle masking manually after the merge.
Example fix
// before
INDArray m = Nd4j.create(4, 20, 10, 1); // rank 4
Pair<INDArray, INDArray> p = DataSetUtil.mergeTimeSeries(new INDArray[]{ts}, new INDArray[]{m});
// after
INDArray m = Nd4j.create(4, 20, 10, 1);
INDArray m2 = m.rank() == 2 || m.rank() == 3 ? m : m.reshape(m.size(0), m.size(m.rank()-1)); // -> rank 2 [mb, steps]
Pair<INDArray, INDArray> p = DataSetUtil.mergeTimeSeries(new INDArray[]{ts}, new INDArray[]{m2}); Defensive patterns
Strategy: validation
Validate before calling
for (INDArray m : masks)
if (m != null && m.rank() != 2 && m.rank() != 3)
throw new IllegalArgumentException("Time-series mask must be rank 2 or 3, got rank " + m.rank()); Type guard
boolean isValidTsMask(INDArray m) { return m == null || m.rank() == 2 || m.rank() == 3; } Try / catch
try {
p = DataSetUtil.mergeTimeSeries(arrays, masks);
} catch (UnsupportedOperationException e) {
if (e.getMessage().startsWith("Cannot merge time series with mask rank")) {
masks = java.util.Arrays.stream(masks).map(m -> (m == null || m.rank() <= 3) ? m : m.reshape(m.size(0), m.size(m.rank()-1))).toArray(INDArray[]::new);
p = DataSetUtil.mergeTimeSeries(arrays, masks);
} else throw e;
} Prevention
- Build time-series masks as rank 2 [mb, timeSteps] (standard) or rank 3 [mb, numOutputs, timeSteps] (per-output) only.
- Never reuse CNN/4d masks for time-series merges; regenerate them for the correct rank.
- Avoid squeeze/expandDims operations on masks in custom layers that change their rank.
- Check for null-vs-empty mask mistakes so wrong arrays do not land in the masks slot.
When it happens
Trigger: Calling DataSetUtil.mergeTimeSeries(arrays, masks) where a non-null mask has rank 1 or rank > 3 (e.g. a 4d mask, or a collapsed 1d per-example mask), while the merge takes the masked path (lengths differ or hasMask is true).
Common situations: Passing 4d CNN masks to time-series data; a mask reshaped/reshaped incorrectly upstream (e.g. squeezed to 1d); labels wrapped or expanded an extra dimension by a custom layer so the mask inherits rank 4; typo building the masks INDArray[][] so the wrong element is selected.
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 merge mask arrays: expected mask array of rank 4…
- Cannot merge time series with different size for dimension…
- Expected column vector mask array for LossCosineProximity…
- Mismatched mask shapes: masks should have same…
- With 3d (time series) input with shape [minibatch, size…
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/f4cd1b810fbef617.
Report an issue: GitHub.
Appendix: source
Thrown at nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/dataset/api/DataSetUtil.java:546
long thisNExamples = a.size(0);
long thisLength = a.size(2);
arr.put(new INDArrayIndex[] {NDArrayIndex.interval(examplesSoFar, examplesSoFar + thisNExamples),
NDArrayIndex.all(), NDArrayIndex.interval(0, thisLength)}, a);
if (m == null) {
//This mask is null -> equivalent to "all present"
mask.get(NDArrayIndex.interval(examplesSoFar, examplesSoFar + thisNExamples),
NDArrayIndex.all(), NDArrayIndex.interval(0, thisLength)).assign(1);
} else {
mask.put(new INDArrayIndex[] {
NDArrayIndex.interval(examplesSoFar, examplesSoFar + thisNExamples),
NDArrayIndex.all(), NDArrayIndex.interval(0, thisLength)}, m);
}
examplesSoFar += thisNExamples;
}
} else {
throw new UnsupportedOperationException("Cannot merge time series with mask rank " + maskRank);
}
}
return new Pair<>(arr, mask);
}
/**
* Merge the specified 4d arrays and masks. See {@link #mergeFeatures(INDArray[], INDArray[])}
* and {@link #mergeLabels(INDArray[], INDArray[])}
*
* @param arrays Arrays to merge
* @param masks Mask arrays to merge
* @param inOutIdx Index to extract out before merging
* @return Merged arrays and mask
*/
public static Pair<INDArray, INDArray> merge4d(INDArray[][] arrays, INDArray[][] masks, int inOutIdx) {
Pair<INDArray[], INDArray[]> p = selectColumnFromMDSData(arrays, masks, inOutIdx);
return merge4d(p.getFirst(), p.getSecond());View on GitHub (pinned to 4c22ac5fe4)