architecture

architecture/activationArrayPool.ts

ActivationArray

Allowed activation array shapes for pooling.

activationArrayPool

ActivationArrayPool

A size-bucketed pool of activation arrays.

Buckets map array length -> stack of arrays. Acquire pops and zero-fills, or allocates a new array when empty. Release pushes back up to a configurable per-bucket cap to avoid unbounded growth.

Note: not thread-safe; intended for typical single-threaded JS execution.

architecture/architect.ts

architect

Provides static methods for constructing various predefined neural network architectures.

The Architect class simplifies the creation of common network types like Multi-Layer Perceptrons (MLPs), Long Short-Term Memory (LSTM) networks, Gated Recurrent Units (GRUs), and more complex structures inspired by neuro-evolutionary algorithms. It leverages the underlying Layer, Group, and Node components to build interconnected Network objects.

Methods often utilize helper functions from Layer (e.g., Layer.dense, Layer.lstm) and connection strategies from methods.groupConnection.

Architect

Provides static methods for constructing various predefined neural network architectures.

The Architect class simplifies the creation of common network types like Multi-Layer Perceptrons (MLPs), Long Short-Term Memory (LSTM) networks, Gated Recurrent Units (GRUs), and more complex structures inspired by neuro-evolutionary algorithms. It leverages the underlying Layer, Group, and Node components to build interconnected Network objects.

Methods often utilize helper functions from Layer (e.g., Layer.dense, Layer.lstm) and connection strategies from methods.groupConnection.

default

construct

