Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
State Machine
Browse files Browse the repository at this point in the history
  • Loading branch information
okozelsk committed May 1, 2018
1 parent a2791fd commit c2a83a8
Show file tree
Hide file tree
Showing 25 changed files with 720 additions and 770 deletions.
306 changes: 185 additions & 121 deletions Demo/DemoConsoleApp/DemoSettings.xml

Large diffs are not rendered by default.

27 changes: 25 additions & 2 deletions Demo/DemoConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using RCNet.DemoConsoleApp.Log;

using RCNet.Extensions;
using RCNet.MathTools;
using RCNet.Neural.Activation;
using RCNet.Neural.Network.SM;
Expand All @@ -12,13 +13,16 @@ class Program
{
static void Main(string[] args)
{
LIF lif = new LIF(0.95, 15, -70, -68, -62, 0);
/*
Random rand = new Random();
LIF lif = new LIF(15, 0.05, 5, 20, 1);
for(int i = 0; i < 500; i++)
{
double signal = 0;
if (i < 100)
{
signal = lif.Compute(0.1);
double input = rand.NextDouble(0, 1, false, RandomClassExtensions.DistributionType.Uniform) * rand.NextDouble(0, 1, false, RandomClassExtensions.DistributionType.Uniform);
signal = lif.Compute(input);
}
else
{
Expand All @@ -27,6 +31,25 @@ static void Main(string[] args)
Console.WriteLine($"State {lif.InternalState} signal {signal}");
}
Console.ReadLine();
*/
Normalizer nrm = new Normalizer(new Interval(-1, 1), 0, true);
for (int i = 1; i <= 8; i++)
{
nrm.Adjust(i);
}
SignalConverter sg = new SignalConverter(new Interval(-1, 1), 3);
for(int i = 1; i <= 8; i++)
{
double normI = nrm.Normalize(i);
sg.EncodeAnalogValue(normI);
Console.WriteLine(i + ":");
for (int j = 0; j < sg.NumOfCodingFractions; j++)
{
Console.WriteLine(sg.FetchSpike());
}
Console.WriteLine("---");
}
Console.ReadLine();



Expand Down
23 changes: 18 additions & 5 deletions Demo/DemoConsoleApp/SMDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ private static void ReportReservoirsStatistics(List<ReservoirStat> statisticsCol
{
log.Write($" Statistics of pool instance: {poolStat.PoolInstanceName}", false);
log.Write($" Neurons states", false);
log.Write(" MAX Avg, Max, Min, SDdev: " + poolStat.NeuronsMaxStatesStat.ArithAvg.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMaxStatesStat.Max.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMaxStatesStat.Min.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMaxStatesStat.StdDev.ToString("N4", CultureInfo.InvariantCulture), false);
log.Write(" AVG Avg, Max, Min, SDdev: " + poolStat.NeuronsAvgStatesStat.ArithAvg.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsAvgStatesStat.Max.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsAvgStatesStat.Min.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsAvgStatesStat.StdDev.ToString("N4", CultureInfo.InvariantCulture), false);
log.Write(" MAX Avg, Max, Min, SDdev: " + poolStat.NeuronsMaxStatesStat.ArithAvg.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMaxStatesStat.Max.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMaxStatesStat.Min.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMaxStatesStat.StdDev.ToString("N4", CultureInfo.InvariantCulture), false);
log.Write(" SPAN Avg, Max, Min, SDdev: " + poolStat.NeuronsStateSpansStat.ArithAvg.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsStateSpansStat.Max.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsStateSpansStat.Min.ToString("N4", CultureInfo.InvariantCulture) + ", "
Expand All @@ -82,10 +82,23 @@ private static void ReportReservoirsStatistics(List<ReservoirStat> statisticsCol
+ poolStat.NeuronsStimuliSpansStat.Min.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsStimuliSpansStat.StdDev.ToString("N4", CultureInfo.InvariantCulture), false);
log.Write($" Neurons transmission signals", false);
log.Write(" ArithAvg Avg, Max, Min, SDdev: " + poolStat.NeuronsAvgTransmissionSignalStat.ArithAvg.ToString("N4", CultureInfo.InvariantCulture) + ", "
log.Write(" AVG Avg, Max, Min, SDdev: " + poolStat.NeuronsAvgTransmissionSignalStat.ArithAvg.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsAvgTransmissionSignalStat.Max.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsAvgTransmissionSignalStat.Min.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsAvgTransmissionSignalStat.StdDev.ToString("N4", CultureInfo.InvariantCulture), false);
log.Write(" MAX Avg, Max, Min, SDdev: " + poolStat.NeuronsMaxTransmissionSignalStat.ArithAvg.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMaxTransmissionSignalStat.Max.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMaxTransmissionSignalStat.Min.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMaxTransmissionSignalStat.StdDev.ToString("N4", CultureInfo.InvariantCulture), false);
log.Write(" MIN Avg, Max, Min, SDdev: " + poolStat.NeuronsMinTransmissionSignalStat.ArithAvg.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMinTransmissionSignalStat.Max.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMinTransmissionSignalStat.Min.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsMinTransmissionSignalStat.StdDev.ToString("N4", CultureInfo.InvariantCulture), false);
log.Write($" Neurons transmission frequencies", false);
log.Write(" AVG Avg, Max, Min, SDdev: " + poolStat.NeuronsAvgTransmissionFreqStat.ArithAvg.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsAvgTransmissionFreqStat.Max.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsAvgTransmissionFreqStat.Min.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.NeuronsAvgTransmissionFreqStat.StdDev.ToString("N4", CultureInfo.InvariantCulture), false);
log.Write($" Weights statistics", false);
log.Write(" Input Avg, Max, Min, SDdev: " + poolStat.InputWeightsStat.ArithAvg.ToString("N4", CultureInfo.InvariantCulture) + ", "
+ poolStat.InputWeightsStat.Max.ToString("N4", CultureInfo.InvariantCulture) + ", "
Expand Down
5 changes: 5 additions & 0 deletions RCNet/MathTools/Interval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ public bool BelongsTo(double value, IntervalType intervalType = IntervalType.Lef
return false;
}

/// <summary>
/// Rescales given value to this interval
/// </summary>
/// <param name="value">Input value</param>
/// <param name="valueRange">Input value range</param>
public double Rescale(double value, Interval valueRange)
{
return _min + (((value - valueRange._min) / valueRange.Span) * Span);
Expand Down
44 changes: 9 additions & 35 deletions RCNet/Neural/Activation/ActivationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ public enum Function
/// </summary>
Elliot,
/// <summary>
/// Exponential Integrate and Fire
/// </summary>
ExpIF,
/// <summary>
/// Analog "Gaussian" activation function
/// </summary>
Gaussian,
Expand All @@ -38,16 +34,16 @@ public enum Function
/// </summary>
ISRU,
/// <summary>
/// Leaky Integrate and Fire
/// </summary>
LIF,
/// <summary>
/// Analog "Leaky ReLU" activation function.
/// ReLU function is a specific case of this LeakyReLU having negSlope = 0.
/// Identity function is a specific case of this LeakyReLU having negSlope = 1.
/// </summary>
LeakyReLU,
/// <summary>
/// Leaky integrate and fire
/// </summary>
LIF,
/// <summary>
/// Analog "Sigmoid" activation function
/// </summary>
Sigmoid,
Expand Down Expand Up @@ -105,16 +101,6 @@ public static IActivationFunction Create(ActivationSettings settings, Random ran
return new BentIdentity();
case Function.Elliot:
return new Elliot((double.IsNaN(settings.Arg1) ? 1 : settings.Arg1));
case Function.ExpIF:
return new ExpIF(double.IsNaN(settings.Arg1) ? 12 : settings.Arg1, //membraneTimeScale (ms)
double.IsNaN(settings.Arg2) ? 20 : settings.Arg2, //membraneResistance (MOhm)
double.IsNaN(settings.Arg3) ? -65 : settings.Arg3, //restV (mV)
double.IsNaN(settings.Arg4) ? -60 : settings.Arg4, //resetV (mV)
double.IsNaN(settings.Arg5) ? -55 : settings.Arg5, //rheobaseV (mV)
double.IsNaN(settings.Arg6) ? -30 : settings.Arg6, //firingTresholdV (mV)
double.IsNaN(settings.Arg7) ? 2 : settings.Arg7, //sharpnessDeltaT (ms)
double.IsNaN(settings.Arg8) ? 0 : settings.Arg8 //refractoryPeriods (ms)
);
case Function.Gaussian:
return new Gaussian();
case Function.Identity:
Expand All @@ -123,23 +109,12 @@ public static IActivationFunction Create(ActivationSettings settings, Random ran
return new ISRU((double.IsNaN(settings.Arg1) ? 1 : settings.Arg1));
case Function.LeakyReLU:
return new LeakyReLU((double.IsNaN(settings.Arg1) ? 0.01 : settings.Arg1));
/* Original
case Function.LIF:
return new LIF(double.IsNaN(settings.Arg1) ? 8 : settings.Arg1, //membraneTimeScale (ms)
double.IsNaN(settings.Arg2) ? 10 : settings.Arg2, //membraneResistance (MOhm)
double.IsNaN(settings.Arg3) ? -70 : settings.Arg3, //restV (mV)
double.IsNaN(settings.Arg4) ? -65 : settings.Arg4, //resetV (mV)
double.IsNaN(settings.Arg5) ? -50 : settings.Arg5, //firingTresholdV (mV)
double.IsNaN(settings.Arg6) ? 2 : settings.Arg6 //refractoryPeriods (ms)
);
*/
case Function.LIF:
return new LIF(double.IsNaN(settings.Arg1) ? 8 : settings.Arg1, //membraneTimeScale (ms)
double.IsNaN(settings.Arg2) ? 10 : settings.Arg2, //membraneResistance (MOhm)
double.IsNaN(settings.Arg3) ? -70 : settings.Arg3, //restV (mV)
double.IsNaN(settings.Arg4) ? -65 : settings.Arg4, //resetV (mV)
double.IsNaN(settings.Arg5) ? -50 : settings.Arg5, //firingTresholdV (mV)
double.IsNaN(settings.Arg6) ? 0 : settings.Arg6 //refractoryPeriods (ms)
return new LIF(double.IsNaN(settings.Arg1) ? 15 : settings.Arg1, //membraneResistance (MOhm)
double.IsNaN(settings.Arg2) ? 0.05 : settings.Arg2, //membrane decay rate
double.IsNaN(settings.Arg3) ? 5 : settings.Arg3, //resetV (mV)
double.IsNaN(settings.Arg4) ? 20 : settings.Arg4, //firingTresholdV (mV)
double.IsNaN(settings.Arg5) ? 0 : settings.Arg5 //refractoryPeriods (ms)
);
case Function.Sigmoid:
return new Sigmoid();
Expand Down Expand Up @@ -168,7 +143,6 @@ public static Function ParseActivationFunctionType(string code)
{
case "BENTIDENTITY": return Function.BentIdentity;
case "ELLIOT": return Function.Elliot;
case "EXPIF": return Function.ExpIF;
case "GAUSSIAN": return Function.Gaussian;
case "IDENTITY": return Function.Identity;
case "ISRU": return Function.ISRU;
Expand Down
3 changes: 3 additions & 0 deletions RCNet/Neural/Activation/ExpIF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace RCNet.Neural.Activation
{
/// <summary>
/// Exponential Integrate and Fire neuron model.
/// </summary>
public class ExpIF : IActivationFunction
{
//Attributes
Expand Down
3 changes: 3 additions & 0 deletions RCNet/Neural/Activation/IActivationFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace RCNet.Neural.Activation
{
/// <summary>
/// Each neuron activation function has to implement this interface
/// </summary>
public interface IActivationFunction
{
//Properties
Expand Down
68 changes: 23 additions & 45 deletions RCNet/Neural/Activation/LIF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
namespace RCNet.Neural.Activation
{
/// <summary>
/// Leaky integrate and fire
/// Implements Leaky Integrate and Fire neuron model
/// </summary>
public class LIF : IActivationFunction
{
//Constants
private const double SpikeCurrent = 1;
private double SpikeCurrent = 1d;

//Attributes
//Static working ranges
private static readonly Interval _inputRange = new Interval(double.NegativeInfinity.Bound(), double.PositiveInfinity.Bound());
private static readonly Interval _outputRange = new Interval(0, 1);

//Parameters
private Interval _stateRange;
private double _retainmentRate;
private double _membraneResistance;
private double _membraneDecayRate;
private double _restV;
private double _resetV;
private double _firingTresholdV;
Expand All @@ -38,28 +38,25 @@ public class LIF : IActivationFunction
/// <summary>
/// Constructs an initialized instance
/// </summary>
/// <param name="retainmentRate"></param>
/// <param name="membraneResistance">(MOhm)</param>
/// <param name="restV">(mV)</param>
/// <param name="resetV">(mV)</param>
/// <param name="firingTresholdV">(mV)</param>
/// <param name="refractoryPeriods">(ms)</param>
public LIF(double retainmentRate,
double membraneResistance,
double restV,
double resetV,
double firingTresholdV,
/// <param name="membraneResistance">Membrane resisatance in MOhm.</param>
/// <param name="membraneDecayRate">Membrane potential decay</param>
/// <param name="absResetV">Reset voltage in mV (positive value)</param>
/// <param name="absFiringTresholdV">Firing treshold voltage in mV (positive value)</param>
/// <param name="refractoryPeriods">Number of after spike computation cycles, when is ignored input stimuli.</param>
public LIF(double membraneResistance,
double membraneDecayRate,
double absResetV,
double absFiringTresholdV,
double refractoryPeriods
)
{
_retainmentRate = retainmentRate;
_membraneResistance = membraneResistance;
_restV = restV;
_resetV = resetV;
_firingTresholdV = firingTresholdV;
_refractoryPeriods = (int)refractoryPeriods;
_membraneDecayRate = membraneDecayRate;
_restV = 0;
_resetV = Math.Abs(absResetV);
_firingTresholdV = Math.Abs(absFiringTresholdV);
_refractoryPeriods = (int)Math.Round(refractoryPeriods, 0);
_stateRange = new Interval(_restV, _firingTresholdV);
//_membraneResistance = (_firingTresholdV - _resetV) / SpikeCurrent;
Reset();
return;
}
Expand Down Expand Up @@ -119,7 +116,7 @@ public void Reset()
public double Compute(double x)
{
x = x.Bound();
double output = 0;
double spike = 0;
if (_membraneV >= _firingTresholdV)
{
//Membrane potential after spike
Expand All @@ -141,34 +138,15 @@ public double Compute(double x)
x = 0;
}
}
//Compute membrane potential
_membraneV = _restV + (_membraneV - _restV) * (_retainmentRate) + _membraneResistance * x;
//Compute membrane new potential
_membraneV = _restV + (_membraneV - _restV) * (1d - _membraneDecayRate) + _membraneResistance * x;
//Output
if (_membraneV >= _firingTresholdV)
{
double spikeVoltage = (_membraneV - _resetV);
//Spike current
output = (spikeVoltage / _membraneResistance);
output = 1 / (1 + Math.Exp(-1 * output));
/*
double spikeVoltage = (_membraneV - _resetV);
//Spike current
output = (spikeVoltage / _membraneResistance);
//Membrane potential after spike
_membraneV = _resetV;
_refractoryPeriod = 0;
_inRefractory = true;
*/
//output = SpikeCurrent;
/*
double spikeVoltage = (_membraneV - _resetV);
//Spike current
output = (spikeVoltage / _membraneResistance);
output = 1;
spike = SpikeCurrent;
_membraneV = _firingTresholdV;
*/
}
return output;
return spike;
}

/// <summary>
Expand Down
Loading

0 comments on commit c2a83a8

Please sign in to comment.