Exemple #1
0
 //
 public NCommittee(DataSet pat, DataSet tar, params string[] netFiles)
 {
     try
     {
         networks = new BackPropNet[netFiles.Length];
         //
         for(int i=0; i<netFiles.Length; i++)
             networks[i] = BackPropNet.BinaryLoad(netFiles[i]);
         //
         this.SetData(pat, tar);
         this.CheckNetworks();
         //
         // allocate memory
         alpha = new double[networks.Length];
         outputs = new Double[patterns.GetNumberOfVectors()];
         errors = new double[networks.Length, patterns.GetNumberOfVectors()];
     }
     catch(System.Exception e)
     {
         Console.WriteLine(e.Message);
         networks = null;
         patterns = null;
         targets  = null;
         errors   = null;
         alpha    = null;
         throw;
     }
 }
        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;
            }
        }
 //
 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;
     }
 }
Exemple #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();
 }
Exemple #5
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;
     }
 }
Exemple #6
0
 //
 public void Simulate(string sPatterns, string sTargets, string results)
 {
     DataSet patterns = new DataSet(sPatterns);
     DataSet targets  = new DataSet(sTargets);
     //
     Simulate(patterns, targets, results);
 }
Exemple #7
0
 //
 public void Simulate(string patterns, string results)
 {
     DataSet ds = new DataSet(patterns);
     //
     Simulate(ds, results);
 }
Exemple #8
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();
     }
 }
Exemple #9
0
 private void button2_Click(object sender, System.EventArgs e)
 {
     try
     {
         if(of.ShowDialog() == DialogResult.OK && of.FileName != "")
         {
             network = BackPropNet.BinaryLoad(of.FileName);
             rtb1.Text = "Network: "+network.GetStructure();
         }
         patterns = null;
         targets = null;
     }
     catch(System.Exception ex)
     {
         MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
         network = null;
     }
     //
 }
Exemple #10
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();
     }
 }
Exemple #11
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();
            }
        }
Exemple #12
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;
            }
        }
Exemple #13
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;
            }

            //
        }
Exemple #14
0
 private void button7_Click(object sender, System.EventArgs e)
 {
     try
     {
         canStart = true;
         // check training inputs and targets
         int tinputs  = new NeuralLib.DataSet(trainingInputs).GetNumberOfVectors();
         int ttargets = new NeuralLib.DataSet(trainingTargets).GetNumberOfVectors();
         //
         if(tinputs != ttargets)
         {
             canStart = false;
             MessageBox.Show("Training Inputs and Targets Differ in Size");
             trIsOk = false;
         }
         else
         {
             trIsOk = true;
             MessageBox.Show(tinputs.ToString()+" Training Inputs and Targets.", "Attention");
         }
         //
         if(validationInputs != null && validationTargets != null)
         {
             int vinputs  = new NeuralLib.DataSet(validationInputs).GetNumberOfVectors();
             int vtargets = new NeuralLib.DataSet(validationTargets).GetNumberOfVectors();
             //
             if(vinputs != vtargets)
             {
                 canStart = false;
                 MessageBox.Show("Validation Inputs and Targets Differ in Size");
                 valIsOk = false;
             }
             else
             {
                 valIsOk = true;
                 MessageBox.Show(vinputs.ToString()+" Validation Inputs & Targets. ", "Attention");
             }
         }
         //
         if(testInputs != null && testTargets != null)
         {
             int sinputs  = new NeuralLib.DataSet(testInputs).GetNumberOfVectors();
             int stargets = new NeuralLib.DataSet(testTargets).GetNumberOfVectors();
             //
             if(sinputs != stargets)
             {
                 canStart = false;
                 MessageBox.Show("Test Inputs and Targets Differ in Size");
                 testIsOk = false;
             }
             else
             {
                 testIsOk = true;
                 MessageBox.Show(sinputs.ToString()+" Test Inputs and Targets.", "Attention");
             }
         }
         //
     }
     catch(Exception ex)
     {
         MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemple #15
0
 private void button6_Click(object sender, System.EventArgs e)
 {
     string filename;
     //
     openFile.Title = "Choose Test Inputs File...";
     //
     if(openFile.ShowDialog() == DialogResult.OK)
     {
         filename = openFile.FileName;
         //
         if(openFile.FileName != " ")
         {
             this.testInputs = filename;
             //
             try
             {
                 NeuralLib.DataSet temp = new NeuralLib.DataSet(testInputs);
                 //
                 if(!temp.GetDataValidation())
                     throw new Exception("Test Inputs Data Corrupt!");
                 else if(temp.GetLengthOfVectors() != Convert.ToInt32(cbInputNeurons.SelectedItem))
                     throw new Exception("Test Inputs Vector Size NOT EQUAL to Network's Number Of Input Neurons!");
                 else
                     MessageBox.Show("Test Inputs Loaded!", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Information);
             }
             catch(System.Exception ex)
             {
                 MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 MessageBox.Show("Choose AGAIN Test Inputs!", "Warning");
                 this.testInputs = null;
             }
         }
     }
 }