deeplearning4j/deeplearning4j · error · IllegalArgumentException

Kernel argument should be a [dim]d array, got [kernelSize]

Error message

Kernel argument should be a [dim]d array, got [kernelSize]

What it means

Thrown by the ConvolutionLayer constructor (deeplearning4j-nn, ConvolutionLayer.java:130) as an IllegalArgumentException when the kernelSize array's length does not match the layer's convolution dimension (2 for Convolution2D, 3 for Convolution3D). DL4J validates all shape-configuration arrays up front so misconfiguration fails at network construction rather than during training. The message shows the expected dimensionality and the actual array contents via Arrays.toString.

Solutions

  1. Count the values passed to kernelSize(...) and make the length match the layer's convolution dimension: 2 for 2D convolution (e.g. kernelSize(3,3)), 3 for 3D (e.g. kernelSize(3,3,3)).
  2. If you build configs programmatically, check kernelSize.length against the intended dim before constructing the layer.
  3. Use the single-value convenience form (e.g. kernelSize(3)) which DL4J expands to a square kernel of the right dimensionality.

Example fix

// before
new ConvolutionLayer.Builder(1, 2, 3) // 3 values on a 2D layer
    .nIn(1).nOut(32).build();
// after
new ConvolutionLayer.Builder(3, 3) // 2 values for 2D convolution
    .nIn(1).nOut(32).build();
Defensive patterns

Strategy: validation

Validate before calling

long[] kernelSize = ...;
int convDim = 2; // 3 for Convolution3D
if (kernelSize.length != convDim)
    throw new IllegalArgumentException("kernelSize must have " + convDim + " values, got " + kernelSize.length);

Try / catch

try {
    layer = new ConvolutionLayer.Builder(kernelSize).nOut(32).build();
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("Kernel argument")) {
        kernelSize = Arrays.copyOf(kernelSize, convDim);
        layer = new ConvolutionLayer.Builder(kernelSize).nOut(32).build();
    } else throw e;
}

Prevention

When it happens

Trigger: Calling the ConvolutionLayer.Builder with kernelSize(...) whose array length differs from the dimension implied by the builder variant, e.g. new ConvolutionLayer.Builder(1,2,3) (3 values) on a 2D convolution where dim==2, or a 2-element kernelSize on a 3D layer.

Common situations: Copy-pasting config between Convolution2D and Convolution3D; specifying per-dimension values like {height,width,depth} for a 2D layer; programmatic generation of layer configs with variable-length arrays; upgrading code where the old Builder(int...) convenience constructor expected a different arity.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/ConvolutionLayer.java:130

    protected AlgoMode cudnnAlgoMode = AlgoMode.PREFER_FASTEST;
    protected FwdAlgo cudnnFwdAlgo;
    protected BwdFilterAlgo cudnnBwdFilterAlgo;
    protected BwdDataAlgo cudnnBwdDataAlgo;

    /**
     * ConvolutionLayer nIn in the input layer is the number of channels nOut is the number of filters to be used in the
     * net or in other words the channels The builder specifies the filter/kernel size, the stride and padding The
     * pooling layer takes the kernel size
     */
    protected ConvolutionLayer(BaseConvBuilder<?> builder) {
        super(builder);
        long dim = builder.convolutionDim;

        this.hasBias = builder.hasBias;
        this.convolutionMode = builder.convolutionMode;
        this.dilation = builder.dilation;
        if (builder.kernelSize.length != dim) {
            throw new IllegalArgumentException("Kernel argument should be a " + dim + "d array, got " + Arrays.toString(builder.kernelSize));
        }
        this.kernelSize = builder.kernelSize;
        if (builder.stride.length != dim) {
            throw new IllegalArgumentException("Strides argument should be a " + dim + "d array, got " + Arrays.toString(builder.stride));
        }
        this.stride = builder.stride;
        if (builder.padding.length != dim) {
            throw new IllegalArgumentException("Padding argument should be a " + dim + "d array, got " + Arrays.toString(builder.padding));
        }
        this.padding = builder.padding;
        if (builder.dilation.length != dim) {
            throw new IllegalArgumentException("Dilation argument should be a " + dim + "d array, got " + Arrays.toString(builder.dilation));
        }
        this.dilation = builder.dilation;
        this.cudnnAlgoMode = builder.cudnnAlgoMode;
        this.cudnnFwdAlgo = builder.cudnnFwdAlgo;
        this.cudnnBwdFilterAlgo = builder.cudnnBwdFilterAlgo;
        this.cudnnBwdDataAlgo = builder.cudnnBwdDataAlgo;

View on GitHub (pinned to 4c22ac5fe4)