Package
serez-ai
Neural networks, autodiff, and training loops — with a Keras-like API. Build, train, and run models in a few lines of Serez Code.
Install
sz install serez-ai
Quick start
import "serez-ai"
Random.seed(42)
// XOR dataset
let X = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]
let y = [[0.0], [1.0], [1.0], [0.0]]
// Build the model
let model = new Sequential()
model.add(new Dense(2, 16, "relu"))
model.add(new Dense(16, 1, "sigmoid"))
// Train
let opt = new Adam(0.01, 0.9, 0.999)
let loss = model.fit_opt(X, y, new BCE(), 1000, opt)
out "Final loss: {loss}"
// Predict
let pred = model.forward([[1.0, 0.0]])
out "XOR(1, 0) ≈ {pred[0][0]}" // → ~0.97Sequential model
Sequential stacks layers one after the other. Add layers with .add(), then train with .fit_opt():
let model = new Sequential() model.add(new Dense(input_size, output_size, activation)) // fit_opt(X, y, loss_fn, epochs, optimizer) → final loss let loss = model.fit_opt(X, y, new MSE(), 500, new SGD(0.01)) // Forward pass — returns array of predictions let predictions = model.forward(X)
Layers
| Layer | Parameters | Use for |
|---|---|---|
Dense | in, out, activation | Fully connected layer. Activations: relu, sigmoid, tanh, linear |
Conv2D | filters, kernel_size, stride, padding | 2D convolution for image data |
LSTM | input_size, hidden_size | Sequences, time series, text |
Attention | d_model, n_heads | Transformer-style self-attention |
BatchNorm | features | Stabilizes training, normalizes activations |
Dropout | rate | Regularization — randomly zeros activations during training |
// Image classifier example let model = new Sequential() model.add(new Conv2D(32, 3, 1, 1)) model.add(new BatchNorm(32)) model.add(new Dense(32, 64, "relu")) model.add(new Dropout(0.2)) model.add(new Dense(64, 10, "sigmoid"))
Optimizers
new Adam(lr, beta1, beta2) // e.g. new Adam(0.001, 0.9, 0.999) new SGD(lr) // e.g. new SGD(0.01) new Momentum(lr, momentum) // e.g. new Momentum(0.01, 0.9)
Loss functions
new MSE() // Mean Squared Error — regression new BCE() // Binary Cross-Entropy — binary classification
Tensors
Create tensors directly and operate on them. Gradients are tracked automatically:
import "serez-ai" let a = new Tensor([[1.0, 2.0], [3.0, 4.0]]) let b = new Tensor([[2.0, 0.0], [1.0, 3.0]]) // Operations — all differentiable let c = Autodiff.add(a, b) let d = Autodiff.mul(a, b) let e = Autodiff.relu(c) // Compute gradients Autodiff.backward(e) out a.grad // gradient of the loss w.r.t. a out b.grad // gradient of the loss w.r.t. b
Autodiff operations
| Operation | Description |
|---|---|
Autodiff.add(a, b) | Element-wise addition |
Autodiff.mul(a, b) | Element-wise multiplication |
Autodiff.matmul(a, b) | Matrix multiplication |
Autodiff.relu(a) | ReLU activation |
Autodiff.sigmoid(a) | Sigmoid activation |
Autodiff.backward(tensor) | Backpropagate gradients from tensor |
Random
Reproducible random numbers for dataset shuffling and weight initialization:
Random.seed(42) // set seed for reproducibility let n = Random.float() // decimal in [0, 1) let i = Random.int(10) // int in [0, 10)