CNN model parameters
Inspect image geometry, every layer option, architecture settings and the native fitting and precision contracts.
Released in Aner 0.0.5Aner 0.0.5 or newerThis reference targets Aner 0.0.5 or newer. Updating documentation or editor help alone does not upgrade an installed runtime; check Downloads for verified platform packages.
This is the public Aner source reference for immutable CNNModel values in aner.nn. Start from Neural networks, the CNN workflow, or the model directory. The API inventory records every registered overload. The mathematical Tensor nn.conv2d primitive has a different signature from the model builder described here.
Model creation and architecture variables
import aner.nn
let model = nn.cnn(channels: 1, height: 8, width: 8, seed: 42)
.conv2d(outputs: 4, kernel: 3, stride: 1, padding: 1)
.relu().global_pool().flatten()
.dropout(probability: 0.2).dense(outputs: 2)
print(model.summary())
print(model.parameter_count())Construction declares shapes and does not allocate initialized weights. prepare, fitting, inference or parameter inspection initializes the relevant snapshot. Configure topology before initialization: adding layers to an initialized/trained snapshot is rejected. Builders return new values; they do not mutate an earlier model.
| Call / parameter | Type and source default | Meaning and constraints |
|---|---|---|
nn.cnn(channels, height, width, seed) | Three required positive Int64 extents; seed = 42 | Declared single image geometry [C,H,W]. Execution input is [N,C,H,W], not NHWC. Shape products are bounded. Seed owns model initialization and its model specific dropout derivation. |
nn.vision(architecture, classes, seed) | Required String; classes: Int64 = 1000, seed: Int64 = 42 | Construct the exact named native architecture, including its declared input geometry and classifier. nn.architectures() lists supported names; inspect summary() rather than assuming any image size works. No pretrained weights are downloaded. |
.conv2d(outputs, kernel, stride, padding, groups, dilation) | outputs required; kernel=3, stride=1, padding=0, groups=1, dilation=1 | Positive output channels; square kernel, stride and dilation in 1..1024; symmetric padding in 0..1024. Groups partition input/output channels and must divide both. Builder infers output geometry and rejects nonpositive results. |
.max_pool2d(kernel, stride, padding) | 2, 2, 0 | Square maximum pooling; kernel/stride 1..1024, padding 0..floor(kernel/2) and valid output geometry. |
.avg_pool2d(kernel, stride, padding) | 2, 2, 0 | Square average pooling with the implemented include padding divisor. It is not a configurable pooling divisor policy. |
.relu(), .gelu() | No extra arguments | Shape preserving activation. These do not add learned parameters. |
.batch_norm() | No source arguments | Per channel normalization with learned affine parameters and retained running mean/variance. Running statistics update only during permitted training. Public builder does not expose epsilon or momentum knobs. |
.global_pool() | No extra arguments | Adaptive average pooling to one spatial value per channel. |
.flatten() | No extra arguments | Preserve batch and flatten the remaining logical dimensions for a Dense classifier. |
.dense(outputs) | Required Int64 in 1..100000 | Inferred input width; output is raw class logits. Fitting/inference requires a terminal Dense stage. |
.head(classes) | Required Int64 in 1..100000 | Add a global-pool/flatten/classifier head when needed, or replace a terminal classifier for a new task. Replacing it resets training/optimizer counters and run metadata; it is not continuation of the old head. |
.dropout(probability) | Float64 = 0.5 | Finite [0,1), elementwise inverted dropout in training; inference is identity. Sampling uses model seed, attempted update, node and stable sample identity. This is separate from the Dense plan's .dropout_seed API. |
For one spatial dimension, convolution computes floor((input + 2*padding - dilation*(kernel-1) - 1)/stride) + 1. Infer both height and width this way; checked geometry and tensor/work budgets can reject a large combination. A group count equal to input channels admits depthwise style grouping only when output divisibility also holds. The architecture registry caps combined parameter/buffer storage at 150M elements; per operation and graph budgets can reject a smaller declared network.
Placement, compute policy and resource variables
| Call / parameter | Default | Contract and effect |
|---|---|---|
.on(device, precision) | device="auto", precision="Float32" | Explicit family placement. Auto prefers available CUDA, then MPS, then CPU. CPU/CUDA support admitted Float32/Float64; MPS Float32. Unsupported explicit dtype/device/ordinal fails. |
.performance(amp, matmul, collectives) | "off", "strict", "auto" | Select compute/collective policy without changing storage precision. |
amp: String | "off" | "off", "fp16", "bf16". Mixed GEMM operands require CUDA Float32 storage and hardware capability. FP16 retains dynamic loss scaling; BF16 uses scale one. |
matmul: String | "strict" | "strict" or "tf32"; TF32 requires supported CUDA Float32. AMP and TF32 cannot be selected together. |
collectives: String | "auto" | "auto", "mpi", "nccl"; relates to admitted multi process gradient communication, not a general Tensor device. |
.budget(tensor_elements, graph_elements) | Model starts at 64,000,000 / 256,000,000 | Required explicit positive Int64 values when called: tensor limit at most 256,000,000; graph limit between tensor limit and 2,000,000,000. Includes numeric temporaries and saved differentiation values. Not a physical RAM/VRAM quota. |
.freeze_backbone(frozen) | true | Train only terminal classifier parameters and freeze backbone BatchNorm buffers. Change trainable selection before training; a new head/weight import is a new run. |
.freeze_batch_norm(frozen) | true | Use retained running statistics during training instead of per batch updates. Required for admitted parallel training with BatchNorm unless the whole backbone is frozen; synchronized BatchNorm is not implemented. |
performance_summary(), device(), dtype(), loss_scale(), attempts(), updates() and skipped_updates() disclose actual policy and state. Selecting a policy does not guarantee a speedup or any particular GPU instruction. Device performance and CNN execution details explain numerical and collective boundaries.
Every source fit variable
model.fit(images, targets, epochs=1, batch=32, rate=0.001,
seed=42, optimizer="adam")The = notation above summarizes implemented shorter overloads. The explicit execution overload adds a ParallelContext. Positional calls use the complete prefix; named calls can supply execution while the checker fills earlier optional fit defaults.
| Argument | Type / range | Interpretation |
|---|---|---|
images | Required detached Tensor, [N,C,H,W] | Must match declared channels/height/width. The model prepares values according to its selected profile. Keep image scaling and any preprocessing explicit. |
targets | Required detached Tensor | [N,1] exact zero based class IDs below the head width, or [N,classes] exact one hot rows. Class order is the caller's retained contract. |
epochs | Int64, 1..100000, default 1 | Additional full passes. |
batch | Int64, 1..100000, default 32 | Maximum samples per update; partial final batches are retained. |
rate | Finite positive Float64, default 0.001 | Learning rate, representable in the selected storage/compute contract. |
seed | Int64, default 42 | Retained shuffle seed, separate from the model's initialization seed. Resume requires matching images, labels, row order and shuffle seed. |
optimizer | String, default "adam" | "adam" or "sgd". Matching Adam retains moments; changing optimizer deliberately resets its optimizer state. |
execution | Explicit ParallelContext overload | Choose admitted local replicas or configured collective execution. Global sample gradients are summed/count-weighted once. |
The native CNN fitting implementation uses global norm clipping 1, weight decay zero and shuffling enabled for these registered source overloads. Its native FitOptions structure has additional fields, but clip_norm, weight_decay and shuffle are not public CNN source .fit arguments. The recurrent model has a different, larger fit signature.
A run admits at most 1,000,000 attempted batch updates, including attempts whose FP16 overflow skips the update. Parameters, moments and successful update count do not advance on a skipped update. Check skipped_updates() rather than treating every attempt as optimization progress. CNN fitting accepts raw tensors and does not automatically enforce Dataset partition roles or fit a standardizer; retain split IDs and preprocessing yourself.
Results, weights and continuation
logits(images) returns detached [N,classes], probabilities(images) applies softmax, predict(images) returns [N,1] class IDs, and accuracy(images, targets) measures the caller supplied labels. These calls disable training dropout and use retained BatchNorm buffers. last_loss() is the recorded training observation; it is not a separately held out validation metric. Evaluate independently retained validation data explicitly.
parameter_names() and parameter(name) inspect exact named weights. load_weights(path) imports compatible local Safetensors values into the declared topology and starts the appropriate fresh training state; it does not download a model. read_images(path, samples) reads a raw little endian Float32 or Float64 NCHW payload in the model's selected dtype. path is required; positive samples requires exactly samples*C*H*W finite values. It does not decode PNG/JPEG files or infer normalization.
save(path) exclusively creates a family specific checkpoint containing topology, parameters, BatchNorm buffers, optimizer moments, successful/attempted/shuffle counters and performance/scaler policy. nn.load_cnn(path) restores CPU portable storage; select placement explicitly afterward. Use identical data/order, seeds and numerical implementation for exact continuation. This format is independent of Dense classifier v7.
Run CNN basics, architecture inspection, and CPU replica training to inspect complete native workflows. Parameter counts describe stored values; convolution's im2col workspace, saved activations, gradients and optimizer moments can dominate memory. Use smaller batches/spatial maps and inspect declared budgets before increasing them.