TrainableParameterAttribute

Attributes · AiDotNet.Attributes

Marks a Tensor field as a trainable parameter that should be registered with the gradient tape training system.

Inheritance: Attribute → TrainableParameterAttribute

For Beginners

Put this attribute on any tensor field that the network should learn during training. The framework handles everything else — registering it, exposing it to the optimizer, and clearing gradients between training steps.

How It Works

The TrainableParameterGenerator source generator discovers fields marked with this attribute and automatically emits:

  • GetTrainableParameters() — returns all marked fields in declaration order
  • SetTrainableParameters(Tensor<T>[]) — updates each marked field from the array
  • ZeroGrad() — zeros gradient fields discovered by convention ({fieldName}Gradient)

This is the equivalent of PyTorch’s nn.Parameter — marking a tensor as trainable makes it automatically visible to the optimizer and gradient tape with zero manual boilerplate.

Convention for gradient fields: For a parameter field named _weights, the generator looks for _weightsGradient (nullable Tensor<T>?). If found, ZeroGrad() will zero or null it automatically.

Example

public partial class MyLayer<T> : LayerBase<T>
{
    [TrainableParameter(Role = PersistentTensorRole.Weights)]
    private Tensor<T> _weights;
            
    [TrainableParameter(Role = PersistentTensorRole.Biases)]
    private Tensor<T> _biases;
            
    private Tensor<T>? _weightsGradient;  // auto-discovered by convention
    private Tensor<T>? _biasesGradient;   // auto-discovered by convention
}

Constructors

Constructor Summary
TrainableParameterAttribute()

Properties

Property Summary
ParameterAvailability Availability { get; set; } Gets or sets when the parameter is expected to become available.
PersistentTensorRole Role { get; set; } Gets or sets the role of this parameter for GPU memory management hints.
bool Optional { get; set; } Gets or sets whether this parameter is optional: a conditionally-used, lazily-materialized field that stays a zero-length [0,0] placeholder until (and unless) the layer actually needs it.
int Order { get; set; } Gets or sets the display order of this parameter in GetTrainableParameters().
string Condition { get; set; } Gets or sets the name of an instance Boolean field or property that enables this parameter for the configured layer instance.
string LowPrecisionBacking { get; set; } Gets or sets the name of an optional Tensor<Half> field that becomes the authoritative value store when this parameter is kept resident at low precision.
string Shape { get; set; } Gets or sets the shape this parameter must have once the layer’s own shapes are resolved, written as a comma-separated list of C# expressions evaluated in the layer’s own scope — for example "InputShape[0], OutputShape[0]" for a weight ma…