27
CNN
Convolution
Pooling
Neural Networks
CNN Layers
Convolutional neural networks (CNNs) process grid-structured data like images and time series. This example demonstrates Deepbox's 4 CNN layers: Conv1d (1D convolution for sequences — input shape [batch, channels, length]), Conv2d (2D convolution for images — input shape [batch, channels, height, width]), MaxPool2d (downsample by taking the maximum in each window), and AvgPool2d (downsample by averaging each window). For each layer, you create an instance, pass a tensor through it, and inspect the output shape to understand how kernel size, stride, and padding affect spatial dimensions. The example also shows how to build a small CNN by stacking Conv2d → ReLU → MaxPool2d layers.
Deepbox Modules Used
deepbox/ndarraydeepbox/nnWhat You Will Learn
- Conv1d for sequences (NLP, time series), Conv2d for images
- Kernel size controls the receptive field; padding='same' preserves spatial size
- MaxPool2d reduces spatial dimensions by 2x — keeps strongest activations
- Stack Conv → ReLU → Pool layers to build feature hierarchies
- Output spatial size = (input + 2*padding - kernel) / stride + 1
Source Code
27-cnn-layers/index.ts
1import { tensor, randn } from "deepbox/ndarray";2import { Conv1d, Conv2d, MaxPool2d, AvgPool2d, ReLU, Sequential } from "deepbox/nn";34console.log("=== CNN Layers ===\n");56// Conv1d: [batch=1, channels=3, length=10]7const seq = randn([1, 3, 10]);8const conv1d = new Conv1d(3, 8, 3); // 3→8 channels, kernel=39const out1d = conv1d.forward(seq);10console.log("Conv1d input:", seq.shape, "→ output:", out1d.shape);1112// Conv2d: [batch=1, channels=1, height=8, width=8]13const img = randn([1, 1, 8, 8]);14const conv2d = new Conv2d(1, 16, 3, { padding: 1 }); // 1→16 channels15const out2d = conv2d.forward(img);16console.log("Conv2d input:", img.shape, "→ output:", out2d.shape);1718// MaxPool2d: downsample by 2x19const pool = new MaxPool2d(2);20const pooled = pool.forward(out2d);21console.log("MaxPool2d:", out2d.shape, "→", pooled.shape);2223// AvgPool2d24const avgpool = new AvgPool2d(2);25const avgpooled = avgpool.forward(out2d);26console.log("AvgPool2d:", out2d.shape, "→", avgpooled.shape);2728// Mini CNN29const cnn = new Sequential(30 new Conv2d(1, 8, 3, { padding: 1 }),31 new ReLU(),32 new MaxPool2d(2),33 new Conv2d(8, 16, 3, { padding: 1 }),34 new ReLU(),35 new MaxPool2d(2),36);37const cnnOut = cnn.forward(img);38console.log("\nMini CNN: [1,1,8,8] →", cnnOut.shape);Console Output
$ npx tsx 27-cnn-layers/index.ts
=== CNN Layers ===
Conv1d input: [1, 3, 10] → output: [1, 8, 8]
Conv2d input: [1, 1, 8, 8] → output: [1, 16, 8, 8]
MaxPool2d: [1, 16, 8, 8] → [1, 16, 4, 4]
AvgPool2d: [1, 16, 8, 8] → [1, 16, 4, 4]
Mini CNN: [1,1,8,8] → [1, 16, 2, 2]