(list: (import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/group").default)[]) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Constructs a Network instance from an array of interconnected Layers, Groups, or Nodes.

This method processes the input list, extracts all unique nodes, identifies connections, gates, and self-connections, and determines the network's input and output sizes based on the type property ('input' or 'output') set on the nodes. It uses Sets internally for efficient handling of unique elements during construction.

Parameters:

Returns: A Network object representing the constructed architecture.

enforceMinimumHiddenLayerSizes

(network: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Enforces the minimum hidden layer size rule on a network.

This ensures that all hidden layers have at least min(input, output) + 1 nodes, which is a common heuristic to ensure networks have adequate representation capacity.

Parameters:

Returns: The same network with properly sized hidden layers

gru

(layers: number[]) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Creates a Gated Recurrent Unit (GRU) network. GRUs are another type of recurrent neural network, similar to LSTMs but often simpler. This constructor uses Layer.gru to create the core GRU blocks.

Parameters:

Returns: The constructed GRU network.

hopfield

(size: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Creates a Hopfield network. Hopfield networks are a form of recurrent neural network often used for associative memory tasks. This implementation creates a simple, fully connected structure.

Parameters:

Returns: The constructed Hopfield network.

lstm

(layerArgs: (number | { inputToOutput?: boolean | undefined; })[]) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Creates a Long Short-Term Memory (LSTM) network. LSTMs are a type of recurrent neural network (RNN) capable of learning long-range dependencies. This constructor uses Layer.lstm to create the core LSTM blocks.

Parameters:

Returns: The constructed LSTM network.

narx

(inputSize: number, hiddenLayers: number | number[], outputSize: number, previousInput: number, previousOutput: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Creates a Nonlinear AutoRegressive network with eXogenous inputs (NARX). NARX networks are recurrent networks often used for time series prediction. They predict the next value of a time series based on previous values of the series and previous values of external (exogenous) input series.

Parameters:

Returns: The constructed NARX network.

perceptron

(layers: number[]) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Creates a standard Multi-Layer Perceptron (MLP) network. An MLP consists of an input layer, one or more hidden layers, and an output layer, fully connected layer by layer.

Parameters:

Returns: The constructed MLP network.

random

(input: number, hidden: number, output: number, options: { connections?: number | undefined; backconnections?: number | undefined; selfconnections?: number | undefined; gates?: number | undefined; }) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Creates a randomly structured network based on specified node counts and connection options.

This method allows for the generation of networks with a less rigid structure than MLPs. It initializes a network with input and output nodes and then iteratively adds hidden nodes and various types of connections (forward, backward, self) and gates using mutation methods. This approach is inspired by neuro-evolution techniques where network topology evolves.

Parameters:

Returns: The constructed network with a randomized topology.

architecture/connection.ts

connection

default

_flags

Packed state flags (private for future-proofing hidden class): bit0 => enabled gene expression (1 = active) bit1 => DropConnect active mask (1 = not dropped this forward pass) bit2 => hasGater (1 = symbol field present) bit3 => plastic (plasticityRate > 0) bits4+ reserved.

acquire

(from: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default, to: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default, weight: number | undefined) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default

Acquire a Connection from the pool (or construct new). Fields are fully reset & given a fresh sequential innovation id. Prefer this in evolutionary algorithms that mutate topology frequently to reduce GC pressure.

Parameters:

Returns: Reinitialized connection instance.

dcMask

DropConnect active mask: 1 = not dropped (active), 0 = dropped for this stochastic pass.

dropConnectActiveMask

Convenience alias for DropConnect mask with clearer naming.

eligibility

Standard eligibility trace (e.g., for RTRL / policy gradient credit assignment).

enabled

Whether the gene (connection) is currently expressed (participates in forward pass).

firstMoment

First moment estimate (Adam / AdamW) (was opt_m).

from

The source (pre-synaptic) node supplying activation.

gain

Multiplicative modulation applied after weight. Default is 1 (neutral). We only store an internal symbol-keyed property when the gain is non-neutral, reducing memory usage across large populations where most connections are ungated.

gater

Optional gating node whose activation can modulate effective weight (symbol-backed).

gradientAccumulator

Generic gradient accumulator (RMSProp / AdaGrad) (was opt_cache).

hasGater

Whether a gater node is assigned (modulates gain); true if the gater symbol field is present.

infinityNorm

Adamax: Exponential moving infinity norm (was opt_u).

innovation

Unique historical marking (auto-increment) for evolutionary alignment.

innovationID

(sourceNodeId: number, targetNodeId: number) => number

Deterministic Cantor pairing function for a (sourceNodeId, targetNodeId) pair. Useful when you want a stable innovation id without relying on global mutable counters (e.g., for hashing or reproducible experiments).

NOTE: For large indices this can overflow 53-bit safe integer space; keep node indices reasonable.

Parameters:

Returns: Unique non-negative integer derived from the ordered pair.

lookaheadShadowWeight

Lookahead: shadow (slow) weight parameter (was _la_shadowWeight).

maxSecondMoment

AMSGrad: Maximum of past second moment (was opt_vhat).

plastic

Whether this connection participates in plastic adaptation (rate > 0).

plasticityRate

Per-connection plasticity / learning rate (0 means non-plastic). Setting >0 marks plastic flag.

previousDeltaWeight

Last applied delta weight (used by classic momentum).

release

(conn: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default) => void

Return a Connection to the internal pool for later reuse. Do NOT use the instance again afterward unless re-acquired (treat as surrendered). Optimizer / trace fields are not scrubbed here (they're overwritten during acquire).

Parameters:

resetInnovationCounter

(value: number) => void

Reset the monotonic auto-increment innovation counter (used for newly constructed / pooled instances). You normally only call this at the start of an experiment or when deserializing a full population.

Parameters:

secondMoment

Second raw moment estimate (Adam family) (was opt_v).

secondMomentum

Secondary momentum (Lion variant) (was opt_m2).

to

The target (post-synaptic) node receiving activation.

toJSON

() => any

Serialize to a minimal JSON-friendly shape (used for saving genomes / networks). Undefined indices are preserved as undefined to allow later resolution / remapping.

Returns: Object with node indices, weight, gain, gater index (if any), innovation id & enabled flag.

totalDeltaWeight

Accumulated (batched) delta weight awaiting an apply step.

weight

Scalar multiplier applied to the source activation (prior to gain modulation).

xtrace

Extended trace structure for modulatory / eligibility propagation algorithms. Parallel arrays for cache-friendly iteration.

architecture/group.ts

group

Represents a collection of nodes functioning as a single unit within a network architecture. Groups facilitate operations like collective activation, propagation, and connection management.

Group

Represents a collection of nodes functioning as a single unit within a network architecture. Groups facilitate operations like collective activation, propagation, and connection management.

default

activate

(value: number[] | undefined) => number[]

Activates all nodes in the group. If input values are provided, they are assigned sequentially to the nodes before activation. Otherwise, nodes activate based on their existing states and incoming connections.

Parameters:

Returns: An array containing the activation value of each node in the group, in order.

clear

() => void

Resets the state of all nodes in the group. This typically involves clearing activation values, state, and propagated errors, preparing the group for a new input pattern, especially relevant in recurrent networks or sequence processing.

connect

(target: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/group").default, method: any, weight: number | undefined) => any[]

Establishes connections from all nodes in this group to a target Group, Layer, or Node. The connection pattern (e.g., all-to-all, one-to-one) can be specified.

Parameters:

Returns: An array containing all the connection objects created. Consider using a more specific type like Connection[].

connections

Stores connection information related to this group. in: Connections coming into any node in this group from outside. out: Connections going out from any node in this group to outside. self: Connections between nodes within this same group (e.g., in ONE_TO_ONE connections).

disconnect

(target: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/group").default, twosided: boolean) => void

Removes connections between nodes in this group and a target Group or Node.

Parameters:

gate

(connections: any, method: any) => void

Configures nodes within this group to act as gates for the specified connection(s). Gating allows the output of a node in this group to modulate the flow of signal through the gated connection.

Parameters:

nodes

An array holding all the nodes within this group.

propagate

(rate: number, momentum: number, target: number[] | undefined) => void

Propagates the error backward through all nodes in the group. If target values are provided, the error is calculated against these targets (typically for output layers). Otherwise, the error is calculated based on the error propagated from subsequent layers/nodes.

Parameters:

set

(values: { bias?: number | undefined; squash?: any; type?: string | undefined; }) => void

Sets specific properties (like bias, squash function, or type) for all nodes within the group.

Parameters:

toJSON

() => { size: number; nodeIndices: (number | undefined)[]; connections: { in: number; out: number; self: number; }; }

Serializes the group into a JSON-compatible format, avoiding circular references. Only includes node indices and connection counts.

Returns: A JSON-compatible representation of the group.

architecture/layer.ts

layer

Represents a functional layer within a neural network architecture.

Layers act as organizational units for nodes, facilitating the creation of complex network structures like Dense, LSTM, GRU, or Memory layers. They manage the collective behavior of their nodes, including activation, propagation, and connection to other network components.

Layer

Represents a functional layer within a neural network architecture.

Layers act as organizational units for nodes, facilitating the creation of complex network structures like Dense, LSTM, GRU, or Memory layers. They manage the collective behavior of their nodes, including activation, propagation, and connection to other network components.

default

activate

(value: number[] | undefined, training: boolean) => number[]

Activates all nodes within the layer, computing their output values.

If an input value array is provided, it's used as the initial activation for the corresponding nodes in the layer. Otherwise, nodes compute their activation based on their incoming connections.

During training, layer-level dropout is applied, masking all nodes in the layer together. During inference, all masks are set to 1.

Parameters:

Returns: An array containing the activation value of each node in the layer after activation.

attention

(size: number, heads: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default

Creates a multi-head self-attention layer (stub implementation).

Parameters:

Returns: A new Layer instance representing an attention layer.

batchNorm

(size: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default

Creates a batch normalization layer. Applies batch normalization to the activations of the nodes in this layer during activation.

Parameters:

Returns: A new Layer instance configured as a batch normalization layer.

clear

() => void

Resets the activation state of all nodes within the layer. This is typically done before processing a new input sequence or sample.

connect

(target: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/group").default, method: any, weight: number | undefined) => any[]

Connects this layer's output to a target component (Layer, Group, or Node).

This method delegates the connection logic primarily to the layer's output group or the target layer's input method. It establishes the forward connections necessary for signal propagation.

Parameters:

Returns: An array containing the newly created connection objects.

connections

Stores connection information related to this layer. This is often managed by the network or higher-level structures rather than directly by the layer itself. in: Incoming connections to the layer's nodes. out: Outgoing connections from the layer's nodes. self: Self-connections within the layer's nodes.

conv1d

(size: number, kernelSize: number, stride: number, padding: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default

Creates a 1D convolutional layer (stub implementation).

Parameters:

Returns: A new Layer instance representing a 1D convolutional layer.

dense

(size: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default

Creates a standard fully connected (dense) layer.

All nodes in the source layer/group will connect to all nodes in this layer when using the default ALL_TO_ALL connection method via layer.input().

Parameters:

Returns: A new Layer instance configured as a dense layer.

disconnect

(target: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/group").default, twosided: boolean | undefined) => void

Removes connections between this layer's nodes and a target Group or Node.

Parameters:

dropout

Dropout rate for this layer (0 to 1). If > 0, all nodes in the layer are masked together during training. Layer-level dropout takes precedence over node-level dropout for nodes in this layer.

gate

(connections: any[], method: any) => void

Applies gating to a set of connections originating from this layer's output group.

Gating allows the activity of nodes in this layer (specifically, the output group) to modulate the flow of information through the specified connections.

Parameters:

gru

(size: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default

Creates a Gated Recurrent Unit (GRU) layer.

GRUs are another type of recurrent neural network cell, often considered simpler than LSTMs but achieving similar performance on many tasks. They use an update gate and a reset gate to manage information flow.

Parameters:

Returns: A new Layer instance configured as a GRU layer.

input

(from: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/group").default, method: any, weight: number | undefined) => any[]

Handles the connection logic when this layer is the target of a connection.

It connects the output of the from layer or group to this layer's primary input mechanism (which is often the output group itself, but depends on the layer type). This method is usually called by the connect method of the source layer/group.

Parameters:

Returns: An array containing the newly created connection objects.

isGroup

(obj: any) => boolean

Type guard to check if an object is likely a Group.

This is a duck-typing check based on the presence of expected properties (set method and nodes array). Used internally where layer.nodes might contain Group instances (e.g., in Memory layers).

Parameters:

Returns: true if the object has set and nodes properties matching a Group, false otherwise.

layerNorm

(size: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default

Creates a layer normalization layer. Applies layer normalization to the activations of the nodes in this layer during activation.

Parameters:

Returns: A new Layer instance configured as a layer normalization layer.

lstm

(size: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default

Creates a Long Short-Term Memory (LSTM) layer.

LSTMs are a type of recurrent neural network (RNN) cell capable of learning long-range dependencies. This implementation uses standard LSTM architecture with input, forget, and output gates, and a memory cell.

Parameters:

Returns: A new Layer instance configured as an LSTM layer.

memory

(size: number, memory: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/layer").default

Creates a Memory layer, designed to hold state over a fixed number of time steps.

This layer consists of multiple groups (memory blocks), each holding the state from a previous time step. The input connects to the most recent block, and information propagates backward through the blocks. The layer's output concatenates the states of all memory blocks.

Parameters:

Returns: A new Layer instance configured as a Memory layer.

nodes

An array containing all the nodes (neurons or groups) that constitute this layer. The order of nodes might be relevant depending on the layer type and its connections.

output

Represents the primary output group of nodes for this layer. This group is typically used when connecting this layer to another layer or group. It might be null if the layer is not yet fully constructed or is an input layer.

propagate

(rate: number, momentum: number, target: number[] | undefined) => void

Propagates the error backward through all nodes in the layer.

This is a core step in the backpropagation algorithm used for training. If a target array is provided (typically for the output layer), it's used to calculate the initial error for each node. Otherwise, nodes calculate their error based on the error propagated from subsequent layers.

Parameters:

set

(values: { bias?: number | undefined; squash?: any; type?: string | undefined; }) => void

Configures properties for all nodes within the layer.

Allows batch setting of common node properties like bias, activation function (squash), or node type. If a node within the nodes array is actually a Group (e.g., in memory layers), the configuration is applied recursively to the nodes within that group.

Parameters:

architecture/network.ts

network

Network (Evolvable / Trainable Graph)

Represents a directed neural computation graph used both as a NEAT genome phenotype and (optionally) as a gradient‑trainable model. The class binds together specialized modules (topology, pruning, serialization, slab packing) to keep the core surface approachable for learners.

Educational Highlights:

Typical Usage:

const net = new Network(4, 2);           // create network
const out = net.activate([0.1,0.3,0.2,0.9]);
net.addNodeBetween();                    // structural mutation
const slab = (net as any).getConnectionSlab(); // inspect packed arrays
const clone = net.clone();               // deep copy

Performance Guidance:

Serialization:

Network

Network (Evolvable / Trainable Graph)

Represents a directed neural computation graph used both as a NEAT genome phenotype and (optionally) as a gradient‑trainable model. The class binds together specialized modules (topology, pruning, serialization, slab packing) to keep the core surface approachable for learners.

Educational Highlights:

Typical Usage:

const net = new Network(4, 2);           // create network
const out = net.activate([0.1,0.3,0.2,0.9]);
net.addNodeBetween();                    // structural mutation
const slab = (net as any).getConnectionSlab(); // inspect packed arrays
const clone = net.clone();               // deep copy

Performance Guidance:

Serialization:

default

_applyGradientClipping

(cfg: { mode: "norm" | "percentile" | "layerwiseNorm" | "layerwisePercentile"; maxNorm?: number | undefined; percentile?: number | undefined; }) => void

Trains the network on a given dataset subset for one pass (epoch or batch). Performs activation and backpropagation for each item in the set. Updates weights based on batch size configuration.

Parameters:

Returns: The average error calculated over the provided dataset subset.

activate

(input: number[], training: boolean, maxActivationDepth: number) => number[]

Activates the network using the given input array. Performs a forward pass through the network, calculating the activation of each node.

Parameters:

Returns: An array of numerical values representing the activations of the network's output nodes.

activateBatch

(inputs: number[][], training: boolean) => number[][]

Activate the network over a batch of input vectors (micro-batching).

Currently iterates sample-by-sample while reusing the network's internal fast-path allocations. Outputs are cloned number[] arrays for API compatibility. Future optimizations can vectorize this path.

Parameters:

Returns: Array of output vectors, each length equals this.output

activateRaw

(input: number[], training: boolean, maxActivationDepth: number) => any

Raw activation that can return a typed array when pooling is enabled (zero-copy). If reuseActivationArrays=false falls back to standard activate().

adjustRateForAccumulation

(rate: number, accumulationSteps: number, reduction: "average" | "sum") => number

Utility: adjust rate for accumulation mode (use result when switching to 'sum' to mimic 'average').

clear

() => void

Clears the internal state of all nodes in the network. Resets node activation, state, eligibility traces, and extended traces to their initial values (usually 0). This is typically done before processing a new input sequence in recurrent networks or between training epochs if desired.

clone

() => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Creates a deep copy of the network.

Returns: A new Network instance that is a clone of the current network.

connect

(from: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default, to: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default, weight: number | undefined) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default[]

Creates a connection between two nodes in the network. Handles both regular connections and self-connections. Adds the new connection object(s) to the appropriate network list (connections or selfconns).

Parameters:

Returns: An array containing the newly created connection object(s). Typically contains one connection, but might be empty or contain more in specialized node types.

createMLP

(inputCount: number, hiddenCounts: number[], outputCount: number) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Creates a fully connected, strictly layered MLP network.

Parameters:

Returns: A new, fully connected, layered MLP

crossOver

(network1: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default, network2: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default, equal: boolean) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Creates a new offspring network by performing crossover between two parent networks. This method implements the crossover mechanism inspired by the NEAT algorithm and described in the Instinct paper, combining genes (nodes and connections) from both parents. Fitness scores can influence the inheritance process. Matching genes are inherited randomly, while disjoint/excess genes are typically inherited from the fitter parent (or randomly if fitness is equal or equal flag is set).

Parameters:

Returns: A new Network instance representing the offspring.

deserialize

(data: any[], inputSize: number | undefined, outputSize: number | undefined) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Creates a Network instance from serialized data produced by serialize(). Reconstructs the network structure and state based on the provided arrays.

Parameters:

Returns: A new Network instance reconstructed from the serialized data.

disconnect

(from: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default, to: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default) => void

Disconnects two nodes, removing the connection between them. Handles both regular connections and self-connections. If the connection being removed was gated, it is also ungated.

Parameters:

enableWeightNoise

(stdDev: number | { perHiddenLayer: number[]; }) => void

Enable weight noise. Provide a single std dev number or { perHiddenLayer: number[] }.

fastSlabActivate

(input: number[]) => number[]

Public wrapper for fast slab forward pass (primarily for tests / benchmarking). Prefer using standard activate(); it will auto dispatch when eligible. Falls back internally if prerequisites not met.

fromJSON

(json: any) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Reconstructs a network from a JSON object (latest standard). Handles formatVersion, robust error handling, and index-based references.

Parameters:

Returns: The reconstructed network.

gate

(node: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default, connection: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default) => void

Gates a connection with a specified node. The activation of the node (gater) will modulate the weight of the connection. Adds the connection to the network's gates list.

Parameters:

getLastGradClipGroupCount

() => number

Returns last gradient clipping group count (0 if no clipping yet).

getLossScale

() => number

Returns current mixed precision loss scale (1 if disabled).

getRawGradientNorm

() => number

Returns last recorded raw (pre-update) gradient L2 norm.

getTrainingStats

() => { gradNorm: number; gradNormRaw: number; lossScale: number; optimizerStep: number; mp: { good: number; bad: number; overflowCount: number; scaleUps: number; scaleDowns: number; lastOverflowStep: number; }; }

Consolidated training stats snapshot.

mutate

(method: any) => void

Mutates the network's structure or parameters according to the specified method. This is a core operation for neuro-evolutionary algorithms (like NEAT). The method argument should be one of the mutation types defined in methods.mutation.

Parameters:

noTraceActivate

(input: number[]) => number[]

Activates the network without calculating eligibility traces. This is a performance optimization for scenarios where backpropagation is not needed, such as during testing, evaluation, or deployment (inference).

Parameters:

Returns: An array of numerical values representing the activations of the network's output nodes.

propagate

(rate: number, momentum: number, update: boolean, target: number[], regularization: number, costDerivative: ((target: number, output: number) => number) | undefined) => void

Propagates the error backward through the network (backpropagation). Calculates the error gradient for each node and connection. If update is true, it adjusts the weights and biases based on the calculated gradients, learning rate, momentum, and optional L2 regularization.

The process starts from the output nodes and moves backward layer by layer (or topologically for recurrent nets).

Parameters:

pruneToSparsity

(targetSparsity: number, method: "magnitude" | "snip") => void

Immediately prune connections to reach (or approach) a target sparsity fraction. Used by evolutionary pruning (generation-based) independent of training iteration schedule.

Parameters:

rebuildConnections

(net: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default) => void

Rebuilds the network's connections array from all per-node connections. This ensures that the network.connections array is consistent with the actual outgoing connections of all nodes. Useful after manual wiring or node manipulation.

Parameters:

Returns: Example usage: Network.rebuildConnections(net);

remove

(node: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default) => void

Removes a node from the network. This involves:

  1. Disconnecting all incoming and outgoing connections associated with the node.
  2. Removing any self-connections.
  3. Removing the node from the nodes array.
  4. Attempting to reconnect the node's direct predecessors to its direct successors to maintain network flow, if possible and configured.
  5. Handling gates involving the removed node (ungating connections gated by this node, and potentially re-gating connections that were gated by other nodes onto the removed node's connections).

Parameters:

resetDropoutMasks

() => void

Resets all masks in the network to 1 (no dropout). Applies to both node-level and layer-level dropout. Should be called after training to ensure inference is unaffected by previous dropout.

serialize

() => any[]

Lightweight tuple serializer delegating to network.serialize.ts

set

(values: { bias?: number | undefined; squash?: any; }) => void

Sets specified properties (e.g., bias, squash function) for all nodes in the network. Useful for initializing or resetting node properties uniformly.

Parameters:

setStochasticDepth

(survival: number[]) => void

Configure stochastic depth with survival probabilities per hidden layer (length must match hidden layer count when using layered network).

test

(set: { input: number[]; output: number[]; }[], cost: any) => { error: number; time: number; }

Tests the network's performance on a given dataset. Calculates the average error over the dataset using a specified cost function. Uses noTraceActivate for efficiency as gradients are not needed. Handles dropout scaling if dropout was used during training.

Parameters:

Returns: An object containing the calculated average error over the dataset and the time taken for the test in milliseconds.

toJSON

() => object

Converts the network into a JSON object representation (latest standard). Includes formatVersion, and only serializes properties needed for full reconstruction. All references are by index. Excludes runtime-only properties (activation, state, traces).

Returns: A JSON-compatible object representing the network.

toONNX

() => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network/network.onnx").OnnxModel

Exports the network to ONNX format (JSON object, minimal MLP support). Only standard feedforward architectures and standard activations are supported. Gating, custom activations, and evolutionary features are ignored or replaced with Identity.

Returns: ONNX model as a JSON object.

ungate

(connection: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default) => void

Removes the gate from a specified connection. The connection will no longer be modulated by its gater node. Removes the connection from the network's gates list.

Parameters:

architecture/node.ts

node

Node (Neuron)

Fundamental computational unit: aggregates weighted inputs, applies an activation function (squash) and emits an activation value. Supports:

Educational note: Traces (eligibility and xtrace) illustrate how recurrent credit assignment works in algorithms like RTRL / policy gradients. They are updated only when using the traced activation path (activate) vs noTraceActivate (inference fast path).

Node

Node (Neuron)

Fundamental computational unit: aggregates weighted inputs, applies an activation function (squash) and emits an activation value. Supports:

Educational note: Traces (eligibility and xtrace) illustrate how recurrent credit assignment works in algorithms like RTRL / policy gradients. They are updated only when using the traced activation path (activate) vs noTraceActivate (inference fast path).

default

_activateCore

(withTrace: boolean, input: number | undefined) => number

Internal shared implementation for activate/noTraceActivate.

Parameters:

_globalNodeIndex

Global index counter for assigning unique indices to nodes.

_safeUpdateWeight

(connection: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default, delta: number) => void

Internal helper to safely update a connection weight with clipping and NaN checks.

activate

(input: number | undefined) => number

Activates the node, calculating its output value based on inputs and state. This method also calculates eligibility traces (xtrace) used for training recurrent connections.

The activation process involves:

  1. Calculating the node's internal state (this.state) based on:
    • Incoming connections' weighted activations.
    • The recurrent self-connection's weighted state from the previous timestep (this.old).
    • The node's bias.
  2. Applying the activation function (this.squash) to the state to get the activation (this.activation).
  3. Applying the dropout mask (this.mask).
  4. Calculating the derivative of the activation function.
  5. Updating the gain of connections gated by this node.
  6. Calculating and updating eligibility traces for incoming connections.

Parameters:

Returns: The calculated activation value of the node.

activation

The output value of the node after applying the activation function. This is the value transmitted to connected nodes.

applyBatchUpdates

(momentum: number) => void

Applies accumulated batch updates to incoming and self connections and this node's bias. Uses momentum in a Nesterov-compatible way: currentDelta = accumulated + momentum * previousDelta. Resets accumulators after applying. Safe to call on any node type.

Parameters:

applyBatchUpdatesWithOptimizer

(opts: { type: "sgd" | "rmsprop" | "adagrad" | "adam" | "adamw" | "amsgrad" | "adamax" | "nadam" | "radam" | "lion" | "adabelief" | "lookahead"; momentum?: number | undefined; beta1?: number | undefined; beta2?: number | undefined; eps?: number | undefined; weightDecay?: number | undefined; lrScale?: number | undefined; t?: number | undefined; baseType?: any; la_k?: number | undefined; la_alpha?: number | undefined; }) => void

Extended batch update supporting multiple optimizers.

Applies accumulated (batch) gradients stored in totalDeltaWeight / totalDeltaBias to the underlying weights and bias using the selected optimization algorithm. Supports both classic SGD (with Nesterov-style momentum via preceding propagate logic) and a collection of adaptive optimizers. After applying an update, gradient accumulators are reset to 0.

Supported optimizers (type):

Options:

Internal per-connection temp fields (created lazily):

Safety: We clip extreme weight / bias magnitudes and guard against NaN/Infinity.

Parameters:

bias

The bias value of the node. Added to the weighted sum of inputs before activation. Input nodes typically have a bias of 0.

clear

() => void

Clears the node's dynamic state information. Resets activation, state, previous state, error signals, and eligibility traces. Useful for starting a new activation sequence (e.g., for a new input pattern).

connect

(target: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default | { nodes: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default[]; }, weight: number | undefined) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default[]

Creates a connection from this node to a target node or all nodes in a group.

Parameters:

Returns: An array containing the newly created Connection object(s).

connections

Stores incoming, outgoing, gated, and self-connections for this node.

derivative

The derivative of the activation function evaluated at the node's current state. Used in backpropagation.

disconnect

(target: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default, twosided: boolean) => void

Removes the connection from this node to the target node.

Parameters:

error

Stores error values calculated during backpropagation.

fromJSON

(json: { bias: number; type: string; squash: string; mask: number; }) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default

Creates a Node instance from a JSON object.

Parameters:

Returns: A new Node instance configured according to the JSON object.

gate

(connections: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default[]) => void

Makes this node gate the provided connection(s). The connection's gain will be controlled by this node's activation value.

Parameters:

geneId

Stable per-node gene identifier for NEAT innovation reuse

index

Optional index, potentially used to identify the node's position within a layer or network structure. Not used internally by the Node class itself.

isActivating

Internal flag to detect cycles during activation

isConnectedTo

(target: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default) => boolean

Checks if this node is connected to another node.

Parameters:

Returns: True if connected, otherwise false.

isProjectedBy

(node: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default) => boolean

Checks if the given node has a direct outgoing connection to this node. Considers both regular incoming connections and the self-connection.

Parameters:

Returns: True if the given node projects to this node, false otherwise.

isProjectingTo

(node: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default) => boolean

Checks if this node has a direct outgoing connection to the given node. Considers both regular outgoing connections and the self-connection.

Parameters:

Returns: True if this node projects to the target node, false otherwise.

mask

A mask factor (typically 0 or 1) used for implementing dropout. If 0, the node's output is effectively silenced.

mutate

(method: any) => void

Applies a mutation method to the node. Used in neuro-evolution.

This allows modifying the node's properties, such as its activation function or bias, based on predefined mutation methods.

Parameters:

noTraceActivate

(input: number | undefined) => number

Activates the node without calculating eligibility traces (xtrace). This is a performance optimization used during inference (when the network is just making predictions, not learning) as trace calculations are only needed for training.

Parameters:

Returns: The calculated activation value of the node.

old

The node's state from the previous activation cycle. Used for recurrent self-connections.

previousDeltaBias

The change in bias applied in the previous training iteration. Used for calculating momentum.

propagate

(rate: number, momentum: number, update: boolean, regularization: number | { type: "L1" | "L2"; lambda: number; } | ((weight: number) => number), target: number | undefined) => void

Back-propagates the error signal through the node and calculates weight/bias updates.

This method implements the backpropagation algorithm, including:

  1. Calculating the node's error responsibility based on errors from subsequent nodes (projected error) and errors from connections it gates (gated error).
  2. Calculating the gradient for each incoming connection's weight using eligibility traces (xtrace).
  3. Calculating the change (delta) for weights and bias, incorporating:
    • Learning rate.
    • L1/L2/custom regularization.
    • Momentum (using Nesterov Accelerated Gradient - NAG).
  4. Optionally applying the calculated updates immediately or accumulating them for batch training.

Parameters:

setActivation

(fn: (x: number, derivate?: boolean | undefined) => number) => void

Sets a custom activation function for this node at runtime.

Parameters:

squash

(x: number, derivate: boolean | undefined) => number

The activation function (squashing function) applied to the node's state. Maps the internal state to the node's output (activation).

Parameters:

Returns: The activation value or its derivative.

state

The internal state of the node (sum of weighted inputs + bias) before the activation function is applied.

toJSON

() => { index: number | undefined; bias: number; type: string; squash: string | null; mask: number; }

Converts the node's essential properties to a JSON object for serialization. Does not include state, activation, error, or connection information, as these are typically transient or reconstructed separately.

Returns: A JSON representation of the node's configuration.

totalDeltaBias

Accumulates changes in bias over a mini-batch during batch training. Reset after each weight update.

type

The type of the node: 'input', 'hidden', or 'output'. Determines behavior (e.g., input nodes don't have biases modified typically, output nodes calculate error differently).

ungate

(connections: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default | import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/connection").default[]) => void

Removes this node's gating control over the specified connection(s). Resets the connection's gain to 1 and removes it from the connections.gated list.

Parameters:

architecture/nodePool.ts

acquireNode

(opts: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/nodePool").AcquireNodeOptions) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default

Acquire (obtain) a node instance from the pool (or construct a new one if empty). The node is guaranteed to have fully reset dynamic state (activation, gradients, error, connections).

AcquireNodeOptions

Options bag for acquiring a node.

nodePoolStats

() => { size: number; highWaterMark: number; reused: number; fresh: number; recycledRatio: number; }

Get current pool statistics (for debugging / future leak detection).

releaseNode

(node: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default) => void

Release (recycle) a node back into the pool. The caller MUST ensure the node is fully detached from any network (connections arrays pruned, no external references maintained) to prevent leaks. After release, the node must be considered invalid until re-acquired.

Phase 2: Automatically invoked by Network.remove() when pooling is enabled to recycle pruned nodes.

resetNode

(node: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/node").default, type: string | undefined, rng: () => number) => void

Reset all mutable / dynamic fields of a node to a pristine post-construction state. This mirrors logic in the constructor & clear() while also clearing arrays & error objects.

We intentionally do NOT reset the type or squash function unless explicitly provided so callers can optionally request a different type on acquire. Bias is reinitialized consistent with constructor semantics.

resetNodePool

() => void

Reset the pool (drops all retained nodes). Intended for test harness cleanup.

ResettableNodeFields

Shape describing minimal mutable fields we explicitly reset (used internally).

architecture/onnx.ts

Conv2DMapping

Mapping declaration for treating a fully-connected layer as a 2D convolution during export. This assumes the dense layer was originally synthesized from a convolution with weight sharing; we reconstitute spatial metadata. Each mapping references an export-layer index (1-based across hidden layers, output layer would be hiddenCount+1) and supplies spatial/kernel hyperparameters. Validation ensures that input spatial * channels product equals the previous layer width and that output channels * output spatial equals the current layer width.

exportToONNX

(network: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default, options: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network/network.onnx").OnnxExportOptions) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network/network.onnx").OnnxModel

Export a minimal multilayer perceptron Network to a lightweight ONNX JSON object.

Steps:

  1. Rebuild connection cache ensuring up-to-date adjacency.
  2. Index nodes for error messaging.
  3. Infer strict layer ordering (throws if structure unsupported).
  4. Validate homogeneity & full connectivity layer-to-layer.
  5. Build initializer tensors (weights + biases) and node list (Gemm + activation pairs).

Constraints: See module doc. Throws descriptive errors when assumptions violated.

importFromONNX

(onnx: import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network/network.onnx").OnnxModel) => import("/home/runner/work/NeatapticTS/NeatapticTS/src/architecture/network").default

Import a model previously produced by {@link exportToONNX} into a fresh Network instance.

Core Steps:

  1. Parse input/output tensor shapes (supports optional symbolic batch dim).
  2. Derive hidden layer sizes (prefer layer_sizes metadata; fallback to weight tensor grouping heuristic).
  3. Instantiate matching layered MLP (inputs -> hidden[] -> outputs); remove placeholder hidden nodes for single layer perceptrons.
  4. Assign weights & biases (aggregated or per-neuron) from W/B initializers.
  5. Reconstruct activation functions from Activation node op_types (layer or per-neuron).
  6. Restore recurrent self connections from recorded diagonal Rk matrices if recurrent_single_step metadata present.
  7. Experimental: Reconstruct LSTM / GRU layers when fused initializers & metadata (lstm_emitted_layers, gru_emitted_layers) detected by replacing the corresponding hidden node block with a freshly constructed Layer.lstm / Layer.gru instance and remapping weights.
  8. Rebuild flat connection array for downstream invariants.

Experimental Behavior:

Limitations:

OnnxExportOptions

Options controlling ONNX export behavior (Phase 1).

OnnxModel

Pool2DMapping

Mapping describing a pooling operation inserted after a given export-layer index.

Generated from source JSDoc • GitHub