Class SpikingNeuralNetwork<T>
- Namespace
- AiDotNet.NeuralNetworks
- Assembly
- AiDotNet.dll
Represents a Spiking Neural Network, which is a type of neural network that more closely models biological neurons with temporal dynamics.
public class SpikingNeuralNetwork<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable
Type Parameters
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
SpikingNeuralNetwork<T>
- Implements
- Inherited Members
- Extension Methods
Constructors
SpikingNeuralNetwork(NeuralNetworkArchitecture<T>, double, int, IActivationFunction<T>?, ILossFunction<T>?)
Initializes a new instance of the SpikingNeuralNetwork<T> class with the specified architecture and a scalar activation function.
public SpikingNeuralNetwork(NeuralNetworkArchitecture<T> architecture, double timeStep = 0.1, int simulationSteps = 100, IActivationFunction<T>? scalarActivation = null, ILossFunction<T>? lossFunction = null)
Parameters
architectureNeuralNetworkArchitecture<T>The neural network architecture to use for the SNN.
timeStepdoubleThe simulation time step, defaults to 0.1.
simulationStepsintThe number of time steps to simulate, defaults to 100.
scalarActivationIActivationFunction<T>The scalar activation function to use. If null, a default activation is used.
lossFunctionILossFunction<T>
SpikingNeuralNetwork(NeuralNetworkArchitecture<T>, double, int, IVectorActivationFunction<T>?, ILossFunction<T>?)
Initializes a new instance of the SpikingNeuralNetwork<T> class with the specified architecture and a vector activation function.
public SpikingNeuralNetwork(NeuralNetworkArchitecture<T> architecture, double timeStep = 0.1, int simulationSteps = 100, IVectorActivationFunction<T>? vectorActivation = null, ILossFunction<T>? lossFunction = null)
Parameters
architectureNeuralNetworkArchitecture<T>The neural network architecture to use for the SNN.
timeStepdoubleThe simulation time step, defaults to 0.1.
simulationStepsintThe number of time steps to simulate, defaults to 100.
vectorActivationIVectorActivationFunction<T>The vector activation function to use. If null, a default activation is used.
lossFunctionILossFunction<T>
Methods
CreateNewInstance()
Creates a new instance of the Spiking Neural Network with the same architecture and configuration.
protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()
Returns
- IFullModel<T, Tensor<T>, Tensor<T>>
A new instance of the Spiking Neural Network with the same configuration as the current instance.
Remarks
This method creates a new spiking neural network with the same architecture, temporal parameters, and activation function type as the current instance. The new instance has freshly initialized parameters and state, making it useful for creating separate instances with the same configuration or for resetting the network while preserving its structure.
For Beginners: This creates a brand new spiking neural network with the same setup.
Think of it like cloning your network's blueprint:
- It has the same structure (layers, neurons)
- It has the same temporal settings (time step, simulation steps)
- It uses the same type of activation function
- But it starts fresh with new connections and neuron states
This is useful when you want to:
- Start over with a fresh network but keep the same design
- Create multiple networks with identical settings for comparison
- Reset a network to its initial state
The new network will need to be trained from scratch, as it doesn't inherit any of the learned weights from the original network.
DeserializeNetworkSpecificData(BinaryReader)
Deserializes SNN-specific data from a binary reader.
protected override void DeserializeNetworkSpecificData(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to read from.
Remarks
This method loads spiking neural network-specific data from the binary stream. It restores temporal parameters, neuron model parameters, and the state of all neurons, allowing the network to continue operation from exactly where it left off when serialized.
For Beginners: This loads all the network's configuration and state from a file.
The deserialization process loads:
- Temporal parameters (time step, simulation steps)
- Neuron model parameters (membrane decay, refractory period)
- State of all neurons (membrane potentials, thresholds)
This allows you to:
- Load a previously trained network
- Continue using or training a network exactly where you left off
- Share networks between different systems or users
GetModelMetadata()
Gets metadata about the spiking neural network.
public override ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetaData object containing information about the network.
Remarks
This method returns comprehensive metadata about the spiking neural network, including its architecture, temporal parameters, neuron model details, and other relevant information. This metadata is useful for documentation, model comparison, and analysis.
For Beginners: This provides detailed information about your spiking neural network.
The metadata includes:
- Basic information about the network structure
- Temporal parameters (time step, simulation steps)
- Neuron model details (membrane decay, refractory period)
- The total number of parameters (weights, thresholds, etc.)
This information is useful for:
- Documentation and record-keeping
- Comparing different network configurations
- Understanding the network's behavior
- Reproducing results in future experiments
InitializeLayers()
Initializes the neural network layers based on the provided architecture or default configuration.
protected override void InitializeLayers()
Predict(Tensor<T>)
Makes a prediction using the spiking neural network.
public override Tensor<T> Predict(Tensor<T> input)
Parameters
inputTensor<T>The input tensor to process.
Returns
- Tensor<T>
The output tensor after processing through all layers.
Remarks
This method processes the input through the spiking neural network by simulating the network's dynamics over a specified number of time steps. It updates the membrane potentials of all neurons at each step, generates spikes when thresholds are exceeded, and tracks the refractory periods. The final output is an aggregation of the spike activity over all time steps.
For Beginners: This method runs the network simulation to process your input data.
The prediction process works like this:
- Reset the network to its initial state
- Feed the input to the network
- Simulate the network for a set number of time steps:
- Update neuron membrane potentials
- Generate spikes when thresholds are reached
- Track refractory periods after neurons fire
- Record the spike activity of the output neurons
- Aggregate this activity into a final result
The result represents how strongly each output neuron responded to the input, based on its spiking activity throughout the simulation.
ResetState()
Resets the internal state of the network.
public override void ResetState()
Remarks
This method resets all internal states of the spiking neural network, including membrane potentials and refractory counters. This is typically called before processing a new, unrelated input, to ensure that the network's response is not influenced by previous inputs.
For Beginners: This clears the network's "memory" to start fresh.
When resetting the state:
- All neuron membrane potentials return to zero (resting state)
- All refractory counters are reset (neurons are ready to fire)
- The network "forgets" any previous activity
This is important when:
- Processing a new, unrelated input
- Starting a new simulation
- Ensuring the network's response isn't influenced by previous inputs
Think of it like clearing a whiteboard before writing new information.
SerializeNetworkSpecificData(BinaryWriter)
Serializes SNN-specific data to a binary writer.
protected override void SerializeNetworkSpecificData(BinaryWriter writer)
Parameters
writerBinaryWriterThe binary writer to write to.
Remarks
This method saves spiking neural network-specific data to the binary stream. It includes temporal parameters like time step and simulation steps, neuron model parameters like membrane decay and refractory period, and the current state of all neurons including membrane potentials and firing thresholds.
For Beginners: This saves all the network's configuration and state to a file.
The serialization process saves:
- Temporal parameters (time step, simulation steps)
- Neuron model parameters (membrane decay, refractory period)
- Current state of all neurons (membrane potentials, thresholds)
This allows you to:
- Save a trained network to use later
- Share your network with others
- Continue training from where you left off
- Create a snapshot of the network's state at a specific point
SetLayerThresholds(int, Vector<T>)
Sets custom firing thresholds for neurons in a specific layer.
public void SetLayerThresholds(int layerIndex, Vector<T> thresholds)
Parameters
layerIndexintThe index of the layer.
thresholdsVector<T>The vector of firing thresholds.
Remarks
This method allows customization of firing thresholds for individual neurons in a specific layer. Neurons with lower thresholds will fire more easily, while those with higher thresholds require stronger input to generate spikes. This can be used to make certain neurons more or less sensitive to input patterns.
For Beginners: This lets you adjust how easily each neuron fires.
The firing threshold:
- Determines how much charge a neuron needs before it generates a spike
- Lower threshold = neuron fires more easily
- Higher threshold = neuron needs stronger input to fire
By setting custom thresholds for each neuron:
- You can make some neurons more sensitive to specific patterns
- You can create neurons that specialize in detecting different features
- You can balance the overall activity of the network
This fine-grained control allows you to optimize how the network responds to different types of input patterns.
SetNeuronModelParameters(T, int)
Sets the neuron model parameters for the network.
public void SetNeuronModelParameters(T membraneDecay, int refractoryPeriod)
Parameters
membraneDecayTThe membrane potential decay constant (0-1).
refractoryPeriodintThe refractory period in time steps.
Remarks
This method configures the key parameters of the neuron model used in the spiking neural network. The membrane decay determines how quickly neuron membrane potentials decay over time, while the refractory period sets how many time steps a neuron must wait after firing before it can fire again.
For Beginners: This lets you adjust how the neurons in your network behave.
You can customize:
Membrane decay: How quickly neurons lose their built-up charge (0-1)
- Values closer to 1 mean neurons "remember" inputs longer
- Values closer to 0 mean neurons quickly "forget" inputs
Refractory period: How long neurons need to recover after firing
- Measured in simulation time steps
- Longer periods limit how rapidly neurons can fire
These parameters affect how your network processes temporal patterns:
- Higher decay and longer refractory periods help detect slow, sustained patterns
- Lower decay and shorter refractory periods help detect quick, transient patterns
SetSimulationParameters(double, int)
Sets the simulation parameters for the network.
public void SetSimulationParameters(double timeStep, int simulationSteps)
Parameters
Remarks
This method configures the temporal aspects of the spiking neural network simulation. The time step determines the temporal resolution of the simulation, while the simulation steps determine how many steps to run for each input. Together, they define the total simulation time.
For Beginners: This lets you adjust how time works in your network simulation.
You can customize:
Time step: How finely time is sliced in the simulation
- Smaller values give more precise timing but require more computation
- Typical values range from 0.01 to 1.0
Simulation steps: How many time steps to run for each input
- More steps allow longer temporal patterns to develop
- But require more computation time
The total simulation time is: timeStep * simulationSteps
For example:
- 0.1 time step × 100 steps = 10 time units of simulation
- 0.01 time step × 1000 steps = 10 time units with 10× more precision
Train(Tensor<T>, Tensor<T>)
Trains the spiking neural network on input-output pairs.
public override void Train(Tensor<T> input, Tensor<T> expectedOutput)
Parameters
inputTensor<T>The input tensor for training.
expectedOutputTensor<T>The expected output tensor.
Remarks
This method trains the spiking neural network using spike-timing-dependent plasticity (STDP), a biologically inspired learning rule that adjusts synaptic strengths based on the relative timing of spikes between neurons. The training process simulates the network dynamics, compares the output with the expected output, and updates the weights accordingly.
For Beginners: This method teaches the network to recognize patterns in your data.
The training process works like this:
- Run the network simulation (similar to the Predict method)
- Record the timing of spikes in each layer
- Compare the output spikes with what was expected
- Adjust connection weights using a learning rule called STDP:
- Strengthen connections between neurons that spike in close sequence
- Weaken connections between neurons that don't coordinate their spiking
This learning rule is inspired by how real brains learn - neurons that fire together wire together. The timing of spikes is crucial to this process, which is a key difference from traditional neural networks.
UpdateParameters(Vector<T>)
Updates the parameters of the spiking neural network layers.
public override void UpdateParameters(Vector<T> parameters)
Parameters
parametersVector<T>The vector of parameter updates to apply.