Ejemplo n.º 1
0
        /// <summary>
        ///  <p>
        /// Called to train the network.  Iterates until the terminating flag is
        /// set.  If there are no training event listeners set then there is a
        /// hard termination limit of 10000 (by default) iterations.
        /// </p>
        /// <p>
        /// The training algorithm is simple.  It presents the training examples
        /// in order, up to epochSize before ending the epoch.  At that point the
        /// error for the epoch is calculated and the EndEpochEvent is sent to all
        /// the listeners.  The weights of the network are then adjusted.  Training
        /// stops when the hard limit is reached (given no listeners) or by a listener
        /// requesting a termination.
        /// </p>
        /// </summary>
        /// <param name="network"></param>
        public void Train(Network network)
        {
            Dictionary <String, double> results;

            terminating = false;
            network.BeginTraining(this);
            epochCount = 0;

            if (errorManager.Count == 0)
            {
                errorManager.AddCalculator("MSE", new MSEErrorCalculator(trainingExamples.Count));
                defaultErrorName = "MSE";
            }

            if (StartTrainingDelegates != null)
            {
                StartTrainingDelegates(new TrainingEpochEvent(this, network));
            }
            while (!terminating)
            {
                try
                {
                    for (int i = 0; i < epochSize; i++)
                    {
                        Random  r       = new Random();
                        Example example = trainingExamples[r.Next(0, trainingExamples.Count - 1)];
                        results = network.Process(example.Inputs);
                        errorManager.AccumulateError(example.Expected, results);
                        network.SetFeedback(results, example.Expected);
                        if (EndPresentationDelegates != null)
                        {
                            EndPresentationDelegates(new TrainingEpochEvent(this, network));
                        }
                    }
                    epochCount++;

                    if (EndEpochDelegates != null)
                    {
                        EndEpochDelegates(new TrainingEpochEvent(this, network));
                    }

                    UpdateNetwork(network);

                    if (PostUpdateDelegates != null)
                    {
                        PostUpdateDelegates(new TrainingEpochEvent(this, network));
                    }

                    if ((epochEventListeners.Count == 0) && (epochCount > hardMax))
                    {
                        terminating = true;
                    }
                } catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                    throw new TrainingException("Unable to train netowrk due to exception", e);
                }
            }
            network.EndTraining();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Evaluates the network, returning the evaluation of the error.
 /// </summary>
 /// <param name="examples">The examples to evaluate</param>
 /// <param name="network">The network to evalute</param>
 /// <returns>An error report for the valuation.</returns>
 public Dictionary <String, double> Evaluate(Network network, ExampleSet examples)
 {
     ZeroErrors();
     for (int i = 0; i < examples.Count; i++)
     {
         Example example = examples[i];
         Dictionary <String, double> results = network.Process(example.Inputs);
         CalculateErrors(example.Expected, results);
     }
     return(errorValues);
 }
Ejemplo n.º 3
0
		/// <summary>
		///  <p>
		/// Called to train the network.  Iterates until the terminating flag is
		/// set.  If there are no training event listeners set then there is a
		/// hard termination limit of 10000 (by default) iterations.
		/// </p>
		/// <p>
		/// The training algorithm is simple.  It presents the training examples
		/// in order, up to epochSize before ending the epoch.  At that point the
		/// error for the epoch is calculated and the EndEpochEvent is sent to all
		/// the listeners.  The weights of the network are then adjusted.  Training
		/// stops when the hard limit is reached (given no listeners) or by a listener
		/// requesting a termination.
		/// </p>
		/// </summary>
		/// <param name="network"></param>
		public void Train(Network network)
		{
			Dictionary<String, double> results;
			terminating = false;
			network.BeginTraining(this);
			epochCount = 0;
        
			if(errorManager.Count == 0) 
			{
				errorManager.AddCalculator("MSE", new MSEErrorCalculator(trainingExamples.Count));
				defaultErrorName = "MSE";
			}

			if(StartTrainingDelegates != null) {
				StartTrainingDelegates(new TrainingEpochEvent(this, network));
			}
			while(!terminating) 
			{
				try 
				{
					for(int i = 0; i < epochSize; i++) 
					{
						Random r = new Random();
						Example example = trainingExamples[r.Next(0, trainingExamples.Count - 1)];
						results = network.Process(example.Inputs);
						errorManager.AccumulateError(example.Expected, results);
						network.SetFeedback(results, example.Expected);
						if(EndPresentationDelegates != null) {
							EndPresentationDelegates(new TrainingEpochEvent(this, network));
						}
					}
					epochCount++;

					if(EndEpochDelegates != null) {
						EndEpochDelegates(new TrainingEpochEvent(this, network));
					}

					UpdateNetwork(network);
					
					if(PostUpdateDelegates != null) {
						PostUpdateDelegates(new TrainingEpochEvent(this, network));
					}

					if((epochEventListeners.Count == 0) && (epochCount > hardMax)) 
					{
						terminating = true;
					}
				} catch(Exception e) 
				{
					Console.WriteLine(e.StackTrace);
					throw new TrainingException("Unable to train netowrk due to exception", e);
				}
			}
			network.EndTraining();
		}
Ejemplo n.º 4
0
		/// <summary>
		/// Evaluates the network, returning the evaluation of the error.
		/// </summary>
		/// <param name="examples">The examples to evaluate</param>
		/// <param name="network">The network to evalute</param>
		/// <returns>An error report for the valuation.</returns>
		public Dictionary<String, double> Evaluate(Network network, ExampleSet examples) 
		{
			ZeroErrors();
			for(int i = 0; i < examples.Count; i++) 
			{
				Example example = examples[i];
				Dictionary<String, double> results = network.Process(example.Inputs);
				CalculateErrors(example.Expected, results);
			}
			return errorValues;
		}