SparseNeuralNetwork<T>

Models & Types · AiDotNet.NeuralNetworks

Represents a Sparse Neural Network with efficient sparse weight matrices.

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → VectorModelLayoutBase<T> → SparseNeuralNetwork<T>

For Beginners

In a regular neural network, every neuron in one layer is connected to every neuron in the next layer. In a sparse network, many of these connections are removed (set to zero), keeping only the most important ones. This has several benefits:

  • Uses less memory (only stores non-zero values)
  • Runs faster (skips multiplications with zero)
  • Can prevent overfitting (acts as regularization)
  • Enables very large networks to fit in limited memory

Common use cases include:

  • Network compression for mobile/edge deployment
  • Recommender systems with sparse user-item matrices
  • Graph neural networks with sparse adjacency matrices
  • Pruned networks from neural architecture search

How It Works

A Sparse Neural Network uses sparse weight matrices where most values are zero. This provides significant memory and computational savings for large networks, especially when combined with network pruning techniques.

Example

var architecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 4);
var input = Tensor<float>.CreateRandom(new[] { 1, 784 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureModel(new SparseNeuralNetwork<float>(architecture))
    .Build(trainX, trainY);
var output = result.Predict(input);

Constructors

Constructor Summary
SparseNeuralNetwork() Initializes a new instance of the SparseNeuralNetwork class.
SparseNeuralNetwork(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, SparseNeuralNetworkOptions options)

Properties

Property Summary
bool SupportsTraining { get; } Serializes sparse neural network-specific data to a binary writer.

Methods

Method Summary
ModelMetadata<T> GetModelMetadata() Retrieves metadata about the sparse neural network model.
Tensor<T> Forward(Tensor<T> input) Performs a forward pass through the network with the given input tensor.