Training & Data

Training, Evaluation & Inference

Move from scaled batches to gradient updates, untouched validation measurements, live predictions, uncertainty estimates, and Pine-safe execution patterns.

Updated Publisher Alien_AlgorithmsPine Script v6

Train

One batch updatepine
model := model.trainOnBatch(train.inputTensor, train.targetTensor)
float lastLoss = model.trainStats.lastLoss

trainOnBatch() runs a forward pass, computes loss, backpropagates gradients, and applies an optimizer step when the active training gate permits it.

For manual control, .backward(target) accumulates gradients and .step() applies the optimizer separately.

Evaluate without learning

Validation losspine
nl.LossResult validationLoss = model.evaluate(validation.inputTensor, validation.targetTensor)

Evaluation performs no weight update. Compare validation behavior with training behavior to detect overfitting. nl.earlyStopper(patience, minDelta) can track whether improvement has stalled.

Predict in original units

Scaled inferencepine
nl.Tensor scaledInput = dataset.scaleInput(liveInput)
nl.Tensor scaledPrediction = model.predict(scaledInput)
nl.Tensor rawPrediction = dataset.inverseScaleTarget(scaledPrediction)
float value = rawPrediction.get1d(0)
  • Rebuild the exact training feature order.
  • Transform live input with the dataset’s fitted scaler.
  • Run .predict() without a target.
  • Inverse-transform output when target scaling was enabled.

Monte Carlo dropout uncertainty

.predictMC(inputTensor, samples) performs multiple dropout-enabled forward passes and reports mean and variance. Variance is a practical model-instability estimate, not a guaranteed probability calibration.

Weights and target models

  • .getWeightsArray() exports flat model weights.
  • .setWeightsArray(values) restores compatible weights.
  • .softUpdateFrom(source, tau) blends source weights into a target model for reinforcement-learning workflows.

Two execution modes

Live-edge training

.withTrainingGate(true) lets datasets collect through history while expensive backpropagation occurs only at the last confirmed historical bar and onward in real time.

Use for: rolling inputs, larger batches, recurrent blocks, attention, and deeper architectures.

Full-history training

.trainEveryCall() permits updates on every call across historical execution. This can produce an adaptive model path across the chart but multiplies runtime cost.

Use for: tiny dense models, 4–8 hidden units, batches around 8–16, and explicitly bounded chart history.

Pine executes on every bar
Set calc_bars_count deliberately. A model that is fast across 600 bars may time out across the symbol’s full history.
NeuraLib is a machine-learning research runtime for Pine Script. Model output is not financial advice and does not guarantee future performance. © Alien_Algorithms.