示例#1
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);
        }
示例#2
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();
            }
        }