axonml-nn
Overview
axonml-nn provides neural network building blocks for the AxonML framework. It includes layers, activation functions, loss functions, and utilities for constructing and training deep learning models with a PyTorch-like API.
Features
-
Module Trait - Core interface for all neural network components with parameter management and train/eval modes.
-
Comprehensive Layers - Linear, Conv1d/Conv2d, RNN/LSTM/GRU, Embedding, BatchNorm, LayerNorm, Dropout, and MultiHeadAttention.
-
Activation Functions - ReLU, Sigmoid, Tanh, GELU, SiLU, ELU, LeakyReLU, Softmax, and LogSoftmax.
-
Loss Functions - MSELoss, CrossEntropyLoss, BCELoss, BCEWithLogitsLoss, NLLLoss, L1Loss, and SmoothL1Loss.
-
Weight Initialization - Xavier/Glorot, Kaiming/He, orthogonal, sparse, and custom initialization schemes.
-
Sequential Container - Easy model composition by chaining layers together.
Modules
| Module | Description |
|---|---|
module |
Core Module trait and ModuleList container for neural network components |
parameter |
Parameter wrapper for learnable weights with gradient tracking |
sequential |
Sequential container for chaining modules in order |
layers |
Neural network layers (Linear, Conv, RNN, Attention, Norm, Pooling, Embedding, Dropout) |
activation |
Activation function modules (ReLU, Sigmoid, Tanh, GELU, etc.) |
loss |
Loss function modules (MSE, CrossEntropy, BCE, etc.) |
init |
Weight initialization functions (Xavier, Kaiming, orthogonal, etc.) |
functional |
Stateless functional versions of operations |
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Building a Simple MLP
use *;
use Variable;
use Tensor;
// Build model using Sequential
let model = new
.add
.add
.add
.add
.add;
// Create input
let input = new;
// Forward pass
let output = model.forward;
assert_eq!;
// Get all parameters
let params = model.parameters;
println!;
Convolutional Neural Network
use *;
use Variable;
use Tensor;
let model = new
.add // [B, 1, 28, 28] -> [B, 32, 26, 26]
.add
.add // -> [B, 32, 13, 13]
.add // -> [B, 64, 11, 11]
.add
.add; // -> [B, 64, 5, 5]
let input = new;
let features = model.forward;
Recurrent Neural Network
use *;
use Variable;
use Tensor;
// LSTM for sequence modeling
let lstm = LSTMnew;
// Input: [batch, seq_len, input_size]
let input = new;
let output = lstm.forward; // [2, 5, 128]
Transformer Attention
use *;
use Variable;
use Tensor;
let attention = new;
// Input: [batch, seq_len, embed_dim]
let input = new;
let output = attention.forward; // [2, 5, 512]
Loss Functions
use *;
use Variable;
use Tensor;
// Cross Entropy Loss for classification
let logits = new;
let targets = new;
let loss_fn = new;
let loss = loss_fn.compute;
loss.backward;
// MSE Loss for regression
let mse = new;
let pred = new;
let target = new;
let loss = mse.compute;
Weight Initialization
use *;
// Xavier/Glorot initialization
let weights = xavier_uniform;
let weights = xavier_normal;
// Kaiming/He initialization (for ReLU networks)
let weights = kaiming_uniform;
let weights = kaiming_normal;
// Other initializations
let zeros_tensor = zeros;
let ones_tensor = ones;
let eye_tensor = eye;
let ortho_tensor = orthogonal;
Training/Evaluation Mode
use *;
let mut model = new
.add
.add
.add;
// Training mode (dropout active)
model.train;
assert!;
// Evaluation mode (dropout disabled)
model.eval;
assert!;
// Zero gradients before backward pass
model.zero_grad;
Tests
Run the test suite:
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.