deeplearning4j/deeplearning4j · error · IllegalArgumentException

Both arrays must have the same length

Error message

Both arrays must have the same length

What it means

ArrayUtil.plus(int[] ints, int[] mult) performs element-wise addition of two int arrays. It throws IllegalArgumentException('Both arrays must have the same length') when lengths differ, since element-wise arithmetic has no defined pairing for extra elements. A strict precondition for a helper that assumes equally-sized operands.

Solutions

  1. Pad or trim one array so both have equal length before calling
  2. Use Nd4j broadcast operations instead if implicit broadcasting semantics are desired
  3. Fix the input generation so both vectors share the same dimension

Example fix

// before
int[] sum = ArrayUtil.plus(activations, bias); // different lengths
// after
int[] sum = activations.length == bias.length
        ? ArrayUtil.plus(activations, bias)
        : ArrayUtil.plus(activations, Arrays.copyOf(bias, activations.length));
Defensive patterns

Strategy: validation

Validate before calling

if (ints.length != mult.length) throw new IllegalArgumentException("plus() requires equal lengths: " + ints.length + " vs " + mult.length);

Type guard

boolean sameLength(int[] a, int[] b) { return a != null && b != null && a.length == b.length; }

Try / catch

try { int[] sum = ArrayUtil.plus(ints, mult); } catch (IllegalArgumentException e) { /* pad/trim or use broadcast ops */ }

Prevention

When it happens

Trigger: Calling ArrayUtil.plus(a, b) where a.length != b.length, e.g. plus(new int[]{1,2}, new int[]{1,2,3}); typically adding a bias vector to activations of different size, or shape vs stride arrays.

Common situations: Configuration drift between two vector inputs (feature count vs coefficient count); broadcasting expectations from other frameworks that silently extend shorter arrays; truncated arrays after serialization/deserialization.

Related errors


AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07). Data as JSON: /api/errors/8efe7c84853fe81b. Report an issue: GitHub.

Appendix: source

Thrown at nd4j/nd4j-common/src/main/java/org/nd4j/common/util/ArrayUtil.java:2928

        int backWardCount = second.length - 1;
        for (int i = 0; i < first.length; i++) {
            if (first[i] != second[backWardCount--])
                return false;
        }
        return true;
    }

    public static int[] plus(int[] ints, int mult) {
        int[] ret = new int[ints.length];
        for (int i = 0; i < ints.length; i++)
            ret[i] = ints[i] + mult;
        return ret;
    }


    public static int[] plus(int[] ints, int[] mult) {
        if (ints.length != mult.length)
            throw new IllegalArgumentException("Both arrays must have the same length");
        int[] ret = new int[ints.length];
        for (int i = 0; i < ints.length; i++)
            ret[i] = ints[i] + mult[i];
        return ret;
    }

    public static int[] times(int[] ints, int mult) {
        int[] ret = new int[ints.length];
        for (int i = 0; i < ints.length; i++)
            ret[i] = ints[i] * mult;
        return ret;
    }

    public static int[] times(int[] ints, int[] mult) {
        Preconditions.checkArgument(ints.length == mult.length, "Ints and mult must be the same length");
        int[] ret = new int[ints.length];
        for (int i = 0; i < ints.length; i++)
            ret[i] = ints[i] * mult[i];

View on GitHub (pinned to 4c22ac5fe4)