TensorLayoutAttribute
Attributes · AiDotNet.Attributes
Declares one tensor layout a layer or model accepts as input, or produces as output, as an ordered list of axis ROLES.
Inheritance: Attribute → TensorLayoutAttribute
For Beginners
This says, in a way the compiler and the test generator can both read, “this layer expects a batch of images with channels, height and width, in that order”.
How It Works
A type may carry SEVERAL of these — one per accepted form — and validation succeeds if ANY of them
matches. That is not a convenience: DenseLayer genuinely accepts both [N, Features] and
[N, Time, Features], and refusing to model that would either force false declarations or
require adapter layers at call sites that work correctly today.
Batch can be marked OPTIONAL via BatchOptional, so
[C,H,W] and [B,C,H,W] are one declaration rather than two. Most models in this codebase
accept both, and writing every layout twice would double the annotation burden for no information.
WHAT THIS ATTRIBUTE DOES NOT CARRY: tied relationships between axes, such as a super-resolution
model’s output Width = ScaleFactor * input Width. C# attributes take only compile-time
constants and ScaleFactor is a runtime option, so those live in
IShapeContract instead, where they are computed from the type’s
ACTUAL configuration and therefore cannot go stale.
AN ATTRIBUTE IS A CLAIM. Every annotated type gets a generated conformance test that feeds the declared layout and asserts it is accepted, plus a wrong-rank case asserting it is rejected. Without that, this becomes a second source of truth that can silently stop matching the code — the same failure mode as a citation that no longer describes its implementation.
Constructors
| Constructor | Summary |
|---|---|
TensorLayoutAttribute(TensorAxis[] axes) |
Declares a layout as an ordered list of axis roles. |
Properties
| Property | Summary |
|---|---|
TensorAxis[] Axes { get; } |
The axis roles, in order, outermost first. |
TensorLayoutDirection Direction { get; set; } |
Whether this declaration describes the type’s INPUT (the default) or its OUTPUT. |
bool BatchOptional { get; set; } |
When true, Batch may be absent, so this one declaration covers both the batched and unbatched forms. |
int Rank { get; } |
The rank this layout describes when the batch axis is present. |
string Note { get; set; } |
Optional note explaining a non-obvious layout, surfaced in validation failures. |
Methods
| Method | Summary |
|---|---|
TensorAxis[] AxesForRank(int rank) |
The axis roles as they appear for a tensor of rank, dropping the optional batch axis when the caller supplied the unbatched form. |
bool AcceptsRank(int rank) |
True when a tensor of rank could match this declaration, allowing for an omitted optional batch axis. |
string ToString() |
Renders the layout as, for example, [Batch?, Channels, Height, Width]. |