Exemplo n.º 1
0
 //Methods
 /// <summary>
 /// Resets the neuron to its initial state
 /// </summary>
 /// <param name="statistics">Specifies whether to reset also internal statistics</param>
 public void Reset(bool statistics)
 {
     _activation.Reset();
     _tStimuli        = 0;
     _rStimuli        = 0;
     OutputSignal     = _activation.Compute(_tStimuli);
     OutputSignalLeak = 0;
     if (statistics)
     {
         Statistics.Reset();
     }
     return;
 }
Exemplo n.º 2
0
        private double FindCurrent(IActivationFunction af, double targetResponse, double tolerance)
        {
            double lo = -100, hi = 100;

            while (true)
            {
                af.Reset();
                double current  = (lo + (hi - lo) / 2);
                double response = af.Compute(current);
                if (af.TypeOfActivation == Neural.Activation.ActivationType.Spiking)
                {
                    response = af.InternalState;
                }
                Console.CursorLeft = 0;
                Console.Write($"lo {lo}, hi {hi}, current {current}, response {response}".PadRight(150, ' '));
                if (Math.Abs(response - targetResponse) <= tolerance)
                {
                    Console.WriteLine();
                    return(current);
                }
                if (response > targetResponse)
                {
                    hi = current;
                }
                else if (response < targetResponse)
                {
                    lo = current;
                }
            }
        }
