LayerGraph<T>

Models & Types · AiDotNet.NeuralNetworks.Graph

The wiring of a model, expressed once: which layer feeds which, and what happens on the edges between them.

For Beginners

Most networks are a straight line — layer 1 feeds layer 2 feeds layer 3. Some are not: one layer’s output can feed two others, and results can be reshaped in between. This records the real structure so the rest of the system stops assuming a straight line.

How It Works

WHY THIS EXISTS. The flat Layers list cannot express what these models actually do. ABCNet has two detection heads reading one backbone, and a permute-plus-flatten between its last convolution and its dense head. A positional walk of that list therefore reports mismatches that are not real, and — far worse — the inherited sequential training path computes the WRONG GRAPH while raising nothing.

The graph is the single source of truth for wiring, so it cannot drift from the forward pass the way a second description would. ToLayerList projects it back to the flat list, which is what keeps parameter counting, cloning and serialization working untouched: they iterate nodes either way.

MODELS THAT DECLARE NOTHING KEEP WORKING. Linear builds the implicit chain that a flat list has always meant, so the several hundred existing models need no migration and behave exactly as before.

Properties

Property Summary
IReadOnlyList<LayerNode<T>> Nodes { get; } The nodes, in the order they were added.
bool HasFanOut { get; } True when at least one node’s output feeds more than one consumer.
bool IsLinear { get; } True when every node has at most one consumer and no edge transforms — a plain chain.
int OutputNodeId { get; } Id of the node whose output is the model’s output.

Methods

Method Summary
IReadOnlyList<IReadOnlyList<ILayer<T>>> ContiguousRuns() The maximal runs of nodes that are genuinely sequential — single predecessor, single consumer, and no edge transform between them.
LayerGraph<T> Linear(IReadOnlyList<ILayer<T>> layers) Builds the implicit linear graph a flat layer list has always meant: each layer consumes the one before it.
List<ILayer<T>> ToLayerList() The layers in node order — the projection the rest of the system consumes.
Tensor<T> Forward(Tensor<T> input) Executes the graph, honouring branches and edge transforms.
int[] ResolveShapes(int[] inputShape) Resolves every lazy layer in the graph from its REAL predecessor, without allocating weights.
string ToString()