Table of Contents

Class Model<TInputData, TPrediction>

Namespace
NeuralNetworks.Models
Assembly
NeuralNetworks.dll

Represents an abstract neural network model that processes input data and produces predictions. Provides core functionality for forward and backward passes, training, parameter updates, and checkpointing.

public abstract class Model<TInputData, TPrediction> where TInputData : notnull where TPrediction : notnull

Type Parameters

TInputData

The type of input data provided to the model. Must not be null.

TPrediction

The type of prediction output produced by the model. Must not be null.

Inheritance
Model<TInputData, TPrediction>
Derived
Inherited Members

Remarks

This class serves as a base for implementing neural network models with customizable layers and loss functions. It supports training workflows, including batch training and parameter optimization, and provides mechanisms for saving and restoring model checkpoints. Derived classes must implement the method for constructing the layer list. Thread safety is not guaranteed; concurrent access should be managed externally.

Constructors

Model(LayerListBuilder<TInputData, TPrediction>?, Loss<TPrediction>?, SeededRandom?, string?)

protected Model(LayerListBuilder<TInputData, TPrediction>? layerListBuilder, Loss<TPrediction>? defaultLossFunction = null, SeededRandom? random = null, string? modelFilePath = null)

Parameters

layerListBuilder LayerListBuilder<TInputData, TPrediction>
defaultLossFunction Loss<TPrediction>
random SeededRandom
modelFilePath string

Properties

Random

protected SeededRandom? Random { get; }

Property Value

SeededRandom

Methods

Backward(TPrediction)

public void Backward(TPrediction lossGrad)

Parameters

lossGrad TPrediction

CalculateLoss(TPrediction, TPrediction, Loss<TPrediction>?)

public float CalculateLoss(TPrediction predictions, TPrediction targets, Loss<TPrediction>? lossFunction = null)

Parameters

predictions TPrediction
targets TPrediction
lossFunction Loss<TPrediction>

Returns

float

Describe(int)

public virtual List<string> Describe(int indentation = 0)

Parameters

indentation int

Returns

List<string>

Forward(TInputData, bool)

public TPrediction Forward(TInputData input, bool inference)

Parameters

input TInputData
inference bool

Returns

TPrediction

GetParamCount()

public int GetParamCount()

Returns

int

InferFromLayer<TLayerInputData, TLayerOutputData>(Layer<TLayerInputData, TLayerOutputData>, TLayerInputData)

protected TPrediction InferFromLayer<TLayerInputData, TLayerOutputData>(Layer<TLayerInputData, TLayerOutputData> fromLayer, TLayerInputData input) where TLayerInputData : notnull where TLayerOutputData : notnull

Parameters

fromLayer Layer<TLayerInputData, TLayerOutputData>
input TLayerInputData

Returns

TPrediction

Type Parameters

TLayerInputData
TLayerOutputData

LoadParams(string, TInputData?)

public void LoadParams(string filePath, TInputData? initializationSample = default)

Parameters

filePath string
initializationSample TInputData

SaveParams(string, string?)

public void SaveParams(string filePath, string? comment = null)

Parameters

filePath string
comment string

TrainBatch(TInputData, TPrediction, Loss<TPrediction>?)

public float TrainBatch(TInputData xBatch, TPrediction yBatch, Loss<TPrediction>? lossFunction = null)

Parameters

xBatch TInputData
yBatch TPrediction
lossFunction Loss<TPrediction>

Returns

float

UpdateParams(Optimizer)

public void UpdateParams(Optimizer optimizer)

Parameters

optimizer Optimizer