Exemplo n.º 3
0
        public virtual float Compute(List <float> inputs)
        {
            Debug.Assert(Weights != null);

            lastNet    = SumOfProduct(inputs);
            lastOutput = activationFunction.Compute(lastNet);
            return(lastOutput);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Computes neuron's new output signal and updates statistics
        /// </summary>
        /// <param name="collectStatistics">Specifies whether to update internal statistics</param>
        public void Recompute(bool collectStatistics)
        {
            //Spike leak handling
            if (OutputData._spikingSignal > 0)
            {
                //Spike during previous cycle, so reset the counter
                OutputData._afterFirstSpike = true;
                OutputData._spikeLeak       = 0;
            }
            ++OutputData._spikeLeak;

            double normalizedActivation;

            if (_activation.TypeOfActivation == ActivationType.Spiking)
            {
                //Spiking activation
                OutputData._spikingSignal = _activation.Compute(_tStimuli);
                //OutputData._analogSignal = OutputData._spikingSignal;
                _activationState         = _activation.InternalState;
                normalizedActivation     = _outputRange.Rescale(_activation.InternalState, _activation.InternalStateRange).Bound(_outputRange.Min, _outputRange.Max);
                OutputData._analogSignal = normalizedActivation;
            }
            else
            {
                //Analog activation
                double newState = _activation.Compute(_tStimuli);
                _activationState     = (_analogRetainmentStrength * _activationState) + (1d - _analogRetainmentStrength) * newState;
                normalizedActivation = _outputRange.Rescale(_activationState, _activation.OutputRange).Bound(_outputRange.Min, _outputRange.Max);
                bool firingEvent = _histActivationsQueue == null ? ((normalizedActivation - OutputData._analogSignal) > _analogFiringThreshold) : (_histActivationsQueue.Full ? (normalizedActivation - _histActivationsQueue.Dequeue()) > _analogFiringThreshold : (normalizedActivation - 0.5d) > _analogFiringThreshold);
                _histActivationsQueue?.Enqueue(normalizedActivation);
                //New output data
                OutputData._analogSignal  = normalizedActivation;
                OutputData._spikingSignal = firingEvent ? 1d : 0d;
            }
            //Update predictors
            _predictors?.Update(_activationState, normalizedActivation, (OutputData._spikingSignal > 0));
            //Update statistics
            if (collectStatistics)
            {
                Statistics.Update(_iStimuli, _rStimuli, _tStimuli, _activationState, OutputData._analogSignal, OutputData._spikingSignal);
            }
            return;
        }
Exemplo n.º 5
0
        private void TestActivation(IActivationFunction af, int simLength, double constCurrent, int from, int count)
        {
            Random rand = new Random();

            for (int i = 1; i <= simLength; i++)
            {
                double signal = 0;
                if (i >= from && i < from + count)
                {
                    double input = double.IsNaN(constCurrent) ? rand.NextDouble(0, 1, false, RandomClassExtensions.DistributionType.Uniform) : constCurrent;
                    signal = af.Compute(input);
                }
                else
                {
                    signal = af.Compute(0);
                }
                Console.WriteLine($"{i}, State {af.InternalState} signal {signal}");
            }
            Console.ReadLine();

            return;
        }
Exemplo n.º 6
0
        private void TestActivation(IActivationFunction af, int simLength, double constCurrent, int from, int count)
        {
            Random rand = new Random();

            for (int i = 1; i <= simLength; i++)
            {
                double signal;
                if (i >= from && i < from + count)
                {
                    double input = double.IsNaN(constCurrent) ? rand.NextDouble() : constCurrent;
                    signal = af.Compute(input);
                }
                else
                {
                    signal = af.Compute(0);
                }
                Console.WriteLine($"{i}, State {(af.TypeOfActivation == Neural.Activation.ActivationType.Spiking ? af.InternalState : signal)} signal {signal}");
            }
            Console.ReadLine();

            return;
        }
Exemplo n.º 7
0
        public double Compute(double[] input)
        {
            double output = 0;

            for (int i = 0; i < input.Length; i++)
            {
                output += m_weights[i] * input[i];
            }
            output  += m_weights[input.Length] * m_threshold;
            output   = m_function.Compute(output);
            m_output = output;
            return(output);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Computes neuron's new output signal and updates statistics
        /// </summary>
        /// <param name="collectStatistics">Specifies whether to update internal statistics</param>
        public void ComputeSignal(bool collectStatistics)
        {
            //Spike leak handling
            if (_spikingSignal > 0)
            {
                //Spike during previous cycle, so reset the counter
                AfterFirstSpike = true;
                SpikeLeak       = 0;
            }
            ++SpikeLeak;

            if (_activation.TypeOfActivation == ActivationType.Spiking)
            {
                //Spiking activation
                _spikingSignal   = _activation.Compute(_tStimuli);
                _activationState = _activation.InternalState;
                _analogSignal    = _spikingSignal;
            }
            else
            {
                //Analog activation
                double prevActivationState = _activationState;
                double newState            = _activation.Compute(_tStimuli);
                _activationState = (_retainmentStrength * _activationState) + (1d - _retainmentStrength) * newState;
                _analogSignal    = _outputRange.Rescale(_activationState, _activation.OutputRange);
                bool firingEvent = (_activationState - prevActivationState) > _analogFiringThreshold;
                _spikingSignal = firingEvent ? 1d : 0d;
            }
            //Update predictors
            _predictors?.Update(_activationState, (_spikingSignal > 0));
            //Update statistics
            if (collectStatistics)
            {
                Statistics.Update(_iStimuli, _rStimuli, _tStimuli, _activationState, _analogSignal, _spikingSignal);
            }
            return;
        }
Exemplo n.º 9
0
        public Matrix Compute()
        {
            if (IsInputLayer)
            {
                return(Weights);
            }
            var inputMatrix = _input.Compute();

            for (var row = 0; row < ActivationsMatrix.Rows; row++)
            {
                var neuronWeights = Weights.GetRow(row);
                WeightedInputMatrix[row, 0] = Matrix.DotProduct(inputMatrix, neuronWeights) + Bias[row, 0];
                var activated = _activation.Compute(WeightedInputMatrix[row, 0]);
                ActivationsMatrix[row, 0] = activated;
            }
            return(ActivationsMatrix);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Computes neuron's new output signal and updates statistics
 /// </summary>
 /// <param name="collectStatistics">Specifies whether to update internal statistics</param>
 public void NewState(bool collectStatistics)
 {
     //Output signal leak handling
     if (OutputSignal > 0)
     {
         //Spike during previous cycle, so reset the counter
         OutputSignalLeak = 0;
     }
     ++OutputSignalLeak;
     //New output signal
     OutputSignal = _activation.Compute(_tStimuli);
     _firingRate.Update(OutputSignal > 0);
     if (collectStatistics)
     {
         Statistics.Update(_tStimuli, _rStimuli, _activation.InternalState, OutputSignal);
     }
     return;
 }
Exemplo n.º 11
0
 /// <summary>
 /// Computes the neuron.
 /// </summary>
 /// <param name="stimuli">Input stimulation</param>
 /// <param name="collectStatistics">Specifies whether to update internal statistics</param>
 public void Compute(double stimuli, bool collectStatistics)
 {
     stimuli = (stimuli + Bias).Bound();
     if (collectStatistics)
     {
         StimuliStat.AddSampleValue(stimuli);
     }
     //State and signal
     _signal = (_retainmentRatio * _state) + (1d - _retainmentRatio) * _activation.Compute(stimuli);
     _state  = _signal;
     //Compute rescaled state
     _rescaledState = _rescalledStateRange.Rescale(_state, _activation.InternalStateRange);
     //Statistics
     if (collectStatistics)
     {
         StatesStat.AddSampleValue(_rescaledState);
     }
     return;
 }
Exemplo n.º 12
0
 /// <summary>
 /// Computes the neuron.
 /// </summary>
 /// <param name="stimuli">Input stimulation</param>
 /// <param name="collectStatistics">Specifies whether to update internal statistics</param>
 public void Compute(double stimuli, bool collectStatistics)
 {
     stimuli = (stimuli + Bias).Bound();
     if (collectStatistics)
     {
         StimuliStat.AddSampleValue(stimuli);
     }
     //State and spike
     _spike = _activation.Compute(stimuli);
     _state = _activation.InternalState;
     _firingRate.Update(_spike > 0);
     //Compute rescaled state
     _rescaledState = _rescalledStateRange.Rescale(_state, _activation.InternalStateRange);
     //Statistics
     if (collectStatistics)
     {
         StatesStat.AddSampleValue(_rescaledState);
     }
     return;
 }
Exemplo n.º 13
0
        //Methods
        private void TestActivation(IActivationFunction af, int simLength, double constCurrent, int from, int count)
        {
            for (int i = 1; i <= simLength; i++)
            {
                double signal;
                double input;
                if (i >= from && i < from + count)
                {
                    input = double.IsNaN(constCurrent) ? _rand.NextDouble() : constCurrent;
                }
                else
                {
                    input = 0d;
                }
                signal = af.Compute(input);
                Console.WriteLine($"{af.GetType().Name} step {i}, State {(af.TypeOfActivation == ActivationType.Spiking ? af.InternalState : signal)} signal {signal}");
            }
            Console.ReadLine();

            return;
        }