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; } // }
private void bStart_Click(object sender, System.EventArgs e) { // System.IO.StreamWriter sw = new System.IO.StreamWriter(results); sw.AutoFlush = true; keepTraining = true; // int numberOfTimesToTrain = Convert.ToInt32(cbTrainTimes.SelectedItem); // uint inputSize = Convert.ToUInt32(cbInputNeurons.SelectedItem); uint outputSize = Convert.ToUInt32(cbOutputNeurons.SelectedItem); // uint minNeurons = Convert.ToUInt32(cbMinHiddenNeurons.SelectedItem); uint maxNeurons = Convert.ToUInt32(cbMaxHiddenNeurons.SelectedItem); // tbResults.Text = "Hidden Neurons ---- Mean Train Error --- Mean Validation Error ------ Mean Test Error"; uint n = minNeurons-1; while(keepTraining) { n++; if(n > maxNeurons) break; // double meanTrError = 0; double meanVaError = 0; double meanTeError = 0; // Application.DoEvents(); for(int i=0; i<numberOfTimesToTrain; i++) { nnet = new BackPropNet(inputSize, outputSize, n); // if(cbHiddenActFunction.SelectedItem.ToString() != "LogSig") { nnet.SetHiddenActivationFunction(ActivationFunction.TanSig); } // if(cbOutputActFunction.SelectedItem.ToString() == "TanSig") nnet.SetOutputActivationFunction(ActivationFunction.TanSig); else if(cbOutputActFunction.SelectedItem.ToString() == "Linear") nnet.SetOutputActivationFunction(ActivationFunction.Linear); // nnet.Randomize(-0.2, 0.2); // if(cbAlgorithm.Text == "RProp") { algorithm = new RProp(nnet, trainingInputs, trainingTargets); } else { algorithm = new SimpleBPM(nnet, trainingInputs, trainingTargets); ((SimpleBPM)algorithm).Momentum = Convert.ToDouble(this.tbMomentum.Text); ((SimpleBPM)algorithm).LearnRate = Convert.ToDouble(this.tbLearnRate.Text); } // algorithm.Epochs = Convert.ToUInt32(this.tbEpochs.Text); algorithm.MaxFailures = Convert.ToUInt32(this.tbMaxFails.Text); // if(valIsOk) algorithm.SetValidationData(validationInputs, validationTargets); // if(cbAlgorithm.Text == "RProp") { ((RProp)algorithm).Train(); } else { ((SimpleBPM)algorithm).Train(); } // double trError = algorithm.TrainingPerformance; double vaError = Double.NaN; double teError = Double.NaN; // if(valIsOk) { vaError = algorithm.ValidationPerformance; meanVaError += vaError; } // if(testIsOk) { teError = nnet.Simulate(new NeuralLib.DataSet(testInputs), new NeuralLib.DataSet(testTargets)); meanTeError += teError; } // meanTrError += trError; Application.DoEvents(); } // meanTrError = meanTrError/numberOfTimesToTrain; // if(valIsOk) meanVaError = meanVaError/numberOfTimesToTrain; // if(testIsOk) meanTeError = meanTeError/numberOfTimesToTrain; // tbResults.AppendText("\n"+"\t"+n.ToString()+"\t\t"+meanTrError.ToString("E5")+"\t\t"+ meanVaError.ToString("E6")+"\t\t"+meanTeError.ToString("E6")); // sw.WriteLine(nnet.GetStructure()+"\t\t"+meanTrError.ToString("E5")+"\t\t"+ meanVaError.ToString("E6")+"\t\t"+meanTeError.ToString("E6")); Application.DoEvents(); } // if(cbUseSecondLayer.Checked == true) { uint k = 2; while(keepTraining) { k++; if(k > n) break; // double meanTrError = 0; double meanVaError = 0; double meanTeError = 0; // Application.DoEvents(); for(int i=0; i<numberOfTimesToTrain; i++) { nnet = new BackPropNet(inputSize, outputSize, n-1, k-1); // if(cbHiddenActFunction.SelectedItem.ToString() != "LogSig") { nnet.SetHiddenActivationFunction(ActivationFunction.TanSig); } // if(cbOutputActFunction.SelectedItem.ToString() == "TanSig") nnet.SetOutputActivationFunction(ActivationFunction.TanSig); else if(cbOutputActFunction.SelectedItem.ToString() == "Linear") nnet.SetOutputActivationFunction(ActivationFunction.Linear); // nnet.Randomize(-0.2, 0.2); // if(cbAlgorithm.Text == "RProp") { algorithm = new RProp(nnet, trainingInputs, trainingTargets); } else { algorithm = new SimpleBPM(nnet, trainingInputs, trainingTargets); ((SimpleBPM)algorithm).Momentum = Convert.ToDouble(this.tbMomentum.Text); ((SimpleBPM)algorithm).LearnRate = Convert.ToDouble(this.tbLearnRate.Text); } // algorithm.Epochs = Convert.ToUInt32(this.tbEpochs.Text); algorithm.MaxFailures = Convert.ToUInt32(this.tbMaxFails.Text); // if(valIsOk) algorithm.SetValidationData(validationInputs, validationTargets); // if(cbAlgorithm.Text == "RProp") { ((RProp)algorithm).Train(); } else { ((SimpleBPM)algorithm).Train(); } // double trError = algorithm.TrainingPerformance; double vaError = Double.NaN; double teError = Double.NaN; // if(valIsOk) { vaError = algorithm.ValidationPerformance; meanVaError += vaError; } // if(testIsOk) { teError = nnet.Simulate(new NeuralLib.DataSet(testInputs), new NeuralLib.DataSet(testTargets)); meanTeError += teError; } // meanTrError += trError; Application.DoEvents(); } // meanTrError = meanTrError/numberOfTimesToTrain; // if(valIsOk) meanVaError = meanVaError/numberOfTimesToTrain; // if(testIsOk) meanTeError = meanTeError/numberOfTimesToTrain; // tbResults.AppendText("\n"+"\t"+nnet.GetStructure()+"\t\t"+meanTrError.ToString("E5")+"\t\t"+ meanVaError.ToString("E6")+"\t\t"+meanTeError.ToString("E6")); // sw.WriteLine(nnet.GetStructure()+"\t\t"+meanTrError.ToString("E5")+"\t\t"+ meanVaError.ToString("E6")+"\t\t"+meanTeError.ToString("E6")); Application.DoEvents(); } } sw.Flush(); sw.Close(); }