⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ga_trainer_xor.cs

📁 AI : Neural Network for beginners
💻 CS
📖 第 1 页 / 共 2 页
字号:
        ///  and also wires up the underlying <see cref="NeuralNetwork">
        /// NeuralNetworks</see> events, to a new GA event, such that the
        /// <see cref="NeuralNetwork">NeuralNetworks</see> event can be 
        /// propogated to the gui
        /// </summary>
        public GA_Trainer_XOR()
        {
            networks = new NeuralNetwork[POPULATION];

            //create new ANN objects, random weights applied at start
            for (int i = 0; i <= networks.GetUpperBound(0); i++)
            {
                networks[i] = new NeuralNetwork(2, 2, 1);
                networks[i].Change += new NeuralNetwork.ChangeHandler(GA_Trainer_NN_Change);
            }
        }

        #endregion
        #region Events


        

        /// <summary>
        /// Raises the GA TrainingDone event
        /// </summary>
        /// <param name="te">The TrainerEventArgs</param>
        public virtual void On_GATrainingDone(EventArgs ea)
        {
            if (GATrainingDone != null)
            {
                // Invokes the delegates. 
                GATrainingDone(this, ea);
            }
        }


        /// <summary>
        /// Raises the GA Change event
        /// </summary>
        /// <param name="te">The TrainerEventArgs</param>
        public virtual void On_GAChange(TrainerEventArgs te)
        {
            if (GAChange != null)
            {
                // Invokes the delegates. 
                GAChange(this, te);
            }
        }

        /// <summary>
        /// Raises the NeuralNetwork Change event, simply propogates
        /// original <see cref="NeuralNetwork">NeuralNetwork</see> 
        /// event up to the GUI
        /// </summary>
        /// <param name="nne">The NeuralNetworkEventArgs</param>
        public virtual void On_NNChange(NeuralNetworkEventArgs nne)
        {
            if (NNChange != null)
            {
                // Invokes the delegates. 
                NNChange(this, nne);
            }
        }
        #endregion
        #region Private Methods

        /// <summary>
        /// Evaluates a member of the population (of <see cref="NeuralNetwork">
        /// NeuralNetworks</see>
        /// </summary>
        /// <param name="popMember">The member of the population to evaluate</param>
        /// <returns>An overall error value for this population member, which is
        /// the result of applying the complete training set to the population
        /// member, with its current weight configuration</returns>
        private double evaluate(int popMember)
        {

            double error = 0.0;

            //loop through the entire training set
            for (int i = 0; i <= train_set.GetUpperBound(0); i++)
            {
                //forward these new values through network
                //forward weights through ANN
                forwardWeights(popMember, getTrainSet(i));
                double[] targetValues = getTargetValues(getTrainSet(i));
                error += networks[popMember].getError(targetValues);

            }
            //if the Error term is < acceptableNNError value we have found
            //a good configuration of weights for teh NeuralNetwork, so tell
            //GA to stop looking
            if (error < acceptableNNError)
            {
                bestConfiguration = popMember;
                foundGoodANN = true;
            }

            //return error
            return error;
        }


        /// <summary>
        /// This event is simply here to propogate the underlying 
        /// <see cref="NeuralNetwork">NeuralNetworks</see> Change
        /// event, to the gui. The gui has no visibility of the 
        /// array of <see cref="NeuralNetwork">NeuralNetworks</see>
        /// so this trainer class propogates the events from the
        /// <see cref="NeuralNetwork">NeuralNetworks</see> to the gui
        /// </summary>
        /// <param name="sender">The orginal <see cref="NeuralNetwork">NeuralNetwork</see>
        /// that changed</param>
        /// <param name="nne">The NeuralNetworkEventArgs</param>
        private void GA_Trainer_NN_Change(object sender, NeuralNetworkEventArgs nne)
        {
            On_NNChange(nne);
        }

        /// <summary>
        /// Returns the array within the 2D train_set array as the index
        /// specfied by the idx input parameter
        /// </summary>
        /// <param name="idx">The index into the 2d array to get</param>
        /// <returns>The array within the 2D train_set array as the index
        /// specfied by the idx input parameter</returns>
        private double[] getTrainSet(int idx)
        {
            //NOTE :
            //
            //If anyone can tell me how to return an array at index idx from
            //a 2D array, which is holding arrays of arrays I would like that
            //very much.
            //I thought it would be
            //double[] trainValues= (double[])train_set.GetValue(0);
            //but this didn't work, so am doing it like this

            double[] trainValues = { train_set[idx, 0], train_set[idx, 1] };
            return trainValues;
        }


        /// <summary>
        /// Forwards the weights from the input->hidden and also from
        /// the hidden->output nodes, for the trainingSet
        /// </summary>
        /// <param name="popMember">The population member</param>
        /// <param name="trainingSet">The training set to present to the 
        /// <see cref="NeuralNetwork"/>NeuralNetwork</param>
        private void forwardWeights(int popMember, double[] trainingSet)
        {
            //forward weights through ANN
            networks[popMember].pass_forward(trainingSet,getTargetValues(trainingSet));
        }

        /// <summary>
        /// Returns a double which represents the output for the
        /// current set of inputs.
        /// In the cases where the summed inputs = 1, then target
        /// should be 1.0, otherwise it should be 0.0. 
        /// This is only for the XOR problem, but this is a trainer
        /// for the XOR problem, so this is fine.
        /// </summary>
        /// <param name="currSet">The current set of inputs</param>
        /// <returns>A double which represents the output for the
        /// current set of inputs</returns>
        private double[] getTargetValues(double[] currSet)
        {
            //the current value of the training set
            double valOfSet = 0;
            double[] targs = new double[1];
            for (int i = 0; i < currSet.Length; i++)
            {
                valOfSet += currSet[i];
            }
            //in the cases where the summed inputs = 1, then target
            //should be 1.0, otherwise it should be 0.0
            targs[0] = valOfSet == 1 ? 1.0 : 0.0;
            return targs;
        }
        #endregion
    }
    #endregion
    #region TrainerEventArgs CLASS
    /// <summary>
    /// Provides the event argumets for the 
    /// <see cref="GA_Trainer_XOR">trainer</see> class
    /// </summary>
    public class TrainerEventArgs : EventArgs
    {
        #region Instance Fields
        //Instance fields
        private int trainLoop = 0;

        #endregion
        #region Public Constructor

        /// <summary>
        /// Constructs a new TrainerEventArgs object using the parameters provided
        /// </summary>
        /// <param name="trainLoop">The current training loop</param>
        public TrainerEventArgs(int trainLoop)
        {
            this.trainLoop = trainLoop;
        }
        #endregion
        #region Public Methods/Properties

        /// <summary>
        /// gets the training loop number
        /// </summary>
        public int TrainingLoop
        {
            get { return trainLoop; }
        }
        #endregion

    }
    #endregion
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -