예제 #1
0
        public double Error(NeuralNetworkConfiguration configuration, double[][] trainDataVector, double[] position)
        {
            var squaredError = 0.0;
            _configuration.NeuralNetwork.SetWeightsAndBiases(position);
            var inputVector = new double[configuration.NumberOfInputNodes];
            var expectedResult = new double[configuration.NumberOfOutputNodes];

            foreach (var vector in trainDataVector)
            {
                Array.Copy(vector, inputVector, configuration.NumberOfInputNodes);
                Array.Copy(vector, configuration.NumberOfInputNodes, expectedResult, 0, configuration.NumberOfOutputNodes);

                var output = _configuration.NeuralNetwork.ComputeOutputs(inputVector);

                for (var i = 0; i < output.Length; i++)
                {
                    squaredError += ((output[i] - expectedResult[i]) * (output[i] - expectedResult[i]));
                }
            }

            return squaredError / trainDataVector.Length;
        }
예제 #2
0
        private void UpdateParticleError(NeuralNetworkConfiguration configuration, double[][] dataVectors, Particle particle)
        {
            var newError = Error(configuration, dataVectors, particle.Properties.Position);
            particle.ParticleError.Error = newError;

            if (newError < particle.ParticleError.BestError)
            {
                var bestPosition = particle.Properties.BestPosition;
                Array.Copy(particle.Properties.Position, particle.Properties.BestPosition, bestPosition.Length);

                particle.ParticleError.BestError = newError;
            }

            if (!(newError < BestGlobalError))
                return;

            var position = particle.Properties.Position;

            Array.Copy(particle.Properties.Position, BestGlobalPosition, position.Length);
            BestGlobalError = newError;
        }
예제 #3
0
        private void StepTroughSwarm(NeuralNetworkConfiguration configuration, double[][] dataVectors)
        {
            var epoch = 0;

            var randomizedParticles = NeuralNetworkHelpers.Randomize(_swarm.Length);

            while (epoch < _configuration.MaxEpochs || BestGlobalError <= _configuration.ExitError)
            {
                if (epoch % 100 == 0)
                    Console.WriteLine("Epoch {0}. Best error = {1}", epoch, BestGlobalError);

                for (var i = 0; i < _swarm.Length; i++)
                {
                    var particle = _swarm[randomizedParticles[i]];

                    UpdatePositionAndVelocity(particle);
                    UpdateParticleError(configuration, dataVectors, particle);
                    LiveOrDie(particle, configuration, dataVectors);
                }
                epoch++;
            }
        }
예제 #4
0
        private void LiveOrDie(Particle particle, NeuralNetworkConfiguration configuration, double[][] dataVectors)
        {
            var currentLifeProbability = _random.NextDouble();

            if (!(currentLifeProbability < ParticleDeathProbability))
                return;

            var position = particle.Properties.Position;
            for (var i = 0; i < position.Length; i++)
                particle.Properties.Position[i] = (_configuration.DimensionMaximum - _configuration.DimensionMinimum) * _random.NextDouble() + _configuration.DimensionMinimum;

            particle.ParticleError.Error = Error(configuration, dataVectors, particle.Properties.Position);
            particle.ParticleError.BestError = particle.ParticleError.Error;

            Array.Copy(particle.Properties.Position, particle.Properties.BestPosition,position.Length);

            if (!(particle.ParticleError.Error < BestGlobalError))
                return;

            BestGlobalError = particle.ParticleError.Error;
            var particlePosition = particle.Properties.Position;
            Array.Copy(particle.Properties.Position, BestGlobalPosition, particlePosition.Length);
        }
예제 #5
0
        private void InitializeSwarm(NeuralNetworkConfiguration configuration, double[][] dataVectors)
        {
            for (int i = 0; i < _swarm.Length; i++)
            {
                CreateParticleForSwarm(configuration, dataVectors, i);

                var localParticleError = _swarm[i].ParticleError;

                if (localParticleError.Error < BestGlobalError)
                {
                    var particlePosition = _swarm[i].Properties;
                    BestGlobalError = localParticleError.Error;
                    Array.Copy(particlePosition.Position, BestGlobalPosition, particlePosition.Position.Length);
                }
            }
        }
예제 #6
0
        private void CreateParticleForSwarm(NeuralNetworkConfiguration configuration, double[][] dataVectors, int i)
        {
            var properties = new ParticleProperties(_vectorSize);
            var randomPosition = InitializeRandomPosition();

            var error = Error(configuration, dataVectors, randomPosition);

            var particleError = new ParticleError
            {
                Error = error,
                BestError = error
            };

            var randomVelocity = InitializeRandomVelocity();

            Array.Copy(randomPosition, properties.Position, randomPosition.Length);
            Array.Copy(randomPosition, properties.BestPosition, randomPosition.Length);
            Array.Copy(randomVelocity, properties.Velocity, randomVelocity.Length);

            _swarm[i] = new Particle(particleError, properties);
        }