Convolutional Layers
CNN layers in Deeplearning4j — Conv1D/2D/3D, pooling, deconvolution, depthwise, separable, and upsampling layers
Overview
Deeplearning4j provides a complete set of convolutional layers covering 1D (sequences), 2D (images), and 3D (video/volumetric) convolutions. All CNN layers share common builder patterns and integrate with setInputType() for automatic shape inference and pre-processor insertion.
ConvolutionMode
ConvolutionMode controls how output spatial dimensions are computed relative to input size. Set it with .convolutionMode(ConvolutionMode.X) on any convolutional layer.
Truncated (default)
Output may be smaller than input if padding doesn't divide evenly; no truncation warning
Same
Output has the same spatial size as the input (appropriate zero-padding is added automatically)
Full
Output is larger than input; full convolution with padding
Causal
Causal (1D only): no future information leaks into past time steps
import org.deeplearning4j.nn.conf.ConvolutionMode;
new ConvolutionLayer.Builder(3, 3)
.convolutionMode(ConvolutionMode.Same)
.nIn(64).nOut(128)
.build()ConvolutionLayer (Conv2D)
Class: org.deeplearning4j.nn.conf.layers.ConvolutionLayer Source: ConvolutionLayer.java
Standard 2D convolution layer. Input shape: [minibatch, channels, height, width].
Builder Parameters
constructor (int, int)
int, int
required
Kernel height and width
nIn
int
auto
Number of input channels
nOut
int
required
Number of output filters
stride(int, int)
int, int
1, 1
Stride in height and width
padding(int, int)
int, int
0, 0
Zero-padding in height and width
dilation(int, int)
int, int
1, 1
Dilation (atrous convolution)
activation
Activation
RELU
Activation function
hasBias
boolean
true
Include bias
convolutionMode
ConvolutionMode
Truncated
How to handle border effects
cudnnAlgoMode
AlgoMode
PREFER_FASTEST
CuDNN algorithm selection
Example — Basic Conv2D
Example — Dilated (Atrous) Convolution
Full Network with setInputType
Convolution1DLayer (Conv1D)
Class: org.deeplearning4j.nn.conf.layers.Convolution1D (alias: Convolution1DLayer) Source: Convolution1D.java
1D convolution for sequence data. Input shape: [minibatch, channels, sequenceLength].
Builder Parameters
constructor (int)
int
required
Kernel size (length)
nIn
int
auto
Input channels (embedding dim)
nOut
int
required
Number of filters
stride(int)
int
1
Stride along sequence
padding(int)
int
0
Zero-padding on each side
dilation(int)
int
1
Dilation along sequence
convolutionMode
ConvolutionMode
Truncated
Border handling
Example — Text Classification
Convolution3D
Class: org.deeplearning4j.nn.conf.layers.Convolution3D Source: Convolution3D.java
3D convolution for volumetric data (e.g., video, medical imaging). Supports two data formats.
Builder Parameters
kernelSize(int...)
int[3]
required
Kernel in (depth, height, width)
stride(int...)
int[3]
1,1,1
Stride in (depth, height, width)
padding(int...)
int[3]
0,0,0
Padding in (depth, height, width)
dilation(int...)
int[3]
1,1,1
Dilation in (depth, height, width)
dataFormat
DataFormat
NCDHW
NCDHW (channels first) or NDHWC (channels last)
hasBias
boolean
true
Include bias
Example
SubsamplingLayer (Pooling2D)
Class: org.deeplearning4j.nn.conf.layers.SubsamplingLayer Source: SubsamplingLayer.java
Standard 2D pooling layer (max, average, sum, or p-norm). Input shape: [minibatch, channels, height, width]. No learnable parameters.
Builder Parameters
constructor PoolingType
PoolingType
required
MAX, AVG, SUM, PNORM, NONE
kernelSize(int, int)
int, int
required
Pooling window height, width
stride(int, int)
int, int
same as kernelSize
Stride
padding(int, int)
int, int
0, 0
Padding
convolutionMode
ConvolutionMode
Truncated
Border handling
pnorm
int
2
P-norm exponent (only for PNORM)
Example
Subsampling1DLayer (Pooling1D)
Class: org.deeplearning4j.nn.conf.layers.Subsampling1DLayer
1D temporal pooling. Input shape: [minibatch, channels, sequenceLength]. Supports MAX, AVG, SUM, PNORM.
GlobalPoolingLayer
See GlobalPoolingLayer in the Layers Reference for full documentation. In the CNN context:
Deconvolution2D (Transposed Convolution)
Class: org.deeplearning4j.nn.conf.layers.Deconvolution2D Source: Deconvolution2D.java
Transposed convolution (also called fractionally strided convolution or deconvolution). Swaps the forward and backward passes of a regular ConvolutionLayer, producing outputs that are typically larger than the input. Used in decoders, segmentation networks, and generative models.
Reference: Zeiler et al., "Deconvolutional Networks" (CVPR 2010).
Builder Parameters
Same as ConvolutionLayer — kernelSize, stride, padding, nIn, nOut, activation, convolutionMode, hasBias.
Example — Upsampling Decoder Block
DepthwiseConvolution2D
Class: org.deeplearning4j.nn.conf.layers.DepthwiseConvolution2D Source: DepthwiseConvolution2D.java
Applies a separate convolutional filter to each input channel independently (no cross-channel mixing). Used as the first step in MobileNet-style depthwise separable convolutions.
Builder Parameters
nIn
int
Number of input channels
depthMultiplier
int
Number of output channels per input channel (nOut = nIn * depthMultiplier)
kernelSize(int, int)
int, int
Kernel size
stride(int, int)
int, int
Stride
convolutionMode
ConvolutionMode
Border handling
Example
SeparableConvolution2D
Class: org.deeplearning4j.nn.conf.layers.SeparableConvolution2D Source: SeparableConvolution2D.java
Combines a depthwise convolution with a 1x1 pointwise convolution for cross-channel mixing. Approximates a standard convolution with far fewer parameters — the MobileNet building block.
Builder Parameters
nIn
int
Input channels
nOut
int
Output channels (after pointwise conv)
kernelSize(int, int)
int, int
Depthwise kernel size
depthMultiplier
int
Intermediate channels = nIn * depthMultiplier
stride(int, int)
int, int
Stride for depthwise conv
convolutionMode
ConvolutionMode
Border handling
Example
Upsampling2D
Class: org.deeplearning4j.nn.conf.layers.Upsampling2D Source: Upsampling2D.java
Nearest-neighbor upsampling — repeats each value size times in both the height and width dimensions. No learnable parameters. Used in U-Net decoders and image segmentation.
Input [mb, C, H, W] with size=2 -> output [mb, C, 2H, 2W].
Builder Parameters
size(int)
int
Upsampling factor (applied to both H and W)
size(int[])
int[2]
Separate height and width factors
Example
Upsampling1D
Class: org.deeplearning4j.nn.conf.layers.Upsampling1D
Repeats each value along the sequence dimension. Input [mb, C, L] -> output [mb, C, size*L].
Upsampling3D
Class: org.deeplearning4j.nn.conf.layers.Upsampling3D
Repeats values in depth, height, and width. Input [mb, C, D, H, W] -> output [mb, C, size*D, size*H, size*W].
ZeroPaddingLayer (2D)
Class: org.deeplearning4j.nn.conf.layers.ZeroPaddingLayer Source: ZeroPaddingLayer.java
Adds zero-padding explicitly around 2D spatial inputs. Useful when you need precise control over padding at a specific point in a ComputationGraph, separate from a convolution layer's built-in padding.
ZeroPadding1DLayer
Adds zero-padding to the start and end of 1D sequences.
ZeroPadding3DLayer
Adds zero-padding to volumetric data. Accepts a 6-element array: [padLeftD, padRightD, padLeftH, padRightH, padLeftW, padRightW].
Cropping Layers
Cropping1D
Removes n time steps from the start and/or end of sequences.
Cropping2D
Removes rows/columns from 2D spatial inputs.
Cropping3D
SpaceToDepthLayer
Class: org.deeplearning4j.nn.conf.layers.SpaceToDepthLayer
Rearranges spatial blocks into the channel (depth) dimension. Downsamples spatial size while increasing channel count without any learned parameters. Used in certain detection architectures (YOLO passthrough layer).
Input [mb, C, H, W] with block size b -> output [mb, C * b * b, H/b, W/b].
Yolo2OutputLayer
For object detection with the YOLOv2 architecture:
See the DL4J object detection examples for a full YOLO pipeline.
Complete CNN Architecture Example
A simple VGG-style classifier for CIFAR-10:
Last updated
Was this helpful?