Serez Packages

serez-ai

v1.0.7

Native AI library for Serez Code — neural networks, autodiff, tensors and random.

by serezdev · published 2026-05-28

serez-ai

Neural network library for Serez-Code, written in pure .sz on top of the core's Tensor + Autodiff. Keras-style API: build a Sequential model, fit it, predict — with dense, convolutional, recurrent and transformer layers.

import "serez-ai"

Random.seed(42)

let X = Tensor.from([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]])
let y = Tensor.from([[0.0], [1.0], [1.0], [0.0]])

let model = new Sequential()
model.add(new Dense(2, 4, "relu"))
model.add(new Dense(4, 1, "sigmoid"))

let history = model.fit(X, y, new MSE(), 3000, 1.0, true)   // XOR learned

let preds = model.predict(X)

Install

sz install serez-ai

Then import "serez-ai" just works.

What's inside

  • LayersDense, Conv2D, MaxPool2D, Flatten, Embedding, LSTM, GRU, MultiHeadAttention, LayerNorm, TransformerBlock.
  • LossesMSE, BCE (numerically stable).
  • OptimizersAdam, Momentum, SGD — plug any of them in with model.fit_opt(...), or drive a manual loop with Autodiff.tape() / Autodiff.backward() + opt.step_layer(layer).
  • Trainingmodel.fit(X, y, loss, epochs, lr, verbose) (plain SGD), fit_opt(..., optimizer, ...), fit_dl(X_arr, y_arr, ...) (shuffled mini-batches via DataLoader), and predict(X).
  • Transfer learningmodel.save("base.szai") / load(...) + freeze() / unfreeze() to reuse a trained base and train only a new head.

Gotcha (pass-by-value): in a manual loop, opt.step_layer(layer) returns the updated layer — always assign it back (model.layers[j] = opt.step_layer(model.layers[j])); mutations inside the call do not propagate to the caller.

Documentation

The full reference lives on the Serez-Code site:

Examples in the repo

FileDescription
apps/xor_demo.sz2-layer MLP, XOR problem, SGD
apps/mnist_demo.szCNN (Conv2D + MaxPool2D + Flatten + Dense), synthetic 8×8 images
apps/sentiment_demo.szEmbedding + LSTM/GRU, binary sentiment classification
apps/transformer_demo.szEmbedding + TransformerBlock + Dense, sequence classification
apps/transfer_demo.szSave/load + freeze + fine-tune a head on a new task

Requirements

Pure .sz, no external dependencies — it uses the core's native Autodiff, Tensor, Random, Math and File namespaces (File only for save/load).