Esempio n. 1
0
        protected uint vPeriod = 5; // validation period.

        #endregion Fields

        #region Constructors

        //
        //
        public TrainingAlgorithm(BackPropNet net, string tPatterns, string tTargets)
        {
            uint depth = net.Depth;
            //
            try
            {
                // allocate mem for matrices of error gradients
                errGrad = new double[depth-1][,]; // allocate mem for depth-1 vectors
                //
                for(uint i=0; i<depth-1; i++)
                {
                    uint temp1 = net.GetLayer(i).GetSize();
                    uint temp2 = net.GetLayer(i+1).GetSize();
                    //
                    errGrad[i] = new double[temp1, temp2];
                }
                //
                //
                this.trPatterns = new DataSet(tPatterns);
                this.trTargets  = new DataSet(tTargets);
                this.network    = net;
                //
                if(trPatterns.GetNumberOfVectors() != trTargets.GetNumberOfVectors())
                    throw new Exception("Datasets have different number of vectors.");
                if(trPatterns.GetDataValidation() != true || trTargets.GetDataValidation() != true)
                    throw new Exception("Invalid Data");
                if(trPatterns.GetLengthOfVectors() != net.GetLayer(0).GetSize() ||
                    trTargets.GetLengthOfVectors() != net.GetLayer(net.Depth-1).GetSize())
                    throw new Exception("Incosistent Data/Inputs or Data/Outputs");

            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
Esempio n. 2
0
 //
 public void SetValidationData(string vPatterns, string vTargets)
 {
     try
     {
         this.vaPatterns = new DataSet(vPatterns);
         this.vaTargets  = new DataSet(vTargets);
         //
         if(vaPatterns.GetNumberOfVectors() != vaTargets.GetNumberOfVectors())
             throw new Exception("Datasets have different number of vectors.");
         if(vaPatterns.GetDataValidation() != true || vaTargets.GetDataValidation() != true)
             throw new Exception("Invalid Data");
         if(vaPatterns.GetLengthOfVectors() != network.GetLayer(0).GetSize() ||
             vaTargets.GetLengthOfVectors() != network.GetLayer(network.Depth-1).GetSize())
             throw new Exception("Incosistent Data/Inputs or Data/Outputs");
         this.validate = true;
     }
     catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         this.validate = false;
         throw;
     }
 }
Esempio n. 3
0
 //
 public double Simulate(DataSet patterns, DataSet targets)
 {
     double  err = 0.0;
     double  sim = 0.0;
     //
     try
     {
         if(patterns.GetLengthOfVectors() != iLayer.GetSize() || targets.GetLengthOfVectors() != oLayer.GetSize())
             throw new Exception("Input/Target Vectors Invalid with Input/Output Layer Size");
         //
         if(patterns.GetDataValidation() == true && targets.GetDataValidation() == true
             && patterns.GetNumberOfVectors() == targets.GetNumberOfVectors())
         {
             for(int i=0; i<patterns.GetNumberOfVectors(); i++)
             {
                 this.SetInputVector(patterns.GetDataVector(i));
                 this.SetTargetVector(targets.GetDataVector(i));
                 this.FeedForward();
                 this.CalculateOutputError();
                 err = this.GetInstantOutputError();
                 sim += err;
             }
             //
         }
         else
         {
             throw new Exception("Invalid Simulation Data");
         }
         //
         return sim/patterns.GetNumberOfVectors();
     }
     catch(System.Exception ex)
     {
         Console.WriteLine(ex.Message);
         throw;
     }
 }
Esempio n. 4
0
 //
 public void SetData(NeuralLib.DataSet pat, NeuralLib.DataSet tar)
 {
     patterns = pat;
     targets  = tar;
     //
     if(patterns.GetDataValidation() != true || targets.GetDataValidation() != true
         || patterns.GetNumberOfVectors() != targets.GetNumberOfVectors())
         throw new Exception("Bad Patterns/Targets or vectors differ in length");
     //
     this.CheckNetworks();
 }
Esempio n. 5
0
 //
 public void Simulate(DataSet patterns, DataSet targets, string results)
 {
     double  err = 0.0;
     double  sim = 0.0;
     //
     string filename = results;
     StreamWriter writer = new StreamWriter(filename, false);
     //
     try
     {
         if(patterns.GetLengthOfVectors() != iLayer.GetSize() || targets.GetLengthOfVectors() != oLayer.GetSize())
             throw new Exception("Input/Target Vectors Invalid with Input/Output Layer Size");
         //
         if(patterns.GetDataValidation() == true && targets.GetDataValidation() == true
             && patterns.GetNumberOfVectors() == targets.GetNumberOfVectors())
         {
             for(int i=0; i<patterns.GetNumberOfVectors(); i++)
             {
                 this.SetInputVector(patterns.GetDataVector(i));
                 this.SetTargetVector(targets.GetDataVector(i));
                 this.FeedForward();
                 this.CalculateOutputError();
                 err = this.GetInstantOutputError();
                 sim += err;
                 //
                 foreach(double d in this.oLayer.GetOutputVector())
                 {
                     writer.Write(d.ToString("F6")+" ");
                 }
                 //
                 writer.Write(err.ToString("E6"));
                 writer.Write("\n");
             }
             //
             Console.WriteLine("Average Simulation Error: {0:E6}", sim/patterns.GetNumberOfVectors());
         }
         else
         {
             writer.WriteLine("Invalid Simulation Data");
             throw new Exception("Invalid Simulation Data");
         }
         //
         writer.Flush();
         writer.Close();
     }
     catch(System.Exception ex)
     {
         Console.WriteLine(ex.Message);
         throw;
     }
     finally
     {
         writer.Close();
     }
 }
Esempio n. 6
0
 //
 public void Simulate(DataSet ds, string results)
 {
     string filename = results;
     //
     StreamWriter writer = new StreamWriter(filename, false);
     //
     try
     {
         //
         if(ds.GetLengthOfVectors() != iLayer.GetSize())
             throw new Exception("Input Vector Length differs from Input Layer's Size");
         //
         if(ds.GetDataValidation())
         {
             for(int i=0; i<ds.GetNumberOfVectors(); i++)
             {
                 this.SetInputVector(ds.GetDataVector(i));
                 this.FeedForward();
                 //
                 foreach(double d in this.oLayer.GetOutputVector())
                 {
                     writer.Write(d.ToString("F4")+" ");
                 }
                 //
                 writer.Write("\n");
             }
         }
         else
         {
             writer.WriteLine("Invalid Simulation Data");
         }
         //
         writer.Flush();
         writer.Close();
     }
     catch(System.Exception ex)
     {
         Console.WriteLine(ex.Message);
         throw;
     }
     finally
     {
         writer.Close();
     }
 }
Esempio n. 7
0
        //
        private void button5_Click(object sender, System.EventArgs e)
        {
            string filename = null;
            //
            if(sf.ShowDialog() == DialogResult.OK && of.FileName != " ")
            {
                filename = sf.FileName;
                //
                try
                {

                    if(patterns != null)
                    {
                        if(targets != null)
                            network.Simulate(patterns, targets, filename);
                        else
                            network.Simulate(patterns, filename);
                    }
                    //
                    MessageBox.Show("Simulation Finished", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch(System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    patterns = null;
                    patterns = null;
                    network  = null;
                    this.button3.Enabled = false;
                    this.button4.Enabled = false;
                    this.button5.Enabled = false;
                }
            }
            //
            if(targets != null)
            {
                NeuralLib.DataSet dset = new NeuralLib.DataSet(filename);
                //
                double[] dummy;
                //
                chart1.Series.Clear();
                chart1.OpenData(COD.Values, 0, dset.GetNumberOfVectors());
                if(network.GetLayer(network.Depth-1).GetSize() == 1)
                {
                    // network has one output neuron.
                    chart1.AxisY.Title.Text = "Real Output-Simulated Output";
                    double sse = 0.0;
                    for(int i=0; i<dset.GetNumberOfVectors(); i++)
                    {
                        dummy = dset.GetDataVector(i);
                        chart1.Value[0, i] = targets.GetDataVector(i)[0];
                        chart1.Value[1, i] = dummy[0];
                        sse += dummy[dummy.Length-1];
                    }
                    //
                    label1.Text = "SSE: "+sse.ToString("F6")+" MSE: "+(sse/dset.GetNumberOfVectors()).ToString("F6")+" RMSE: "+
                        Math.Sqrt(sse/dset.GetNumberOfVectors()).ToString("F6");
                }
                else
                {
                    chart1.AxisY.Title.Text = "RMSE";
                    double sse = 0.0;
                    for(int i=0; i<dset.GetNumberOfVectors(); i++)
                    {
                        dummy = dset.GetDataVector(i);
                        chart1.Value[0, i] = Math.Sqrt(dummy[dummy.Length-1]);
                        sse += dummy[dummy.Length-1];
                    }
                    label1.Text = "SSE: "+sse.ToString("F6")+" MSE: "+(sse/dset.GetNumberOfVectors()).ToString("F6")+" RMSE: "+
                        Math.Sqrt(sse/dset.GetNumberOfVectors()).ToString("F6");
                }
                //
                label1.Left = chart1.Left+chart1.Width/2-label1.Width/2;
                chart1.CloseData(COD.Values);
                chart1.RecalcScale();
                chart1.Refresh();
            }
        }
Esempio n. 8
0
        //
        private void button4_Click(object sender, System.EventArgs e)
        {
            try
            {

                if(of.ShowDialog() == DialogResult.OK && of.FileName != "")
                {
                    targets = new NeuralLib.DataSet(of.FileName);
                    rtb1.Text = "\nTargets: "+targets.GetNumberOfVectors().ToString()+"x"+targets.GetLengthOfVectors().ToString();
                }
            }
            catch(System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                targets = null;
            }
        }
Esempio n. 9
0
        private void button3_Click(object sender, System.EventArgs e)
        {
            try
            {
                if(of.ShowDialog() == DialogResult.OK && of.FileName != "")
                {
                    patterns = new NeuralLib.DataSet(of.FileName);
                    rtb1.Text = "\nPatterns: "+patterns.GetNumberOfVectors().ToString()+"x"+patterns.GetLengthOfVectors().ToString();
                }
                //
                button4.Enabled = true;
                button5.Enabled = true;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                patterns = null;
            }

            //
        }