All examples
Neural networks

Build a native CNN

Declare NCHW input geometry, compose inferred convolutional layers and retain an immutable fitted snapshot.

cnn_basics.aner
// A native CNN in NCHW format. Use --dev reports/cnn to inspect its architecture.
import aner.nn
import aner.tensor
import aner.dev

let model = nn.cnn(channels: 1, height: 8, width: 8, seed: 42)
    .conv2d(outputs: 4, padding: 1).batch_norm().relu()
    .max_pool2d().head(classes: 2)
    .on(device: "auto", precision: "Float32")
dev.capture("CNN architecture", model)
print(model.summary())

// Synthetic tensors demonstrate API mechanics; use held-out real images for evaluation.
let images = tensor.reshape(nn.random(8, 64, seed: 2026, scale: 1.0), List(8, 1, 8, 8))
let targets = nn.zeros(8, 1)
let fitted = model.fit(images, targets, epochs: 3, batch: 4, rate: 0.001)
print(fitted.last_loss())
print(fitted.predict(images).rows())
// Save into a new file, then continue later with optimizer and shuffle state retained:
// fitted.save("cnn.anercnn")
// let resumed = nn.load_cnn("cnn.anercnn").on("auto").fit(images, targets, epochs: 3)

Make it your experiment.

Use Aner 0.0.3 or newer. Save this program as examples/cnn_basics.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/cnn_basics.aner
aner run examples/cnn_basics.aner

The aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.

Requires Aner 0.0.4 or newer and matching editor integration. Uses synthetic tensors with no external dataset, credentials or network connection. Supported Mac packages include Metal/MPS; current Linux and Windows packages do not include CUDA or MPI/NCCL. Optional source backends require a compatible runtime and hardware.

Credential setup
aner check examples/cnn_basics.aner
aner examples/cnn_basics.aner

Bundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner