NeuralNetworkBase<T>

Base Classes · AiDotNet.NeuralNetworks

Base class for all neural network implementations in AiDotNet.

Implements: IInputGradientComputable<T>, IInterpretableModel<T>, IModelShape, INeuralNetworkModel<T>, IParameterChunkSource<T>, IParameterLayoutSource, IParameterManifestProvider, IParameterMaterializationSource, IParameterSurfaceLifecycle, ISupportsLossFunction<T>

Known derived types: ABCNet<T>, AdversarialImageEvaluator<T>, AudioNeuralNetworkBase<T>, CSPDarknet<T>, CodeModelBase<T>, Cutie<T>, DGCNN<T>, DeclaredModelLayoutBase<T>, DeepOperatorNetwork<T>, DeepRitzMethod<T>, DepthAnythingV2<T>, DocumentNeuralNetworkBase<T>, EfficientNet<T>, FinancialModelBase<T>, FourierNeuralOperator<T>, GraphNeuralOperator<T>, HamiltonianNeuralNetwork<T>, InternVideo2<T>, InverseProblemPINN<T>, LagrangianNeuralNetwork<T>, MiDaS<T>, MultiScalePINN<T>, NERNeuralNetworkBase<T>, NeuralSyntheticTabularGeneratorBase<T>, PhysicsInformedNeuralNetwork<T>, PointNetPlusPlus<T>, PointNet<T>, RVM<T>, ResNet<T>, SAM2<T>, SegmentationModelBase<T>, SlowFast<T>, StegaStampWatermarker<T>, SwinTransformer<T>, TabularNeuralNetworkBase<T>, TextConditioningBase<T>, TimeSformer<T>, TtsModelBase<T>, UniversalDifferentialEquation<T>, VGG16BNBackbone<T>, VGG16Backbone<T>, VariationalPINN<T>, VideoCLIP<T>, VideoMAE<T>, VideoNeuralNetworkBase<T>, VisionLanguageModelBase<T>, XMem<T>, YOLOv9Backbone<T>, YoloStagedBackboneBase<T>

For Beginners

A neural network is a computing system inspired by the human brain. It consists of interconnected “layers” of artificial neurons that process information and learn patterns from data. This class provides the foundation for building different types of neural networks.

Properties

Property Summary
ICreditRule<T> ActiveCreditRule { get; } Gets the configured credit-assignment rule, or null when standard back-propagation is used.
ILossFunction<T> DefaultLossFunction { get; } Gets the default loss function for this network.
List<ILayer<T>> Layers { get; } Gets the collection of layers that make up this neural network (read-only access).
ParameterLayoutSnapshot ParameterLayout { get; }
StreamingTrainingMode StreamingTraining { get; set; } Performs tape-based forward/backward pass and delegates the parameter update to the provided optimizer via Step.
bool CanTrainOnGpu { get; } Gets whether GPU-resident training can be used right now.
bool FastApproxGradClip { get; set; } #1662 lever #1 (§5c) — opt-in single-pass approximate gradient clipping for the streaming (optimizer-in-backward) path.
bool HasUninitializedParameters { get; } true when some layer will have trainable parameters but cannot size them until it sees its first input.
bool IsGradientCheckpointingEnabled { get; } Gets whether gradient checkpointing is enabled.
bool IsMemoryManagementEnabled { get; } Gets whether memory management (gradient checkpointing/pooling) is enabled.
bool IsMixedPrecisionEnabled { get; } Gets whether mixed-precision training is enabled.
bool IsTrainingMode { get; set; } Indicates whether the network is currently in training mode.
bool LastStepHadNonFiniteGradients { get; set; } Best-effort read of the supplied optimizer’s current learning rate, used by the network-level extras update path.
bool SupportsGpuTraining { get; } Gets whether all layers in the network support GPU-resident training.
bool SupportsParameterInitialization { get; }
bool SupportsTraining { get; } Indicates whether this network supports training (learning from data).
double MaxGradNormValue { get; } Maximum allowed global L2 norm for gradients per training step, exposed as a double.
double StreamingTrainingLearningRate { get; set; } Learning rate used by the default streaming 8-bit Adam epilogue.
double StreamingTrainingWeightDecay { get; set; } Decoupled weight decay used by the default streaming Adam epilogue.
int LayerCount { get; } Gets the number of layers in this neural network.
long ParameterCount { get; } Gets the total number of parameters in the model.
long TrainableParameterCount { get; } Gets the number of values the optimizer trains: ParameterCount without the buffers.

Methods

