{"record":{"id":"f846552c4cbac3d3","repo":"deeplearning4j/deeplearning4j","slug":"kernel-argument-should-be-a-dim-d-array-got-ker","errorCode":null,"errorMessage":"Kernel argument should be a [dim]d array, got [kernelSize]","messagePattern":"Kernel argument should be a \\[dim\\]d array, got \\[kernelSize\\]","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/ConvolutionLayer.java","lineNumber":130,"sourceCode":"    protected AlgoMode cudnnAlgoMode = AlgoMode.PREFER_FASTEST;\n    protected FwdAlgo cudnnFwdAlgo;\n    protected BwdFilterAlgo cudnnBwdFilterAlgo;\n    protected BwdDataAlgo cudnnBwdDataAlgo;\n\n    /**\n     * ConvolutionLayer nIn in the input layer is the number of channels nOut is the number of filters to be used in the\n     * net or in other words the channels The builder specifies the filter/kernel size, the stride and padding The\n     * pooling layer takes the kernel size\n     */\n    protected ConvolutionLayer(BaseConvBuilder<?> builder) {\n        super(builder);\n        long dim = builder.convolutionDim;\n\n        this.hasBias = builder.hasBias;\n        this.convolutionMode = builder.convolutionMode;\n        this.dilation = builder.dilation;\n        if (builder.kernelSize.length != dim) {\n            throw new IllegalArgumentException(\"Kernel argument should be a \" + dim + \"d array, got \" + Arrays.toString(builder.kernelSize));\n        }\n        this.kernelSize = builder.kernelSize;\n        if (builder.stride.length != dim) {\n            throw new IllegalArgumentException(\"Strides argument should be a \" + dim + \"d array, got \" + Arrays.toString(builder.stride));\n        }\n        this.stride = builder.stride;\n        if (builder.padding.length != dim) {\n            throw new IllegalArgumentException(\"Padding argument should be a \" + dim + \"d array, got \" + Arrays.toString(builder.padding));\n        }\n        this.padding = builder.padding;\n        if (builder.dilation.length != dim) {\n            throw new IllegalArgumentException(\"Dilation argument should be a \" + dim + \"d array, got \" + Arrays.toString(builder.dilation));\n        }\n        this.dilation = builder.dilation;\n        this.cudnnAlgoMode = builder.cudnnAlgoMode;\n        this.cudnnFwdAlgo = builder.cudnnFwdAlgo;\n        this.cudnnBwdFilterAlgo = builder.cudnnBwdFilterAlgo;\n        this.cudnnBwdDataAlgo = builder.cudnnBwdDataAlgo;","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/ConvolutionLayer.java#L112-L148","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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)).","If you build configs programmatically, check kernelSize.length against the intended dim before constructing the layer.","Use the single-value convenience form (e.g. kernelSize(3)) which DL4J expands to a square kernel of the right dimensionality."],"exampleFix":"// before\nnew ConvolutionLayer.Builder(1, 2, 3) // 3 values on a 2D layer\n    .nIn(1).nOut(32).build();\n// after\nnew ConvolutionLayer.Builder(3, 3) // 2 values for 2D convolution\n    .nIn(1).nOut(32).build();","handlingStrategy":"validation","validationCode":"long[] kernelSize = ...;\nint convDim = 2; // 3 for Convolution3D\nif (kernelSize.length != convDim)\n    throw new IllegalArgumentException(\"kernelSize must have \" + convDim + \" values, got \" + kernelSize.length);","typeGuard":null,"tryCatchPattern":"try {\n    layer = new ConvolutionLayer.Builder(kernelSize).nOut(32).build();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"Kernel argument\")) {\n        kernelSize = Arrays.copyOf(kernelSize, convDim);\n        layer = new ConvolutionLayer.Builder(kernelSize).nOut(32).build();\n    } else throw e;\n}","preventionTips":["Match kernelSize/stride/padding/dilation lengths to the convolution dimension (2 or 3).","Use the scalar convenience forms (kernelSize(3)) that expand per dimension.","Unit-test network construction so config errors surface at build time, not fit time."],"tags":["deeplearning4j","java","convolution","array-length"],"backgroundTag":"invalid-argument-value","analyzedSha":"4c22ac5fe4a8350d05d224e7f4499429f7f69c93","analyzedAt":"2026-09-07T05:27:04.714Z","contentChangedAt":"2026-09-07T05:27:04.714Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}