Building Models
Assemble readable neural architectures with NeuraLib’s fluent Sequential API, then build deterministic weights or compile the model for training.
Sequential API
Each chained method adds one block, so the code reads from input to output like an architecture diagram.
var nl.Sequential model = nl.sequential("my_model")
if barstate.isfirst
model := model
.input(array.from(4), "features")
.dense(8, nl.ActivationKind.relu, "hidden")
.dropout(0.10, "regularization")
.dense(1, nl.ActivationKind.linear, "output")
.compile(cfg)barstate.isfirst. Persistent model variables should use var so their learned state survives across bars.Core layers
| Method | Purpose |
|---|---|
.input(dims, name) | Defines the model input shape. It must be the first architecture call. |
.dense(units, activation, name) | Adds a fully connected trainable layer. |
.dropout(rate, name) | Randomly masks activations during training to reduce co-adaptation. |
.layerNorm(name) | Normalizes layer features to stabilize deeper paths. |
.activation(kind, alpha, name) | Adds a standalone activation transform. |
.qHead(actionCount, activation, name) | Adds a core action-value output head. |
.flatten(name) / .reshape(dims, name) | Changes shape metadata between compatible blocks. |
.block(graphBlock) | Inserts a custom or advanced GraphBlock. |
Build vs. compile
Compile for training
.compile(cfg) builds missing weights and attaches loss, optimizer, metric, schedule, clipping, and execution-gate behavior. Use it when the model will train.
Build for deterministic inference
.build(nl.rng(seed)) initializes weights with a reproducible random stream without requiring a training configuration. It is useful for inference-only demonstrations, graph probes, and architecture testing.
A model can be built and then compiled when you need explicit control over initialization followed by training configuration.
Initialization and persistence
- Use stable, unique names for models and blocks when debugging graph structure.
- Use
nl.rng(seed)for reproducible initialization. - Changing architecture or hyperparameters causes Pine to rebuild script state.
- Export weights with
.getWeightsArray()and restore them with.setWeightsArray().
Shape discipline
Most architecture errors are shape errors. Keep a written contract for input features, ordering, dimensions, output targets, and batch layout. NeuraLib validates calls before expensive graph work, but a mathematically valid shape can still represent the wrong semantic ordering.