Method Summary
BoundInputContract BindInputContract(int[] inputShape, string variant) Binds external input semantics and geometry to concrete instance configuration.
Dictionary<int, Tensor<T>> GetLayerActivations(Tensor<T> input) Gets the activations (outputs) from each layer for a given input.
Dictionary<string, T> GetFeatureImportance() Gets the feature importance scores for the model.
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) Gets the intermediate activations from each layer when processing the given input with named keys.
DynamicShapeInfo GetDynamicShapeInfo()
GpuExecutionContext BeginGpuExecution(GpuExecutionOptions options) Begins a GPU execution context for managing GPU-resident tensor lifecycle.
GpuMemoryStats GetGpuMemoryStats() Gets GPU memory statistics if running within a GPU execution context.
IEnumerable<ParameterChunk<T>> GetParameterStateChunks()
IEnumerable<Tensor<T>> GetParameterChunks()
IEnumerable<int> GetActiveFeatureIndices() Gets the indices of input features that are actively used by the network.
IFullModel<T, Tensor<T>, Tensor<T>> Clone() Creates a clone of the neural network.
IFullModel<T, Tensor<T>, Tensor<T>> DeepCopy() Determines whether every layer in the executable graph can safely consume shared parameter storage during its forward pass.
IFullModel<T, Tensor<T>, Tensor<T>> WithParameters(Vector<T> parameters) Creates a new neural network with the specified parameters.
IReadOnlyList<LayerInfo<T>> GetAllLayerInfo() Gets metadata for all layers, including parameter offsets, types, shapes, names, and cost estimates.
IReadOnlyList<ParameterSlotDescriptor> GetParameterLayout()
InputContractManifest GetInputContract(int[] inputShape) Returns the canonical external-input schema inferred from the first semantic consumer.
LayerInfo<T> GetLayerInfo(int layerIndex) Gets metadata for a specific layer including its parameter offset within the flat parameter vector.
LayerInputDomain GetInputDomain(int[] inputShape) Retrieves the gradients for all trainable parameters in the network.
LayerInputDomain GetOutputDomain(int[] outputShape) Gets the value domain produced by this network’s public output.
MemorySavingsEstimate GetMemoryEstimate(int batchSize, int sequenceLength) Gets memory usage statistics if memory management is enabled.
ModelInputShapeConstraint GetInputShapeConstraint() Minimum external input geometry required by this model.
ModelMetadata<T> GetModelMetadata() Gets the metadata for this neural network model.
ModelOptions GetOptions()
NeuralNetworkArchitecture<T> GetArchitecture() Gets the architectural structure of the neural network.
NeuralNetworkBase<T> To(DeviceInfo device) Moves the whole model — every layer’s parameters and buffers — to the given device, the model-level equivalent of PyTorch’s model.to(device).
SubModel<T> ExtractSubModel(int startLayer, int endLayer) Extracts a contiguous sub-model from startLayer to endLayer (inclusive).
T GetLastLoss()
T TrainBatchGpuDeferred(Tensor<T> input, Tensor<T> target, IGpuOptimizerConfig config, GpuExecutionOptions options) Performs a complete training step (forward + backward + update) on GPU with deferred execution.
T TrainWithCustomLoss(Tensor<T> input, Func<Tensor<T>, Tensor<T>> computeLoss, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer)
Task<AnchorExplanation<T>> GetAnchorExplanationAsync(Tensor<T> input, T threshold) Gets anchor explanation for a given input.
Task<CounterfactualExplanation<T>> GetCounterfactualAsync(Tensor<T> input, Tensor<T> desiredOutput, int maxChanges) Gets counterfactual explanation for a given input and desired output.
Task<DeepLIFTExplanation<T>> GetDeepLIFTAsync(Tensor<T> input, Tensor<T> baseline, bool useRevealCancel) Gets DeepLIFT attributions for a neural network prediction.
Task<Dictionary<int, T>> GetGlobalFeatureImportanceAsync() Gets the global feature importance across all predictions.
Task<Dictionary<int, T>> GetLocalFeatureImportanceAsync(Tensor<T> input) Gets the local feature importance for a specific input.
Task<Dictionary<string, object>> GetModelSpecificInterpretabilityAsync() Gets model-specific interpretability information.
Task<FairnessMetrics<T>> ValidateFairnessAsync(Tensor<T> inputs, int sensitiveFeatureIndex) Validates fairness metrics for the given inputs.
Task<GradCAMExplanation<T>> GetGradCAMAsync(Tensor<T> input, int targetClass) Gets GradCAM visual explanation for a CNN prediction.
Task<IntegratedGradientsExplanation<T>> GetIntegratedGradientsAsync(Tensor<T> input, Tensor<T> baseline, int numSteps) Gets Integrated Gradients attributions for a neural network prediction.
Task<LimeExplanation<T>> GetLimeExplanationAsync(Tensor<T> input, int numFeatures) Gets LIME explanation for a specific input.
Task<Matrix<T>> GetShapValuesAsync(Tensor<T> inputs) Gets SHAP values for the given inputs.
Task<PartialDependenceData<T>> GetPartialDependenceAsync(Vector<int> featureIndices, int gridResolution) Gets partial dependence data for specified features.
Task<T> GetFeatureInteractionAsync(int feature1Index, int feature2Index) Gets feature interaction effects between two features.
Task<T> TrainBatchGpuDeferredAsync(Tensor<T> input, Tensor<T> target, IGpuOptimizerConfig config, GpuExecutionOptions options, CancellationToken cancellationToken) Performs a complete training step (forward + backward + update) on GPU with deferred execution asynchronously.
Task<Tensor<T>> ForwardDeferredAsync(Tensor<T> input, CancellationToken cancellationToken) Performs an asynchronous forward pass using deferred execution for optimized GPU performance.
Task<string> GenerateTextExplanationAsync(Tensor<T> input, Tensor<T> prediction) Generates a text explanation for a prediction.
Tensor<T> ForwardDeferred(Tensor<T> input) Performs a forward pass using deferred execution for optimized GPU performance.
Tensor<T> ForwardForTraining(Tensor<T> input)
Tensor<T> ForwardGpu(Tensor<T> input) Performs a GPU-resident forward pass, keeping intermediate results on GPU.
Tensor<T> ForwardGpu(Tensor<T> input, IReadOnlyDictionary<string, Tensor<T>> auxiliaryInputs) Performs a forward pass through the network entirely on GPU.
Tensor<T> ForwardWithGpuContext(Tensor<T> input) Performs a GPU-resident forward pass within a GPU execution context.
Tensor<T> ForwardWithMemory(Tensor<T> input) Performs a forward pass through the network while storing intermediate values for backpropagation.
Tensor<T> ForwardWithMemory(Tensor<T> input, IReadOnlyDictionary<string, Tensor<T>> auxiliaryInputs) Forward pass with named auxiliary inputs that are routed to layers declaring matching ports.
Tensor<T> Predict(Tensor<T> input)
Tensor<T> Predict(Tensor<T> input, Tensor<T> auxiliary) Runs inference with a second input, for models that consume two modalities.
Tensor<T> PredictInBatches(Tensor<T> input, int batchSize) Chunked inference path: splits input along axis 0 into slices of size batchSize, runs Predict on each slice, and concatenates the per-slice outputs back along axis 0.
Tensor<T> PredictWithGradients(Tensor<T> input) Runs the same inference forward as Predict (evaluation mode: dropout off, batch normalization on running statistics) but records it on the active GradientTape, so gradients with respect to the input – and higher derivatives – can be t…
ValueTuple<Tensor<T>, Dictionary<int, Tensor<T>>> ForwardWithFeatures(Tensor<T> input, int[] layerIndices) Performs a forward pass and returns intermediate layer activations for feature extraction.
Vector<T> ComputeGradients(Tensor<T> input, Tensor<T> auxiliary, Tensor<T> target, ILossFunction<T> lossFunction) Computes gradients for a sample whose input has two modalities.
Vector<T> ComputeGradients(Tensor<T> input, Tensor<T> target, ILossFunction<T> lossFunction)
Vector<T> GetGradients() Gets the gradients from all layers in the neural network.
Vector<T> GetParameterGradients()
Vector<T> GetParameters()
Vector<T> SanitizeParameters(Vector<T> parameters)
bool CompileForward(Tensor<T> sampleInput) Eagerly traces and compiles the forward pass for the given input shape, storing the compiled plan in the per-instance cache.
bool IsFeatureUsed(int featureIndex) Determines if a specific input feature is actively used by the network.
bool ValidatePartitionPoint(int afterLayerIndex) Validates that a partition point between layers is valid for pipeline parallelism.
byte[] Serialize() Serializes the neural network to a byte array.
int[] GetInputShape() Gets the input shape expected by the neural network.
int[] GetOutputShape()
long GetParameterCount() Gets the total number of parameters in the model.
void AddBatchNormalizationLayer(int featureSize, double epsilon, double momentum) Adds a batch normalization layer to the neural network.
void AddConvolutionalLayer(int filters, int kernelSize, int stride, ActivationFunction activation) Adds a convolutional layer to the neural network.
void AddDropoutLayer(double dropoutRate) Adds a dropout layer to the neural network.
void AddLSTMLayer(int units, bool returnSequences) Adds an LSTM layer to the neural network.
void AddLayer(LayerType layerType, int units, ActivationFunction activation) Adds a layer to the neural network.
void AddPoolingLayer(int[] inputShape, PoolingType poolingType, int poolSize, int? strides) Adds a pooling layer to the neural network.
void ApplyGradients(Vector<T> gradients, T learningRate) Applies a flattened gradient vector to update the network’s parameters.
void ConfigureFairness(Vector<int> sensitiveFeatures, FairnessMetric[] fairnessMetrics) Configures fairness evaluation settings.
void Deserialize(byte[] data) Deserializes the neural network from a byte array.
void DisableGradientCheckpointing() Disables activation gradient checkpointing previously enabled on this network.
void DisableMemoryManagement() Disables memory management and releases associated resources.
void Dispose() Disposes resources used by the neural network.
void DownloadWeightsFromGpu() Downloads all layer weights from GPU back to CPU.
void EnableDeterministicMode() Enables deterministic CPU inference by setting MKL to single-threaded.
void EnableGradientCheckpointing(int segmentSize) Enables activation gradient checkpointing during training with the specified segment size, or 0 to select max(1, floor(sqrt(Layers.Count))) automatically.
void EnableInferenceAcceleration(bool enabled) Opts this model into the #1622 verify-then-trust compiled-inference path regardless of size, the industry-standard explicit “compile this model” gesture (cf.
void EnableMemoryManagement(TrainingMemoryConfig config) Enables memory management with the specified configuration.
void EnableMethod(InterpretationMethod[] methods) Enables specific interpretation methods.
void LoadModel(string filePath) Loads a neural network model from a file.
void LoadState(Stream stream) Loads the model’s state from a stream.
void MaterializeParameters() Brings this network’s weights into existence now, for a network whose architecture already determines every shape.
void PredictInBatches(Tensor<T> input, int batchSize, Action<Tensor<T>, int> consume) Streaming chunked inference: splits input along axis 0 into slices of batchSize, runs Predict on each slice, invokes consume with that slice’s output and its axis-0 start offset, then DISPOSES the slice output before advancing.
void ReleaseCompiledPlans() Releases every compiled inference plan this network holds, returning their pre-allocated intermediate buffers to the GC.
void ResetState() Resets the internal state of the different layers, clearing any remembered information.
void ResolveShapes(Tensor<T> sampleInput) Runs a one-shot dry-run forward with the supplied sample input to materialize every lazy-shape layer.
void SaveModel(string filePath) Saves the model to a file.
void SaveState(Stream stream) Saves the model’s current state to a stream.
void SetActiveFeatureIndices(IEnumerable<int> featureIndices)
void SetBaseModel(IFullModel<T, TInput, TOutput> model) Sets the base model for interpretability analysis.
void SetCreditRule(ICreditRule<T> rule, int? seed) Selects a pluggable credit-assignment rule for training.
void SetLossFunction(ILossFunction<T> lossFunction)
void SetParameterChunks(IEnumerable<Tensor<T>> chunks) Writes a chunk stream back into this model without building a flat vector.
void SetParameters(Vector<T> parameters) Sets the parameters of the neural network.
void SetTrainingMode(bool isTraining) Sets the neural network to either training or inference mode.
void Train(Tensor<T> input, IReadOnlyList<Tensor<T>> targets)
void Train(Tensor<T> input, Tensor<T> auxiliary, Tensor<T> expectedOutput) Trains on a sample whose input has two modalities.
void Train(Tensor<T> input, Tensor<T> expectedOutput)
void TrainBatched(Tensor<T>[] inputs, Tensor<T>[] targets) Trains the network on a batch of input/target pairs in a single optimizer step.
void TrainWithGradientAccumulation(Tensor<T> input, Tensor<T> target, int batchSize)
void UpdateParameters(Vector<T> parameters) Updates the network’s parameters with new values.
void UpdateParametersGpu(IGpuOptimizerConfig config) Updates all trainable parameters in the network using the specified optimizer configuration.
void UpdateParametersGpu(T learningRate, T momentum, T weightDecay) Updates all trainable parameters in the network using GPU-computed gradients.
void UpdateParametersGpuDeferred(IGpuOptimizerConfig config, GpuExecutionOptions options) Updates all trainable parameters with deferred GPU execution.
void UploadWeightsToGpu() Uploads all layer weights to GPU for GPU-resident training.
void ZeroGradientsGpu() Zeros all GPU gradient accumulators in preparation for a new batch.

Fields

Field Summary
NeuralNetworkArchitecture<T> Architecture The architecture definition for this neural network.