Spiking Neuron Network Simulator  1.0
Simulation and training of spiking neuron networks, primarily theta neurons
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Pages
SumOfSquaredSpikingError.cs
Go to the documentation of this file.
1 namespace SpikingNeuronNetwork.Lib
2 {
3  using Interfaces;
4  using System;
5  using System.Collections.Generic;
6  using System.Linq;
7 
12  {
16  public SimulationMethod SimulationMethod { get; set; }
17 
24  public double GetError(List<Spike> desiredOutputSpikes, List<Spike> actualOutputSpikes)
25  {
26  if (desiredOutputSpikes.Count > actualOutputSpikes.Count)
27  {
28  throw new ArgumentException("Desired Spikes Count Must Not Be Greater Than The Actual Spike Count To Calculate Error");
29  }
30 
31  if (actualOutputSpikes.Count > desiredOutputSpikes.Count)
32  {
33  actualOutputSpikes = actualOutputSpikes.Take(desiredOutputSpikes.Count).ToList();
34  }
35 
36  desiredOutputSpikes = desiredOutputSpikes.OrderBy(x => x.Time).ToList();
37  desiredOutputSpikes = desiredOutputSpikes.OrderBy(x => x.NeuronIndex).ToList();
38  actualOutputSpikes = actualOutputSpikes.OrderBy(x => x.Time).ToList();
39  actualOutputSpikes = actualOutputSpikes.OrderBy(x => x.NeuronIndex).ToList();
40 
41  double error = 0;
42  for (var j = 0; j < desiredOutputSpikes.Count; j++)
43  {
44  if (desiredOutputSpikes[j].NeuronIndex != actualOutputSpikes[j].NeuronIndex)
45  {
46  throw new ArgumentException("Desired and Actual Spikes Counts On Each Neuron Must Be Equal To Calculate Error");
47  }
48  error += Math.Pow(desiredOutputSpikes[j].Time - actualOutputSpikes[j].Time, 2);
49  }
50  return error/2;
51  }
52 
59  public List<double> GetErrorToOutputSpikeTimeDerivative(List<Spike> desiredOutputSpikes, List<Spike> actualOutputSpikes)
60  {
61  if (desiredOutputSpikes.Count > actualOutputSpikes.Count)
62  {
63  throw new ArgumentException("Desired Spike Count Must Not Be Greater Than the Actual Spike Count To Calculate Error");
64  }
65 
66  if (actualOutputSpikes.Count > desiredOutputSpikes.Count)
67  {
68  actualOutputSpikes = actualOutputSpikes.Take(desiredOutputSpikes.Count).ToList();
69  }
70 
71  var errorDerivatives = new List<double>();
72  if (SimulationMethod == SimulationMethod.Numerical)
73  {
74  const double offset = 1e-4;
75  foreach (var actualOutputSpike in actualOutputSpikes)
76  {
77  actualOutputSpike.Time -= offset;
78  var error1 = GetError(desiredOutputSpikes, actualOutputSpikes);
79  actualOutputSpike.Time += 2*offset;
80  var error2 = GetError(desiredOutputSpikes, actualOutputSpikes);
81  actualOutputSpike.Time -= offset;
82  errorDerivatives.Add((error2-error1)/(2*offset));
83  }
84  }
85  else
86  {
87  desiredOutputSpikes = desiredOutputSpikes.OrderBy(x => x.Time).ToList();
88  desiredOutputSpikes = desiredOutputSpikes.OrderBy(x => x.NeuronIndex).ToList();
89  actualOutputSpikes = actualOutputSpikes.OrderBy(x => x.Time).ToList();
90  actualOutputSpikes = actualOutputSpikes.OrderBy(x => x.NeuronIndex).ToList();
91 
92  for (var j = 0; j < desiredOutputSpikes.Count; j++)
93  {
94  if (desiredOutputSpikes[j].NeuronIndex != actualOutputSpikes[j].NeuronIndex)
95  {
96  throw new ArgumentException("Desired and Actual Spikes Counts On Each Neuron Must Be Equal To Calculate Error");
97  }
98  errorDerivatives.Add(actualOutputSpikes[j].Time - desiredOutputSpikes[j].Time);
99  }
100  }
101 
102  return errorDerivatives;
103  }
104  }
105 }
double GetError(List< Spike > desiredOutputSpikes, List< Spike > actualOutputSpikes)
Get the sum of squared spiking error between the desired and actual output spike trains ...
SimulationMethod
Simulation Method Enum
List< double > GetErrorToOutputSpikeTimeDerivative(List< Spike > desiredOutputSpikes, List< Spike > actualOutputSpikes)
Get the error to output spike time derivatives