All examples
Neural networks

Compare native classifier devices

Fit the same seeded Iris classifier on CPU and available MPS or CUDA, report placement and precision, and inspect loss, duration, and held out accuracy.

Download .aner source
neural_gpu.aner
// CPU and available accelerator classifiers use the same data and architecture.
// CPU uses Float64; GPU uses Float32. Compare metrics within numerical tolerance.
// For matched Float32 Tensor throughput, use the native aner_gpu_benchmark tool.
import aner.dataset
import aner.nn
import aner.ml
import aner.dev

fn train(device: String, split: DatasetSplit, network: NeuralNetwork) {
    let plan = ml.classifier(network, standardize: true,
        loss: "cross_entropy", optimizer: "adam").on(device: device)
    let fitted = plan.fit(split.train(), epochs: 20, rate: 0.02, progress: true)
    print(device)
    print(fitted.weights(0).device())
    print(fitted.weights(0).dtype())
    print(fitted.weights(0).backend())
    print(fitted.training_seconds())
    print(fitted.initial_loss())
    print(fitted.training_loss())
    print(fitted.evaluate(split.test()).accuracy())
    dev.capture("trained classifier", fitted)
    dev.capture("resident classifier weights", fitted.weights(0))
}

print(nn.devices())
let split = dataset.iris().split(test: 0.2, seed: 2026)
let network = nn.sequential(seed: 42)
    .dense(inputs: 4, outputs: 16, initialization: "he").relu()
    .dense(inputs: 16, outputs: 3, initialization: "xavier")
train("cpu", split, network)
if nn.device_available("mps") { train("mps", split, network) }
if nn.device_available("cuda") { train("cuda", split, network) }

Make it your experiment.

With Aner installed, save this program as examples/neural_gpu.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/neural_gpu.aner
aner run examples/neural_gpu.aner

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

Use the updated native Aner interpreter and VS Code extension 0.1.29 or newer. CPU is always available; MPS requires a compatible macOS Metal build, and CUDA requires a compatible NVIDIA driver and CUDA enabled Aner build. Explicit GPU Tensor transfer requires Float32. These examples query availability and label unavailable backends as skipped. See Tensor devices and performance for prerequisites, synchronization, complexity, and matched measurements.

Iris and Wine are teaching datasets with their own attribution. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner