public static void cc() { //整理输入输出数据 double[][] input = new double[4][]; double[][] output = new double[4][]; input[0] = new double[] { 0, 0 }; output[0] = new double[] { 0 }; input[1] = new double[] { 0, 1 }; output[1] = new double[] { 0 }; input[2] = new double[] { 1, 0 }; output[2] = new double[] { 0 }; input[3] = new double[] { 1, 1 }; output[3] = new double[] { 1 }; for (int i = 0; i < 4; i++) { Console.WriteLine("input{0}: ===> {1},{2} output{0}: ===> {3}", i, input[i][0], input[i][1], output[i][0]); } //建立网络,层数1,输入2,输出1,激励函数阈函数 ActivationNetwork network = new ActivationNetwork(new ThresholdFunction(), 2, 1); //学习方法为感知器学习算法 PerceptronLearning teacher = new PerceptronLearning(network); //定义绝对误差 double error = 1.0; Console.WriteLine(); Console.WriteLine("learning error ===> {0}", error); //输出学习速率 Console.WriteLine(); Console.WriteLine("learning rate ===> {0}", teacher.LearningRate); //迭代次数 int iterations = 0; Console.WriteLine(); while (error > 0.001) { error = teacher.RunEpoch(input, output); Console.WriteLine("learning error ===> {0}", error); iterations++; } Console.WriteLine("iterations ===> {0}", iterations); Console.WriteLine(); Console.WriteLine("sim:"); //模拟 for (int i = 0; i < 4; i++) { Console.WriteLine("input{0}: ===> {1},{2} sim{0}: ===> {3}", i, input[i][0], input[i][1], network.Compute(input[i])[0]); } }
private static void TrainNetwork(LearningData learningData, String networkPath) { ActivationNetwork network = new ActivationNetwork( UseBipolar ? (IActivationFunction) new BipolarSigmoidFunction(1) : (IActivationFunction) new SigmoidFunction(), 784, //784, 10); network.Randomize(); Int32 epochIndex = 0; //// create teacher PerceptronLearning teacher = new PerceptronLearning(network); // new BackPropagationLearning(network); //PerceptronLearning teacher = new PerceptronLearning(network);// new BackPropagationLearning(network); teacher.LearningRate = 0.1f; ////teacher.Momentum = 0.5f; Double error = Double.MaxValue; Double previousError = Double.MaxValue; Int32 counter = 100; // loop while (counter > 0) { // run epoch of learning procedure error = teacher.RunEpoch(learningData.Input, learningData.Output); if (error > previousError) { teacher.LearningRate = teacher.LearningRate * 0.5f; } Console.WriteLine(String.Format("{0} {1}", epochIndex, error)); epochIndex++; previousError = error; counter--; } network.Save(networkPath); //Double[] output = network.Compute(learningData.Input[0]); }
public void TestName() { IActivationFunction function = new BipolarSigmoidFunction(); var network = new ActivationNetwork(function, inputsCount: 2, neuronsCount: new[] { 2, 1 }); var layer = network.Layers[0] as ActivationLayer; var teacher = new PerceptronLearning(network); teacher.LearningRate = 0.1; var input = new double[4][]; input[0] = new[] { 0d, 0d }; input[1] = new[] { 0d, 1d }; input[2] = new[] { 1d, 0d }; input[3] = new[] { 1d, 1d }; var output = new double[4][]; output[0] = new[] { 0d }; output[1] = new[] { 0d }; output[2] = new[] { 0d }; output[3] = new[] { 1d }; //// Iterate until stop criteria is met double error = double.PositiveInfinity; double previous; var needToStop = false; while (!needToStop) { } do { previous = error; // // Compute one learning iteration error = teacher.RunEpoch(input, output); } while (Math.Abs(previous - error) > 0.01); int[] answers = input.Apply(network.Compute).GetColumn(0).Apply(System.Math.Sign); //https://github.com/accord-net/framework/blob/development/Samples/Neuro/Perceptron/Applications/OneLayerPerceptron.cs }
public void Treinar(double[][] dados) { double[][] output = new double[dados.Length][]; double[][] input = new double[dados.Length][]; for (int i = 0; i < dados.Length; i++) { input[i] = new double[Variaveis]; output[i] = new double[TotalNeuronios]; for (int j = 0; j < Variaveis; j++) { input[i][j] = dados[i][j]; } int classe = Convert.ToInt32(dados[i][4]) - 1; output[i][classe] = 1; } ActivationNetwork network = new ActivationNetwork(new SigmoidFunction(ValorAlpha), Variaveis, TotalNeuronios); ActivationLayer layer = network[0]; PerceptronLearning teacher = new PerceptronLearning(network); teacher.LearningRate = CamadaRate; int interacao = 1000; int count = 0; List <double> errolist = new List <double>(); while (count <= interacao) { double erro = teacher.RunEpoch(input, output) / dados.Length; errolist.Add(erro); count++; } CriaColunaFuncao(); for (int i = 0; i < TotalNeuronios; i++) { this.FuncaoNeuronio.Rows.Add("Neuronio [" + (i + 1) + "]", Convert.ToInt32(layer[i].Threshold), layer[i][0]); } }
public override ConfusionMatrix Execute() { //Create an network with one layer and one neuron in that layer var network = new ActivationNetwork(new ThresholdFunction(), 3, 1); //Bind the reference of the neuron var neuron = network.Layers[0].Neurons[0] as ActivationNeuron; //Create the Perceptron learning algorithm //Library perceptron implements a single layer linear classifier var teacher = new PerceptronLearning(network); teacher.LearningRate = 0.1; //Enrich the dimensions of the vectors, padding 1 to the end var richTraining = AlgorithmHelpers.PaddDimension(trainingSet); var richTesting = AlgorithmHelpers.PaddDimension(testSet); //Training the network until the error is small enough //or 500 hundred iterations have been computed int epochs = 0; while (true) { double error = teacher.RunEpoch(richTraining, trainingOutput);/// trainingSet.Length; ++epochs; if (error < 0.025 * trainingSet.Length || epochs == 500) { break; } } var predicted = richTesting .Select(x => neuron.Compute(x)) .Select(x => Convert.ToInt32(x)) .ToArray(); //Create a confusion matrix with the calculated parameters ConfusionMatrix cmatrix = new ConfusionMatrix(predicted, expected, POSITIVE, NEGATIVE); OnAlgorithmEnded(Enumerable.Repeat(neuron, 1), cmatrix); return(cmatrix); }
public void Teach() { // Teacher method. // Perceptron was experimentally found to give best results. var teacher = new PerceptronLearning(Network); // Value to track network's effectivness. // If value increases over time, network is overfitted - terminate learning process. var previousError = double.MaxValue; // Value tracks the amount of times error value increased. // Once it reaches a treshold, stop trainig - network is overfitted. var errorIncreaseCount = 0; // Epochs tracks the amount of cycles neural network took. // Used to prevent infinite training. var epochs = 0; Console.WriteLine("Teaching network."); // Train the neural network. // Training lasts until EITHER network starts to get overfitted with data, // or trainig has reached maximum epoch count. while (errorIncreaseCount < OverfittedErrorTreshold && epochs < EpochsMax) { var error = teacher.RunEpoch(_inputData, _outputData); if (error > previousError) { errorIncreaseCount++; } else { errorIncreaseCount = 0; } previousError = error; epochs += 1; if (epochs % 20 == 0) { Console.WriteLine($"{(double)epochs / EpochsMax * 100}%"); } } }
/// <summary> /// Machine Learning /// </summary> private void Training() { // Creating Network network = new DeepBeliefNetwork( new GaussianFunction(), // Activation function inputsCount: 4, // Input degree hiddenNeurons: new int[] { 1 }); // Output degree // Initialize the network weight with gaussian distribution new GaussianWeights(network).Randomize(); network.UpdateVisibleWeights(); // Creating DBN Learning Algorithm var teacher = new PerceptronLearning(network); // Start learning. Do it for 1000 times. for (int i = 0; i < 1000; i++) { teacher.RunEpoch(inputs, outputs); } // 重みの更新 network.UpdateVisibleWeights(); }
// Worker thread void SearchSolution() { // 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[1]; // copy input for (var j = 0; j < this.variables; j++) { input[i][j] = this.data[i, j]; } // copy output output[i][0] = this.classes[i]; } // create perceptron var network = new ActivationNetwork(new ThresholdFunction(), this.variables, 1); var neuron = network[0][0]; // create teacher var teacher = new PerceptronLearning(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.variables; i++) { weightsFile.Write(neuron[i] + ";"); } weightsFile.WriteLine(neuron.Threshold); } // run epoch of learning procedure var error = teacher.RunEpoch(input, output); errorsList.Add(error); // show current iteration this.iterationsBox.Text = iteration.ToString(); // save current error if (errorsFile != null) { errorsFile.WriteLine(error); } // show classifier in the case of 2 dimensional data if ((neuron.InputsCount == 2) && (neuron[1] != 0)) { var k = -neuron[0] / neuron[1]; var b = -neuron.Threshold / neuron[1]; var classifier = new double[2, 2] { { this.chart.RangeX.Min, this.chart.RangeX.Min * k + b }, { this.chart.RangeX.Max, this.chart.RangeX.Max * k + b } }; // update chart this.chart.UpdateDataSeries("classifier", classifier); } // stop if no error if (error == 0) { break; } iteration++; } // show perceptron's weights this.weightsList.Items.Clear(); for (var i = 0; i < this.variables; i++) { this.weightsList.Items.Add(string.Format("Weight {0}", i + 1)); this.weightsList.Items[i].SubItems.Add(neuron[i].ToString("F6")); } this.weightsList.Items.Add("Threshold"); this.weightsList.Items[this.variables].SubItems.Add(neuron.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.RangeY = new DoubleRange(0, this.samples); 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); }
private void button1_Click(object sender, EventArgs e) { // НЕЙРОСЕТЬ ActivationNeuron neuron = network[0][0]; PerceptronLearning teacher = new PerceptronLearning(network); teacher.LearningRate = 1; StreamWriter errorsFile = null; StreamWriter weightsFile = null; errorsFile = File.CreateText("errors.csv"); weightsFile = File.CreateText("weights.csv"); // iterations int iteration = 1; ArrayList errorsList = new ArrayList(); double[][] input = new double[samplesNum][]; double[][] output = new double[samplesNum][]; for (int i = 0; i < samplesNum; i++) { input[i] = new double[2]; output[i] = new double[1]; // copy input input[i][0] = inputs[i].X; input[i][1] = inputs[i].Y; // copy output output[i][0] = inputs[i].type; } while (true) { // save current weights if (weightsFile != null) { for (int i = 0; i < 2; i++) { weightsFile.Write(neuron[i] + ";"); } weightsFile.WriteLine(neuron.Threshold); } // run epoch of learning procedure double error = teacher.RunEpoch(input, output); errorsList.Add(error); // show current iteration iterationsBox.Text = iteration.ToString(); // save current error if (errorsFile != null) { errorsFile.WriteLine(error); } // show classifier in the case of 2 dimensional data if ((neuron.InputsCount == 2) && (neuron[1] != 0)) { double k = -neuron[0] / neuron[1]; double b = -neuron.Threshold / neuron[1]; } // stop if no error if (error == 0) { break; } iteration++; } // 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.AddDataSeries("error", Color.Red, AForge.Controls.Chart.SeriesType.ConnectedDots, 3, false); errorChart.RangeX = new DoubleRange(0, errorsList.Count - 1); errorChart.RangeY = new DoubleRange(0, samplesNum); errorChart.UpdateDataSeries("error", errors); if (errorsFile != null) { errorsFile.Close(); } if (weightsFile != null) { weightsFile.Close(); } }
// Worker thread void SearchSolution() { // prepare learning data double[][] input = new double[samples][]; double[][] output = new double[samples][]; for (int i = 0; i < samples; i++) { input[i] = new double[2]; output[i] = new double[classesCount]; // set input input[i][0] = data[i, 0]; input[i][1] = data[i, 1]; // set output output[i][classes[i]] = 1; } // create perceptron ActivationNetwork network = new ActivationNetwork(new ThresholdFunction(), 2, classesCount); ActivationLayer layer = network.Layers[0] as ActivationLayer; // create teacher PerceptronLearning teacher = new PerceptronLearning(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 < classesCount; i++) { weightsFile.Write("neuron" + i + ","); weightsFile.Write(layer.Neurons[i].Weights[0] + ","); weightsFile.Write(layer.Neurons[i].Weights[1] + ","); weightsFile.WriteLine(((ActivationNeuron)layer.Neurons[i]).Threshold); } } // run epoch of learning procedure double error = teacher.RunEpoch(input, output); errorsList.Add(error); // save current error if (errorsFile != null) { errorsFile.WriteLine(error); } // show current iteration SetText(iterationsBox, iteration.ToString()); // stop if no error if (error == 0) { break; } // show classifiers for (int j = 0; j < classesCount; j++) { double k = (layer.Neurons[j].Weights[1] != 0) ? (-layer.Neurons[j].Weights[0] / layer.Neurons[j].Weights[1]) : 0; double b = (layer.Neurons[j].Weights[1] != 0) ? (-((ActivationNeuron)layer.Neurons[j]).Threshold / layer.Neurons[j].Weights[1]) : 0; double[,] classifier = new double[2, 2] { { chart.RangeX.Min, chart.RangeX.Min *k + b }, { chart.RangeX.Max, chart.RangeX.Max *k + b } }; // update chart chart.UpdateDataSeries(string.Format("classifier" + j), classifier); } iteration++; } // show perceptron's weights ClearList(weightsList); for (int i = 0; i < classesCount; i++) { string neuronName = string.Format("Neuron {0}", i + 1); // weight 0 ListViewItem item = AddListItem(weightsList, neuronName); AddListSubitem(item, "Weight 1"); AddListSubitem(item, layer.Neurons[i].Weights[0].ToString("F6")); // weight 1 item = AddListItem(weightsList, neuronName); AddListSubitem(item, "Weight 2"); AddListSubitem(item, layer.Neurons[i].Weights[1].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() { Console.WriteLine("Создание нейронной сети..."); DataItem data = DataItemFactory.GetNumericData(); int inputCount = data.Input.First().Length; var neuronCount = new[] { 10 }; IActivationFunction function = new BipolarSigmoidFunction(); double learningRate = 0.1; double momentum = 0; int maxEpochNumber = 1000000; double minErrorChange = 0.000001; double minError = 0.001; var network = MultilayerNeuralNetwork.CreateNetwork(neuronCount, inputCount, function); network.Randomize(); Console.WriteLine("Создание нейронной сети завершено."); var teacher = new PerceptronLearning(network) { LearningRate = learningRate }; int epochNumber = 1; double lastError = double.MaxValue; double error; double errorChange; Console.WriteLine("Start learning..."); do { DateTime dtStart = DateTime.Now; error = teacher.RunEpoch(data.Input, data.Output) / data.Input.Length; Console.WriteLine( $"Epoch #{epochNumber} finished; " + $"current error is {error}; " + $"it takes: {(DateTime.Now - dtStart).Duration()}"); errorChange = Math.Abs(lastError - error); lastError = error; epochNumber++; }while (epochNumber < maxEpochNumber && error > minError && errorChange > minErrorChange); for (int i = 0; i < data.Input.Length; i++) { double[] outputs = network.Compute(data.Input[i]); string strIn = ""; foreach (var input in data.Output[i]) { strIn += $"{input},"; } string strOut = ""; foreach (var output in outputs) { strOut += $"{Math.Abs(output):0.00},"; } Console.WriteLine($"{i}. Expected {strIn} Actual {strOut}"); } }