deeplearning4j/deeplearning4j · error · IllegalStateException
Cannot merge MultiDataSets with different number of input…
Error message
Cannot merge MultiDataSets with different number of input arrays: toMerge[0] has
What it means
MultiDataSet.merge() combines a list of MultiDataSets into one batched MultiDataSet by concatenating features/labels along the example dimension. It requires every MultiDataSet in the list to have the same number of feature and label arrays as the first one (toMerge[0]). If any element has null features or a different array count, this IllegalStateException is thrown so a silently mis-shaped batch is never produced.
Solutions
- Verify every MultiDataSet in the list has the same number of non-null feature and label arrays before calling merge
- Fix the data pipeline so each record produces the same count of input/output arrays
- If features are legitimately absent, supply zero-size placeholder arrays instead of null
- Log shapes of all toMerge elements to find the first offending index (the message reports toMerge[i])
- Split the merge into separate groups by array count if heterogeneous data is intentional
Example fix
// before
MultiDataSet bad = new MultiDataSet(new INDArray[]{features}, null); // labels null/missing
MultiDataSet merged = MultiDataSet.merge(Arrays.asList(good3In, bad));
// after
INDArray[] zeroLabels = new INDArray[]{Nd4j.zeros(1, numClasses)};
MultiDataSet fixed = new MultiDataSet(new INDArray[]{features}, zeroLabels);
MultiDataSet merged = MultiDataSet.merge(Arrays.asList(good3In, fixed)); Defensive patterns
Strategy: validation
Validate before calling
if (toMerge.isEmpty()) throw new IllegalArgumentException("empty list");
int nIn = toMerge.get(0).getFeatures() == null ? 0 : toMerge.get(0).getFeatures().length;
int nOut = toMerge.get(0).getLabels() == null ? 0 : toMerge.get(0).getLabels().length;
for (int i = 0; i < toMerge.size(); i++) {
MultiDataSet m = toMerge.get(i);
if (m.getFeatures() == null || m.getFeatures().length != nIn || m.getLabels() == null || m.getLabels().length != nOut)
throw new IllegalStateException("MultiDataSet at index " + i + " has incompatible array counts");
} Try / catch
try { merged = MultiDataSet.merge(toMerge); } catch (IllegalStateException e) { log.error("Merge failed, check array counts: {}", e.getMessage()); throw e; } Prevention
- Always construct MultiDataSets with the same number of feature/label arrays in a given pipeline
- Assert array counts in dataset-producing code with Preconditions
- Never leave getLabels() null when others are non-null; use placeholders
- Unit-test your batching code with a representative list before merge
When it happens
Trigger: Calling MultiDataSet.merge(List<MultiDataSet>) where toMerge contains MultiDataSets whose getFeatures() (or getLabels()) is null or whose features.length differs from the first element's, e.g. mixing MultiDataSets created with 2 input arrays with ones created with 3.
Common situations: Batching data from heterogeneous pipelines (some records missing label arrays), mixing outputs of different network configurations, feeding MultiDataSet objects produced by different iterators into one merge during mini-batch creation, or a null features array due to an earlier failed preprocessing step.
Understand the failure class
Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.
Related errors
- Cannot merge MultiDataSets with different number of output…
- Cannot merge EvaluationBinary instances with different…
- Array shapes are not broadcastable: [X] vs. [Y]
- Both arrays must have the same length
- C does not match outRows or outCols
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/f9bebad9203d596f.
Report an issue: GitHub.
Appendix: source
Thrown at nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/dataset/MultiDataSet.java:494
INDArray[][] features = new INDArray[nonEmpty][0];
INDArray[][] labels = new INDArray[nonEmpty][0];
INDArray[][] featuresMasks = new INDArray[nonEmpty][0];
INDArray[][] labelsMasks = new INDArray[nonEmpty][0];
int i = 0;
for (org.nd4j.linalg.dataset.api.MultiDataSet mds : list) {
if(mds.isEmpty()){
continue;
}
features[i] = mds.getFeatures();
labels[i] = mds.getLabels();
featuresMasks[i] = mds.getFeaturesMaskArrays();
labelsMasks[i] = mds.getLabelsMaskArrays();
if (features[i] == null || features[i].length != nInArrays) {
throw new IllegalStateException(
"Cannot merge MultiDataSets with different number of input arrays: toMerge[0] has "
+ nInArrays + " input arrays; toMerge[" + i + "] has "
+ (features[i] != null ? features[i].length : null) + " arrays");
}
if (labels[i] == null || labels[i].length != nOutArrays) {
throw new IllegalStateException(
"Cannot merge MultiDataSets with different number of output arrays: toMerge[0] has "
+ nOutArrays + " output arrays; toMerge[" + i + "] has "
+ (labels[i] != null ? labels[i].length : null) + " arrays");
}
i++;
}
//Now, merge:
INDArray[] mergedFeatures = new INDArray[nInArrays];
INDArray[] mergedLabels = new INDArray[nOutArrays];
INDArray[] mergedFeaturesMasks = new INDArray[nInArrays];View on GitHub (pinned to 4c22ac5fe4)