NeuraLib_Models
The official architecture expansion for compact temporal, recurrent, attention, Transformer, residual, and reinforcement-learning research inside TradingView.
The expansion library
Import NeuraLib first, then NeuraLib_Models. The expansion registers fluent methods directly on NeuraLib Sequential models.
import Alien_Algorithms/NeuraLib/1 as nl
import Alien_Algorithms/NeuraLib_Models/1 as models
var nl.Sequential model = nl.sequential("advanced_model")
if barstate.isfirst
model := model
.input(array.from(8), "sequence")
.temporalConvStack(4, 2, 3, 2, 2, 1, nl.ActivationKind.relu, 0.0, "temporal")
.globalAvgPool1d(2, 3, "pool")
.duelingQHead(4, 2, nl.ActivationKind.relu, "q_head")
.build(nl.rng(7))The result remains a normal NeuraLib model. .compile(), .trainOnBatch(), .predict(), .evaluate(), datasets, scalers, and weight utilities work unchanged.
Model families
| Family | Methods | Purpose |
|---|---|---|
| Residual | residualDense() | Deeper feedforward paths that preserve input information. |
| Temporal convolution | conv1d(), temporalConvStack() | Local structure in short flattened sequences. |
| Pooling | globalAvgPool1d(), globalMaxPool1d() | Compress a sequence into feature summaries. |
| Recurrent | lstm(), gru() | Ordered state memory across a compact sequence. |
| Attention | selfAttention(), multiHeadSelfAttention(), crossAttention() | Context mixing across token rows. |
| Transformers | transformerEncoder(), transformerEncoderStack(), transformerDecoder() | Attention plus feedforward residual architecture. |
| Reinforcement learning | duelingQHead(), prioritizedReplayBuffer() | Action values and deterministic prioritized replay. |
Temporal convolution and pooling
A sequence of four time steps with two features is represented as eight flattened inputs. Conv1D filters slide across time, while a temporal stack repeats compact convolution blocks. Global average pooling captures broad sequence behavior; max pooling emphasizes the strongest activation.
LSTM and GRU
.lstm() and .gru() scan flattened sequences in time order. Use them when ordering and memory matter more than a single snapshot. Keep time steps and unit counts small: recurrent backpropagation is expensive in Pine.
Attention and Transformers
selfAttention()mixes row context with one attention head.multiHeadSelfAttention()splits the model dimension across up to eight heads.crossAttention()uses packed query and memory rows.transformerEncoder()adds attention, residual paths, normalization, and feedforward transformation.transformerDecoder()combines causal target attention with memory attention.
modelDim must be divisible by headCount. The current implementation supports at most eight heads.Q-heads and prioritized replay
Dueling Q-head
.duelingQHead() separates overall state value from action advantage, then recombines them into action scores.
Prioritized Experience Replay
prioritizedReplayBuffer() stores feature/target transitions with priorities. sampleBatch(batchSize, alpha, beta, seed) returns a normal batch, logical indices, normalized importance weights, and sample count.
pushExperience()inserts or overwrites a row.updatePriority()updates a sampled row after observing error.- Sampling is deterministic for the same buffer, arguments, and seed.
Shape rules
- Conv1D, temporal stacks, LSTM, and GRU expect
timeSteps × featureCountflattened features. - Attention expects each row to contain exactly
modelDimcolumns. - Cross-attention and decoders require documented packed row layouts.
pushPositionalEncoding()adds sinusoidal position features to a FeatureBuilder.- Import the expansion after NeuraLib and avoid method-name collisions with core methods.