IWeightLoadable<T>

Interfaces · AiDotNet.Interfaces

Defines the contract for models that support loading weights by name.

Known implementations: ILayer<T>

For Beginners

Think of this as a way to “transplant” knowledge from pretrained models. Each weight has a name (like “encoder.conv1.weight”) and this interface lets us set those weights by their names.

Example:

// Load pretrained weights
var safeTensorsLoader = new SafeTensorsLoader<float>();
var weights = safeTensorsLoader.Load("model.safetensors");

var architecture = new NeuralNetworkArchitecture<float>(
InputType.OneDimensional, NeuralNetworkTaskType.Regression, inputSize: 8, outputSize: 1);
var model = new NeuralNetwork<float>(architecture);

// Apply to model
if (model is IWeightLoadable<float> loadable)
{
loadable.SetParameter("encoder.conv1.weight", weights["encoder.conv1.weight"]);
}

How It Works

This interface enables loading pretrained weights from external sources like SafeTensors, HuggingFace, and ONNX files into AiDotNet models.

Properties

Property Summary
int NamedParameterCount { get; } Gets the total number of named parameters.

Methods

Method Summary
IEnumerable<string> GetParameterNames() Gets all parameter names in this model.
WeightLoadResult LoadWeights(Dictionary<string, Tensor<T>> weights, Func<string, string> mapping, bool strict) Loads weights from a dictionary of tensors using optional name mapping.
WeightLoadValidation ValidateWeights(IEnumerable<string> weightNames, Func<string, string> mapping) Validates that a set of weight names can be loaded into this model.
bool SetParameter(string name, Tensor<T> value) Sets a parameter tensor by name.
bool TryGetParameter(string name, Tensor<T> tensor) Tries to get a parameter tensor by name.
int[] GetParameterShape(string name) Gets the expected shape for a parameter.