static void Main(string[] args) { double[][] inputs = { new double[] { 0, 0 }, new double[] { 0, 1 }, new double[] { 1, 0 }, new double[] { 1, 1 } }; double[] targets = { 0, 1, 1, 1 }; double error = 1; int epoch = 0; ActivationFunction function = new SigmoidFunction(); Neuron neuron = new Neuron(2, function); LearningStrategy learning = new DeltaRuleLearning(neuron, 0.75); while (error > 0.01) { error = learning.RunEpoch(inputs, targets); epoch++; Console.WriteLine("Iteration {0} error: {1}", epoch, error); } Console.WriteLine("Training complete after {0} epochs using the Delta Rule learning regime.", epoch); Console.WriteLine("Testing"); neuron.Update(new double[] { 0, 0 }); Console.WriteLine("{0}", neuron.Output); neuron.Update(new double[] { 0, 1 }); Console.WriteLine("{0}", neuron.Output); neuron.Update(new double[] { 1, 0 }); Console.WriteLine("{0}", neuron.Output); neuron.Update(new double[] { 1, 1 }); Console.WriteLine("{0}", neuron.Output); }
// Worker thread void SearchSolution() { var reducedNetwork = ((this.classesCount == 2) && (this.useOneNeuronForTwoClasses)); // prepare learning data var input = new double[this.samples][]; var output = new double[this.samples][]; for (var i = 0; i < this.samples; i++) { input[i] = new double[this.variables]; output[i] = new double[this.neuronsCount]; // set input for (var j = 0; j < this.variables; j++) { input[i][j] = this.data[i, j]; } // set output if (reducedNetwork) { output[i][0] = this.classes[i]; } else { output[i][this.classes[i]] = 1; } } // create perceptron var network = new ActivationNetwork( new SigmoidFunction(this.sigmoidAlphaValue), this.variables, this.neuronsCount); var layer = network[0]; // create teacher var teacher = new DeltaRuleLearning(network); // set learning rate teacher.LearningRate = this.learningRate; // iterations var iteration = 1; // statistic files StreamWriter errorsFile = null; StreamWriter weightsFile = null; try { // check if we need to save statistics to files if (this.saveStatisticsToFiles) { // open files errorsFile = File.CreateText("errors.csv"); weightsFile = File.CreateText("weights.csv"); } // erros list var errorsList = new ArrayList(); // loop while (!this.needToStop) { // save current weights if (weightsFile != null) { for (var i = 0; i < this.neuronsCount; i++) { weightsFile.Write("neuron" + i + ";"); for (var j = 0; j < this.variables; j++) { weightsFile.Write(layer[i][j] + ";"); } weightsFile.WriteLine(layer[i].Threshold); } } // run epoch of learning procedure var error = teacher.RunEpoch(input, output) / this.samples; errorsList.Add(error); // save current error if (errorsFile != null) { errorsFile.WriteLine(error); } // show current iteration & error this.currentIterationBox.Text = iteration.ToString(); this.currentErrorBox.Text = error.ToString(); iteration++; // check if we need to stop if ((this.useErrorLimit) && (error <= this.learningErrorLimit)) { break; } if ((!this.useErrorLimit) && (this.iterationLimit != 0) && (iteration > this.iterationLimit)) { break; } } // show perceptron's weights this.weightsList.Items.Clear(); for (var i = 0; i < this.neuronsCount; i++) { var neuronName = string.Format("Neuron {0}", i + 1); ListViewItem item = null; // add all weights for (var j = 0; j < this.variables; j++) { item = this.weightsList.Items.Add(neuronName); item.SubItems.Add(string.Format("Weight {0}", j + 1)); item.SubItems.Add(layer[i][0].ToString("F6")); } // threshold item = this.weightsList.Items.Add(neuronName); item.SubItems.Add("Threshold"); item.SubItems.Add(layer[i].Threshold.ToString("F6")); } // show error's dynamics var errors = new double[errorsList.Count, 2]; for (int i = 0, n = errorsList.Count; i < n; i++) { errors[i, 0] = i; errors[i, 1] = (double)errorsList[i]; } this.errorChart.RangeX = new DoubleRange(0, errorsList.Count - 1); this.errorChart.UpdateDataSeries("error", errors); } catch (IOException) { MessageBox.Show("Failed writing file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // close files if (errorsFile != null) { errorsFile.Close(); } if (weightsFile != null) { weightsFile.Close(); } } // enable settings controls EnableControls(true); }
// Worker thread void SearchSolution() { bool reducedNetwork = ((classesCount == 2) && (useOneNeuronForTwoClasses)); // prepare learning data double[][] input = new double[samples][]; double[][] output = new double[samples][]; for (int i = 0; i < samples; i++) { input[i] = new double[variables]; output[i] = new double[neuronsCount]; // set input for (int j = 0; j < variables; j++) { input[i][j] = data[i, j]; } // set output if (reducedNetwork) { output[i][0] = classes[i]; } else { output[i][classes[i]] = 1; } } // create perceptron ActivationNetwork network = new ActivationNetwork( new SigmoidFunction(sigmoidAlphaValue), variables, neuronsCount); ActivationLayer layer = network.Layers[0] as ActivationLayer; // create teacher DeltaRuleLearning teacher = new DeltaRuleLearning(network); // set learning rate teacher.LearningRate = learningRate; // iterations int iteration = 1; // statistic files StreamWriter errorsFile = null; StreamWriter weightsFile = null; try { // check if we need to save statistics to files if (saveStatisticsToFiles) { // open files errorsFile = File.CreateText("errors.csv"); weightsFile = File.CreateText("weights.csv"); } // erros list ArrayList errorsList = new ArrayList(); // loop while (!needToStop) { // save current weights if (weightsFile != null) { for (int i = 0; i < neuronsCount; i++) { weightsFile.Write("neuron" + i + ","); for (int j = 0; j < variables; j++) { weightsFile.Write(layer.Neurons[i].Weights[j] + ","); } weightsFile.WriteLine(((ActivationNeuron)layer.Neurons[i]).Threshold); } } // run epoch of learning procedure double error = teacher.RunEpoch(input, output) / samples; errorsList.Add(error); // save current error if (errorsFile != null) { errorsFile.WriteLine(error); } // show current iteration & error SetText(currentIterationBox, iteration.ToString()); SetText(currentErrorBox, error.ToString()); iteration++; // check if we need to stop if ((useErrorLimit) && (error <= learningErrorLimit)) { break; } if ((!useErrorLimit) && (iterationLimit != 0) && (iteration > iterationLimit)) { break; } } // show perceptron's weights ClearList(weightsList); for (int i = 0; i < neuronsCount; i++) { string neuronName = string.Format("Neuron {0}", i + 1); ListViewItem item = null; // add all weights for (int j = 0; j < variables; j++) { item = AddListItem(weightsList, neuronName); AddListSubitem(item, string.Format("Weight {0}", j + 1)); AddListSubitem(item, layer.Neurons[i].Weights[0].ToString("F6")); } // threshold item = AddListItem(weightsList, neuronName); AddListSubitem(item, "Threshold"); AddListSubitem(item, ((ActivationNeuron)layer.Neurons[i]).Threshold.ToString("F6")); } // show error's dynamics double[,] errors = new double[errorsList.Count, 2]; for (int i = 0, n = errorsList.Count; i < n; i++) { errors[i, 0] = i; errors[i, 1] = (double)errorsList[i]; } errorChart.RangeX = new Range(0, errorsList.Count - 1); errorChart.UpdateDataSeries("error", errors); } catch (IOException) { MessageBox.Show("Failed writing file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // close files if (errorsFile != null) { errorsFile.Close(); } if (weightsFile != null) { weightsFile.Close(); } } // enable settings controls EnableControls(true); }
void backgroundWorkerTrainer_DoWork(object sender, DoWorkEventArgs e) { while (work) { double error = 0; // run epoch of learning procedure if (selected_algorythm == 0) { error = teacher.RunEpoch(input, output); listPointsOne.Add((double)watch1.ElapsedMilliseconds, error); } if (selected_algorythm == 1) { error = reprop.RunEpoch(input, output); listPointsOne.Add((double)watch1.ElapsedMilliseconds, error); } if (selected_algorythm == 2) { error = evteacher.RunEpoch(input, output); listPointsOne.Add((double)watch1.ElapsedMilliseconds, error); } if (selected_algorythm == 3) { error = lbteacher.RunEpoch(input, output); listPointsOne.Add((double)watch1.ElapsedMilliseconds, error); } if (selected_algorythm == 4) { error = delta.RunEpoch(input, output); listPointsOne.Add((double)watch1.ElapsedMilliseconds, error); } if (selected_algorythm == 5) { error = perceptron.RunEpoch(input, output); listPointsOne.Add((double)watch1.ElapsedMilliseconds, error); } // check error value to see if we need to stop // ... var c = error * 10; var d = c.ToString(); //gerrror.Add(Convert.ToInt32(c)); SetText(error.ToString()); chartdata.Add(Convert.ToSingle(c)); axisChangeZedGraph(zedGraphControl1); Thread.Sleep(2); if (logger) { SetTextLogger(error.ToString()); } if (watch1.IsRunning) { SetTextTime(watch1.ElapsedMilliseconds.ToString()); } Thread.Sleep(sleeptime); } }
static void Main(string[] args) { //this below 3 lines will shuffle the data whenever i run the application List <string> lstFile = File.ReadAllLines(srFileName).ToList(); lstFile = lstFile.OrderBy(a => Guid.NewGuid()).ToList(); File.WriteAllLines(srFileName, lstFile); int irNumberOfExamples = File.ReadAllLines(srFileName).Count(); //the first array holds all of the instances double[][] input = new double[irNumberOfExamples][]; double[][] output = new double[irNumberOfExamples][]; double[][] output2 = new double[irNumberOfExamples][]; List <double> lstOutPutClasses = new List <double>(); NumberFormatInfo formatProvider = new NumberFormatInfo(); formatProvider.NumberDecimalSeparator = "."; formatProvider.NumberGroupSeparator = ","; foreach (var vrPerLine in File.ReadAllLines(srFileName)) { var vrOutPut = Convert.ToDouble(vrPerLine.Split(',').Last(), formatProvider); if (lstOutPutClasses.Contains(vrOutPut) == false) { lstOutPutClasses.Add(vrOutPut); } } int irCounter = 0; foreach (var vrPerLine in File.ReadAllLines(srFileName)) { input[irCounter] = vrPerLine.Split(',').SkipLast(1). Select(pr => Convert.ToDouble(pr.Replace("I", "0.0").Replace("M", "0.5").Replace("F", "1.0"), formatProvider)).ToArray(); output2[irCounter] = new double[] { Convert.ToDouble(vrPerLine.Split(',').Last()) }; output[irCounter] = new double[lstOutPutClasses.Count]; var vrCurrentOutClass = Convert.ToDouble(vrPerLine.Split(',').Last(), formatProvider); output[irCounter][lstOutPutClasses.IndexOf(vrCurrentOutClass)] = 1; irCounter++; } int irFinalClassCount = lstOutPutClasses.Count; double learningRate = 0.1; int irNumberOfFeatures = input[0].Length; int[] numberOfNeurons = new int[] { 100, 100, irFinalClassCount }; ThresholdFunction function = new ThresholdFunction(); ActivationNetwork network3 = new ActivationNetwork( new SigmoidFunction(2), irNumberOfFeatures, 12, // two inputs in the network irFinalClassCount); // one neuron in the second layer // create teacher BackPropagationLearning bpteacher = new BackPropagationLearning(network3); // loop bpteacher.LearningRate = 0.1; bpteacher.Momentum = 0.5; for (int i = 0; i < 200000; i++) { double error = bpteacher.RunEpoch(input, output); var vrAcc = calculateAcurracy(network3, input, output); Console.WriteLine("BackPropagationLearning -> " + i + ", Error = " + error.ToString("N2") + "\t\t accuracy: " + vrAcc); } // create activation network ActivationNetwork network = new ActivationNetwork( function, irNumberOfFeatures, irFinalClassCount); // one neuron in the second layer //double initialStep = 0.125; //double sigmoidAlphaValue = 2.0; //// create neural network //ActivationNetwork neuralNetwork = new ActivationNetwork(new SigmoidFunction(sigmoidAlphaValue), irNumberOfFeatures, 100, irFinalClassCount); //// create teacher //ParallelResilientBackpropagationLearning neuralTeacher = new ParallelResilientBackpropagationLearning(neuralNetwork); //// set learning rate and momentum //neuralTeacher.Reset(initialStep); //for (int i = 0; i < 5000; i++) //{ // double error = neuralTeacher.RunEpoch(input, output); // Console.WriteLine("Supervised -> " + i + ", Error = " + error); //} var Layers = network.Layers.Length; //int irCounter_2 = 0; //BackPropagationLearning teacher2 = new BackPropagationLearning(network); //// loop //while (true) //{ // // run epoch of learning procedure // double error = teacher2.RunEpoch(input, output); // // check error value to see if we need to stop // Console.WriteLine("current iteration: " + irCounter_2.ToString("N0") + "error rate: " + error.ToString("N3")); // // ... // irCounter_2++; //} ActivationNeuron neuron = network.Layers[0].Neurons[0] as ActivationNeuron; // create teacher DeltaRuleLearning teacher = new DeltaRuleLearning(network); // set learning rate teacher.LearningRate = learningRate; // iterations int iteration = 1; // statistic files StreamWriter errorsFile = null; StreamWriter weightsFile = null; // check if we need to save statistics to files if (saveStatisticsToFiles) { // open files errorsFile = File.CreateText("errors.csv"); weightsFile = File.CreateText("weights.csv"); weightsFile.AutoFlush = true; } // erros list ArrayList errorsList = new ArrayList(); // loop while (!needToStop) { // save current weights if (weightsFile != null) { for (int i = 0; i < irNumberOfFeatures; i++) { weightsFile.Write(neuron.Weights[i] + ","); } weightsFile.WriteLine(neuron.Threshold); } // run epoch of learning procedure double error = teacher.RunEpoch(input, output); errorsList.Add(error); // show current iteration Console.WriteLine("current iteration: " + iteration.ToString("N0") + "error rate: " + error.ToString("N3")); // save current error if (errorsFile != null) { errorsFile.WriteLine(error); } // stop if no error if (error == 0) { break; } iteration++; } Console.ReadLine(); }