Build a native CNN
Declare NCHW input geometry, compose inferred convolutional layers and retain an immutable fitted snapshot.
// 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.
With Aner 0.0.3 or newer installed, 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.
aner check examples/cnn_basics.aner
aner run examples/cnn_basics.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Requires the matching neural development runtime and editor; published 0.0.3 packages predate these APIs. Uses synthetic tensors with no external dataset, credentials or network connection. Optional CUDA/MPS placement requires a compatible native build and hardware.
aner check examples/cnn_basics.aner
aner examples/cnn_basics.anerBundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.
Dataset sources & attribution