LayerShape

Structs · AiDotNet.NeuralNetworks.Layers

A layer’s shape, in which individual axes may be dynamic and may be named.

Inheritance: ValueType → LayerShape

Implements: IEquatable<LayerShape>

For Beginners

Some parts of a tensor’s shape are known in advance (how many features a layer outputs) and some are not (how many words are in this particular sentence). This type keeps track of which is which, gives the unknown ones names so two of them can be declared equal, and refuses to hand out a number for an axis nobody knows yet.

How It Works

A plain int[] cannot say “this axis is fixed and that one is not”, so every layer whose output length depends on its runtime input had to either invent a value or refuse to resolve. Inventing is what went wrong repeatedly: PositionalEncodingLayer declared its encoding table size, CrossAttentionLayer declared a defaulted 64, and TransformerDecoderLayer declared whatever its first input happened to be and then went stale. Each fabricated number was then read as fact by parameter-vector slicing, chain resolution and ONNX export.

Where this sits relative to other frameworks. Keras represents an unknown axis as None and ONNX gives it a name; PyTorch has no static shape contract at all. Two things here go past that:

Constructors

Constructor Summary
LayerShape(int[] axes) Creates a shape from axis sizes, using Dynamic for unknown axes.
LayerShape(int[] axes, string[] names) Creates a shape whose dynamic axes carry names.

Properties

Property Summary
bool HasDynamicAxis { get; } true when at least one axis is dynamic.
bool IsFullyConcrete { get; } true when every axis has a known size.
int Item { get; }
int Rank { get; } Number of axes.

Methods

Method Summary
bool Equals(LayerShape other)
bool Equals(object obj)
bool TryGetConcrete(int[] concrete) Gets the concrete axis sizes, or returns false if any axis is dynamic.
int GetHashCode()
int[] RequireConcrete(string reason) Gets the concrete axis sizes, throwing when any axis is dynamic.
int[] ToArray() The axis sizes as a plain array, with Dynamic for unknown axes.
string NameOf(int axis) The name of an axis, or null when it is unnamed.
string ToString()

Fields

Field Summary
int Dynamic Marks an axis whose size is not known until a real input arrives.