Complete reference for all Axiom.jl functions, types, and macros
The fundamental data structure for all neural network operations.
struct Tensor{T<:Number, Shape<:Tuple}
data::Array{T}
endType Parameters:
T: Element type (e.g.,Float32,Float64,Float16)Shape: Tuple of dimensions (e.g.,(784,),(32, 28, 28, 1))
Constructors:
# From existing array (shape inferred)
Tensor(data::Array{T}) where T
# With explicit shape
Tensor{Float32, (784,)}(data)
# Zeros
zeros(Tensor{Float32, (100, 784)})
# Random (normal distribution)
randn(Tensor{Float32, (batch, features)})
# Random (uniform)
rand(Tensor{Float32, (batch, features)})
# From range
Tensor(1:100)Properties:
size(t::Tensor) # Returns shape tuple
length(t::Tensor) # Total number of elements
eltype(t::Tensor) # Element type
ndims(t::Tensor) # Number of dimensionsOperations:
# Arithmetic
a + b, a - b, a * b, a / b
a .+ b, a .- b, a .* b, a ./ b # Broadcasting
# Indexing
t[1, :] # Slice
t[1:10, :] # Range
view(t, 1:10, :) # View (no copy)
# Reshaping
reshape(t, new_shape)
flatten(t)
squeeze(t)
unsqueeze(t, dim)
# Reduction
sum(t), sum(t, dims=1)
mean(t), mean(t, dims=1)
maximum(t), minimum(t)
argmax(t), argmin(t)Fully connected (linear) layer.
Dense(in_features => out_features;
activation=identity,
use_bias=true,
init=glorot_uniform)Parameters:
in_features::Int: Input dimensionout_features::Int: Output dimensionactivation: Activation function (default:identity)use_bias::Bool: Include bias term (default:true)init: Weight initialization function
Example:
layer = Dense(784 => 256, activation=relu)
y = forward(layer, x) # x: (batch, 784) → y: (batch, 256)Shapes:
- Input:
(batch, in_features) - Output:
(batch, out_features) - Weight:
(out_features, in_features) - Bias:
(out_features,)
2D Convolution layer.
Conv2D(in_channels => out_channels, kernel_size;
stride=1,
padding=0,
dilation=1,
groups=1,
activation=identity,
use_bias=true)Parameters:
in_channels::Int: Number of input channelsout_channels::Int: Number of output channelskernel_size::Tuple{Int,Int}: Kernel dimensions(H, W)stride: Stride (default:1or(1, 1))padding: Padding (default:0or(0, 0))dilation: Dilation (default:1)groups: Grouped convolution (default:1)activation: Activation function
Example:
conv = Conv2D(3 => 64, (3, 3), padding=1, activation=relu)
y = forward(conv, x) # x: (batch, H, W, 3) → y: (batch, H, W, 64)Shapes (NHWC format):
- Input:
(batch, height, width, in_channels) - Output:
(batch, out_height, out_width, out_channels) - Weight:
(kernel_h, kernel_w, in_channels, out_channels)
1D Convolution layer.
Conv1D(in_channels => out_channels, kernel_size;
stride=1,
padding=0,
activation=identity)Shapes:
- Input:
(batch, length, channels) - Output:
(batch, out_length, out_channels)
Pooling layers.
MaxPool2D(kernel_size; stride=kernel_size, padding=0)
AvgPool2D(kernel_size; stride=kernel_size, padding=0)Example:
pool = MaxPool2D((2, 2))
y = forward(pool, x) # Halves spatial dimensionsGlobal pooling (reduces to single value per channel).
gap = GlobalAvgPool2D()
y = forward(gap, x) # x: (batch, H, W, C) → y: (batch, C)Batch normalization.
BatchNorm(num_features;
eps=1e-5,
momentum=0.1,
affine=true,
track_running_stats=true)Example:
bn = BatchNorm(64)
y = forward(bn, x) # Normalizes over batch dimensionLayer normalization.
LayerNorm(normalized_shape;
eps=1e-5,
elementwise_affine=true)Example:
ln = LayerNorm(768) # For transformer hidden size
y = forward(ln, x)Dropout regularization.
Dropout(p=0.5)Note: Only active during training.
dropout = Dropout(0.3)
y = forward(dropout, x) # Training: applies dropout
y = forward(dropout, x, training=false) # Inference: identityFlatten spatial dimensions.
flatten = Flatten()
y = forward(flatten, x) # x: (batch, H, W, C) → y: (batch, H*W*C)Container for sequential layers.
model = Sequential(
Dense(784 => 256, activation=relu),
Dense(256 => 10, activation=softmax)
)
# Or using @axiom
model = @axiom begin
Dense(784 => 256, activation=relu)
Dense(256 => 10, activation=softmax)
endAll activation functions can be used as:
- Layer activation:
Dense(10 => 10, activation=relu) - Standalone:
y = relu(x) - In-place:
relu!(x)
| Function | Formula | Use Case |
|---|---|---|
relu |
max(0, x) |
Default choice |
relu6 |
min(max(0, x), 6) |
Mobile nets |
leaky_relu(x, α=0.01) |
x > 0 ? x : αx |
Dying ReLU fix |
elu(x, α=1.0) |
x > 0 ? x : α(exp(x)-1) |
Smooth at 0 |
selu |
Scaled ELU | Self-normalizing |
gelu |
Gaussian error | Transformers |
sigmoid |
1/(1+exp(-x)) |
Binary output |
tanh |
(exp(x)-exp(-x))/(exp(x)+exp(-x)) |
Bounded output |
softmax |
exp(x)/sum(exp(x)) |
Classification |
log_softmax |
log(softmax(x)) |
NLL loss |
swish / silu |
x * sigmoid(x) |
Modern nets |
mish |
x * tanh(softplus(x)) |
Alternative to swish |
softplus |
log(1 + exp(x)) |
Smooth ReLU |
hard_sigmoid |
Piecewise linear | Fast sigmoid |
hard_swish |
Piecewise | Fast swish |
Examples:
# As layer activation
Dense(10 => 10, activation=gelu)
# Standalone
y = gelu(x)
# With parameters
y = leaky_relu(x, 0.2)
# In-place
relu!(x)For multi-class classification.
loss = CrossEntropyLoss(;
reduction=:mean, # :mean, :sum, :none
weight=nothing, # Class weights
ignore_index=-1, # Index to ignore
label_smoothing=0.0
)
l = loss(predictions, targets)Example:
loss_fn = CrossEntropyLoss()
predictions = rand(Float32, 32, 10) # (batch, classes)
targets = rand(1:10, 32) # Class indices
l = loss_fn(predictions, targets)For binary classification.
loss = BinaryCrossEntropyLoss(;
reduction=:mean,
pos_weight=nothing
)Mean squared error.
loss = MSELoss(reduction=:mean)
l = loss(predictions, targets)Mean absolute error.
loss = L1Loss(reduction=:mean)Smooth L1 loss.
loss = HuberLoss(delta=1.0, reduction=:mean)Adaptive moment estimation.
optimizer = Adam(;
lr=0.001,
betas=(0.9, 0.999),
eps=1e-8,
weight_decay=0.0
)Usage:
opt = Adam(lr=0.001)
for epoch in 1:100
grads = gradient(model, x, y)
update!(opt, model, grads)
endStochastic gradient descent.
optimizer = SGD(;
lr=0.01,
momentum=0.0,
dampening=0.0,
weight_decay=0.0,
nesterov=false
)Adam with decoupled weight decay.
optimizer = AdamW(;
lr=0.001,
betas=(0.9, 0.999),
eps=1e-8,
weight_decay=0.01
)optimizer = RMSprop(;
lr=0.01,
alpha=0.99,
eps=1e-8,
weight_decay=0.0,
momentum=0.0
)Define a model with the Axiom DSL.
model = @axiom [options] begin
layer1
layer2
...
endOptions:
backend=ZigBackend("/path/to/libaxiom_zig.so"): Backend selectionverified=true: Enable verificationname="MyModel": Model name
Example:
model = @axiom backend=JuliaBackend() verified=true begin
@ensure size(x, 2) == 784 "Input must be 784-dim"
Dense(784 => 256, activation=relu)
Dropout(0.5)
Dense(256 => 10, activation=softmax)
@ensure size(output, 2) == 10 "Output must be 10 classes"
endRuntime assertion.
@ensure condition "error message"Examples:
@ensure size(x, 1) == batch_size "Batch size mismatch"
@ensure all(isfinite, x) "Input contains NaN/Inf"
@ensure 0 ≤ probability ≤ 1 "Invalid probability"Compile-time verification.
@prove Property args...Available Properties:
@prove BoundedOutputs(min, max) model
@prove Lipschitz(constant) model
@prove Monotonic(region) model
@prove Robust(epsilon) modelHigh-level training function.
train!(model, train_loader;
epochs=10,
optimizer=Adam(),
loss=CrossEntropyLoss(),
val_loader=nothing,
callbacks=[],
verbose=true
)Example:
train!(model, train_loader,
epochs=50,
optimizer=Adam(lr=0.001),
loss=CrossEntropyLoss(),
val_loader=val_loader,
callbacks=[
EarlyStopping(patience=5),
ModelCheckpoint("best.axiom")
]
)Compute gradients.
grads = gradient(model, x, y, loss_fn)Compute loss and gradients together.
loss, grads = value_and_gradient(model, x, y, loss_fn)Apply gradients to model.
update!(optimizer, model, grads)Pure Julia implementation.
backend = JuliaBackend()
model = @axiom backend=JuliaBackend() begin
Dense(10 => 10)
endHigh-performance Zig native implementation.
backend = ZigBackend()
# Check availability
zig_available() # Returns BoolNon-GPU accelerator targets with fallback-safe compilation strategy.
backend = detect_coprocessor() # TPU/NPU/DSP/FPGA or nothing
if backend !== nothing
model_accel = compile(model, backend=backend, verify=false, optimize=:none)
endBase type for verification properties.
abstract type VerificationProperty end
struct BoundedOutputs <: VerificationProperty
min_val::Float32
max_val::Float32
end
struct LipschitzProperty <: VerificationProperty
constant::Float32
endVerify a property on a model.
result = verify(property, model, input_bounds)Generate verification certificate.
cert = generate_certificate(model, properties)save_certificate(cert, "model.cert")
cert = load_certificate("model.cert")save_model(model, "model.axiom")
model = load_model("model.axiom")Export a supported model to ONNX format.
Current coverage includes Dense, Conv2d, BatchNorm, LayerNorm,
MaxPool2d, AvgPool2d, AdaptiveAvgPool2d (output (1,1)), GlobalAvgPool,
Flatten, and common activations in Sequential/Pipeline models.
to_onnx(model, "model.onnx", input_shape=(1, 3, 224, 224))Load a PyTorch model from:
- canonical JSON descriptor (
axiom.pytorch.sequential.v1) - direct
.pt/.pth/.ckptcheckpoints via the built-in Python bridge (python3+torch)
model = from_pytorch("model.pt")
model = from_pytorch("model.pytorch.json")Get all model parameters.
params = parameters(model) # Vector of arraysCount parameters.
n = num_parameters(model)set_training!(model, true) # Training mode
set_training!(model, false) # Inference mode
# Or functional
model_train = training_mode(model, true)Print model summary.
summary(model)
# Output:
# ┌─────────────────────────────────────────┐
# │ Axiom Model Summary │
# ├─────────────────────────────────────────┤
# │ Layer │ Output Shape │ Params │
# ├─────────────────────────────────────────┤
# │ Dense-1 │ (batch, 256) │ 200,960 │
# │ Dense-2 │ (batch, 10) │ 2,570 │
# ├─────────────────────────────────────────┤
# │ Total params: 203,530 │
# │ Trainable params: 203,530 │
# └─────────────────────────────────────────┘Batch data loading with shuffling.
loader = DataLoader(dataset;
batch_size=32,
shuffle=true,
drop_last=false
)
for (x, y) in loader
# x: (batch_size, ...), y: labels
endFor more examples, see Tutorials