Exemplo n.º 1
0
        /// <summary>
        /// Construct a simulated annleaing trainer for a feedforward neural network.
        /// </summary>
        /// <param name="network">   The neural network to be trained. </param>
        /// <param name="startTemp"> The starting temperature. </param>
        /// <param name="stopTemp">  The ending temperature. </param>
        /// <param name="cycles">    The number of cycles in a training iteration. </param>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET:
//ORIGINAL LINE: public SimulatedAnnealingLearning(final org.neuroph.core.NeuralNetwork network, final double startTemp, final double stopTemp, final int cycles)
        public SimulatedAnnealingLearning(NeuralNetwork network, double startTemp, double stopTemp, int cycles)
        {
            NeuralNetwork         = network;
            this.temperature      = startTemp;
            this.startTemperature = startTemp;
            this.stopTemperature  = stopTemp;
            this.cycles           = cycles;

            this.weights     = new double[NeuralNetworkCODEC.determineArraySize(network)];
            this.bestWeights = new double[NeuralNetworkCODEC.determineArraySize(network)];

            NeuralNetworkCODEC.network2array(network, this.weights);
            NeuralNetworkCODEC.network2array(network, this.bestWeights);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Randomize the weights and thresholds. This function does most of the work
        /// of the class. Each call to this class will randomize the data according
        /// to the current temperature. The higher the temperature the more
        /// randomness. </summary>
        /// <param name="randomChance">  </param>
        public virtual void randomize(double randomChance)
        {
            for (int i = 0; i < this.weights.Length; i++)
            {
                if (new Random(1).NextDouble() < randomChance)
                {
                    double add = 0.5 - (new Random(2).NextDouble());
                    add            /= this.startTemperature;
                    add            *= this.temperature;
                    this.weights[i] = this.weights[i] + add;
                }
            }

            NeuralNetworkCODEC.array2network(this.weights, Network);
        }
Exemplo n.º 3
0
        public virtual void doLearningEpoch(DataSet trainingSet, double randomChance)
        {
            Array.Copy(this.weights, 0, this.bestWeights, 0, this.weights.Length);

            double bestError = determineError(trainingSet);

            this.temperature = this.startTemperature;

            for (int i = 0; i < this.cycles; i++)
            {
                randomize(randomChance);
                double currentError = determineError(trainingSet);

                if (currentError < bestError)
                {
                    Array.Copy(this.weights, 0, this.bestWeights, 0, this.weights.Length);
                    bestError = currentError;
                }
                else
                {
                    Array.Copy(this.bestWeights, 0, this.weights, 0, this.weights.Length);
                }

                NeuralNetworkCODEC.array2network(this.bestWeights, Network);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double ratio = Math.exp(Math.log(this.stopTemperature / this.startTemperature) / (this.cycles - 1));
                double ratio = Math.Exp(Math.Log(this.stopTemperature / this.startTemperature) / (this.cycles - 1));
                this.temperature *= ratio;
            }

            // the following line is probably wrong (when is reset() called?), but the result might not be used for anything
            this.previousEpochError = ErrorFunction.TotalError;

            // moved stopping condition to separate method hasReachedStopCondition()
            // so it can be overriden / customized in subclasses
            if (hasReachedStopCondition())
            {
                stopLearning();
            }
        }