All examples
Neural networks

Portable Tensor precision policies

Run the same seeded source with preserved Float64, required compatibility adaptation, or intentional Float32 selection; inspect requested and actual execution before and after training.

Download .aner source
tensor_precision.aner
// Select the execution profile with CLI flags or nn.configure in a notebook.
// Default: CPU Float64. Use --device auto for an available accelerator.
import aner.nn
import aner.dev

print(nn.execution())
let source: Tensor = [[1.00000001]]
let placed = source.place()
print(placed.dtype())
print(placed.device())
print(placed.backend())
print(nn.value(placed, row: 0, column: 0))

let raw_input: Tensor = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]
let raw_target: Tensor = [[1.0, 0.0], [0.0, 1.0]]
let input = raw_input.place()
let target = raw_target.place()
var weights = nn.parameter(nn.random(rows: 3, cols: 2, seed: 42, scale: 0.1).place())
var first_loss = 0.0
var epoch = 0
while epoch < 8 {
    let prediction = nn.matmul(input, weights)
    let loss = nn.mse(prediction, target)
    if epoch == 0 {
        first_loss = nn.item(loss)
    }
    nn.backward(loss)
    dev.capture(label: "precision training", value: loss)
    weights = nn.sgd(weights, rate: 0.1)
    epoch = epoch + 1
}
let final_loss = nn.mse(nn.matmul(input, weights), target)
nn.synchronize(weights.device())
print(first_loss)
print(nn.item(final_loss))
print(weights.dtype())
print(weights.device())

Make it your experiment.

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

Terminal
aner check examples/tensor_precision.aner
aner run examples/tensor_precision.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.30 or newer. Execution defaults to CPU Float64 preserve. Choose --device auto --precision Float64 --precision-policy preserve, adapt, or fast without changing the numerical source. CPU and compiled CUDA support Float64/Float32; native Metal/MPS supports Float32, so preserve uses reported CPU Float64 fallback. Explicit unavailable accelerators fail. Precision changes and fallback remain visible in Dev and Prod. See Tensor devices and performance for exact policy, conversion, and complexity contracts.

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