public NetworkContainer CreateNetworkContainer() { NetworkContainer neuralNetwork = null; int[] hiddenLayers = new int[cbHiddenLayerNumber.SelectedIndex]; switch (hiddenLayers.Length) { case 0: break; case 1: hiddenLayers[0] = (int)this.nHidden1.Value; break; case 2: hiddenLayers[1] = (int)this.nHidden2.Value; goto case 1; case 3: hiddenLayers[2] = (int)this.nHidden3.Value; goto case 2; case 4: hiddenLayers[3] = (int)this.nHidden4.Value; goto case 3; default: break; } IActivationFunction activationFunction = null; if (this.rbBipolarSigmoid.Checked) { activationFunction = new BipolarSigmoidFunction((double)numSigmoidAlpha.Value); } else if (this.rbSigmoid.Checked) { activationFunction = new SigmoidFunction((double)numSigmoidAlpha.Value); } else if (this.rbThreshold.Checked) { activationFunction = new ThresholdFunction(); } neuralNetwork = new NetworkContainer( tbNetworkName.Text, m_networkSchema, activationFunction, hiddenLayers); // neuralNetwork.Schema.DataRanges.ActivationFunctionRange = new AForge.DoubleRange((double)numRangeLow.Value, (double)numRangeHigh.Value); return neuralNetwork; }
private static void network(double[][] inputs, int[] outputs) { // Since we would like to learn binary outputs in the form // [-1,+1], we can use a bipolar sigmoid activation function IActivationFunction function = new BipolarSigmoidFunction(); // In our problem, we have 2 inputs (x, y pairs), and we will // be creating a network with 5 hidden neurons and 1 output: // var network = new ActivationNetwork(function, inputsCount: 2, neuronsCount: new[] { 5, 1 }); // Create a Levenberg-Marquardt algorithm var teacher = new LevenbergMarquardtLearning(network) { UseRegularization = true }; // Because the network is expecting multiple outputs, // we have to convert our single variable into arrays // var y = outputs.ToDouble().ToArray(); // Iterate until stop criteria is met double error = double.PositiveInfinity; double previous; do { previous = error; // Compute one learning iteration error = teacher.RunEpoch(inputs, y); } while (Math.Abs(previous - error) < 1e-10 * previous); // Classify the samples using the model int[] answers = inputs.Apply(network.Compute).GetColumn(0).Apply(System.Math.Sign); // Plot the results ScatterplotBox.Show("Expected results", inputs, outputs); ScatterplotBox.Show("Network results", inputs, answers) .Hold(); }
/// <summary> /// Background worker for neural network learning /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorkerNeural_DoWork(object sender, DoWorkEventArgs e) { string connectionString = "Data Source=192.168.0.245;Initial Catalog=MyDB;Integrated Security=True; Connection Timeout=30000";// + using (SqlConnection connection = new SqlConnection(connectionString)) { try { connection.Open(); string queryString = "SELECT * FROM rates WHERE [i1]<>0 AND [i2]<>0 AND [i3]<>0 AND [i4]<>0 AND [i5]<>0 AND [i6]<>0 AND [i7]<>0 AND [i8]<>0" + " AND [i9]<>0 AND [i10]<>0 AND [i11]<>0 AND [i12]<>0 AND [i13]<>0 AND [i14]<>0 AND [i15]<>0 AND [i16]<>0" + " AND [i17]<>0 AND [i18]<>0 AND [i20]<>0 AND [i21]<>0 AND [i23]<>0"; SqlCommand command = new SqlCommand(queryString, connection); SqlDataReader reader = command.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(reader); int value_count = dt.Rows.Count; double[][] input_arr = new double[value_count][]; double[][] output_arr = new double[value_count][]; for (int j = 0; j < value_count; j++) { input_arr[j] = new double[21]; output_arr[j] = new double[1]; } // Call Read before accessing data. for (int i = 0; i < value_count; i++) { DataRow row = dt.Rows[i]; input_arr[i][0] = Double.Parse(row["i1"].ToString()); input_arr[i][1] = Double.Parse(row["i2"].ToString()); input_arr[i][2] = Double.Parse(row["i3"].ToString()); input_arr[i][3] = Double.Parse(row["i4"].ToString()); input_arr[i][4] = Double.Parse(row["i5"].ToString()); input_arr[i][5] = Double.Parse(row["i6"].ToString()); input_arr[i][6] = Double.Parse(row["i7"].ToString()); input_arr[i][7] = Double.Parse(row["i8"].ToString()); input_arr[i][8] = Double.Parse(row["i9"].ToString()); input_arr[i][9] = Double.Parse(row["i10"].ToString()); input_arr[i][10] = Double.Parse(row["i11"].ToString()); input_arr[i][11] = Double.Parse(row["i12"].ToString()); input_arr[i][12] = Double.Parse(row["i13"].ToString()); input_arr[i][13] = Double.Parse(row["i14"].ToString()); input_arr[i][14] = Double.Parse(row["i15"].ToString()); input_arr[i][15] = Double.Parse(row["i16"].ToString()); input_arr[i][16] = Double.Parse(row["i17"].ToString()); input_arr[i][17] = Double.Parse(row["i18"].ToString()); input_arr[i][18] = Double.Parse(row["i20"].ToString()); input_arr[i][19] = Double.Parse(row["i21"].ToString()); input_arr[i][20] = Double.Parse(row["i23"].ToString()); //output_arr[i][0] = Double.Parse(row["Difference"].ToString()); if (Double.Parse(row["Difference"].ToString()) > 0) output_arr[i][0] = 1; else if (Double.Parse(row["Difference"].ToString()) == 0) output_arr[i][0] = 0; else output_arr[i][0] = -1; } int[] neurons = new int[5] { 21, 21, 21, 21, 1 }; AForge.Neuro.BipolarSigmoidFunction sigmoiddFunction = new AForge.Neuro.BipolarSigmoidFunction(); //AForge.Neuro.SigmoidFunction sigmoiddFunction = new AForge.Neuro.SigmoidFunction(2); AForge.Neuro.ActivationNetwork network = new AForge.Neuro.ActivationNetwork(sigmoiddFunction, 21, 1); AForge.Neuro.ActivationNetwork network1 = new AForge.Neuro.ActivationNetwork(sigmoiddFunction, 21, 1);//neurons); //AForge.Neuro.Learning.DeltaRuleLearning teacher = new AForge.Neuro.Learning.DeltaRuleLearning(network) { LearningRate = 1}; AForge.Neuro.Learning.EvolutionaryLearning teacher = new AForge.Neuro.Learning.EvolutionaryLearning(network, 1000); // AForge.Neuro.Learning.ResilientBackpropagationLearning teacher = new AForge.Neuro.Learning.ResilientBackpropagationLearning(network) { LearningRate = 1 }; //AForge.Neuro.Learning.PerceptronLearning teacherP = new PerceptronLearning(network1){ LearningRate =1}; //AForge.Neuro.Learning.BackPropagationLearning teacher = new AForge.Neuro.Learning.BackPropagationLearning(network) { LearningRate =1, Momentum = .2 }; // loop bool noNeedToStop = false; double error = 0; //double error1 = 0; double lastError = 0; double learningRate = 1; int k = 0; sigmoiddFunction.Alpha = 0.01; while (!noNeedToStop) { // run epoch of learning procedure //error = teacher.RunEpoch(input_arr, output_arr); //error = teacherP.RunEpoch(input_arr,output_arr); error = teacher.RunEpoch(input_arr, output_arr); double temp = Math.Abs(lastError - error); if (error < 30) noNeedToStop = true; else if (temp < 0.0000001) { lastError = error; k++; if (k > 1000) { network.Randomize(); k = 0; } learningRate /= 2; //if (learningRate < 0.001) // { // learningRate = 0.001; //network.Randomize(); // noNeedToStop = true; // } } else lastError = error; // teacherP.LearningRate = learningRate; } network.Save(@"E:\\neural"); } catch (Exception ex) { message += " Exception: " + ex.Message; } finally { connection.Close(); } } }
private static NeuralNetworkEvaluator evaluateSingleLayerActivationNetworkWithSigmoidFunctionBackPropagationLearning( double[][] input, double[][] output, double[][] crossValidationInput, char[] crossValidationDataLabels, double[][] evaluationInput, char[] evaluationDataLabels, double learningRate, string networkName) { //Create the neural Network BipolarSigmoidFunction sigmoidFunction = new BipolarSigmoidFunction(2.0f); ActivationNetwork neuralNet = new ActivationNetwork(sigmoidFunction, input[0].Length, ClassifierHelpers.NUM_CHAR_CLASSES); //Randomise the networks initial weights neuralNet.Randomize(); //Create teacher that the network will use to learn the data (Back Propogation Learning technique used here) BackPropagationLearning teacher = new BackPropagationLearning(neuralNet); teacher.LearningRate = LEARNING_RATE; //Train the Network //trainNetwork(neuralNet, teacher, input, output, crossValidationInput, crossValidationDataLabels); //Train multiple networks, pick the one that performs best on the Cross-Validation data neuralNet = trainNetworksCompeteOnCrossValidation(neuralNet, teacher, input, output, crossValidationInput, crossValidationDataLabels); //Evaluate the network returned on the cross-validation data so it can be compared to the current best NeuralNetworkEvaluator crossValEvaluator = new NeuralNetworkEvaluator(neuralNet); crossValEvaluator.Evaluate(crossValidationInput, crossValidationDataLabels); //See if this network is better than the current best network of it's type //Try and load a previous network of this type string previousNetworkPath = Program.NEURAL_NETWORKS_PATH + networkName + Program.NEURAL_NETWORK_FILE_EXTENSION; string networkCMPath = Program.NEURAL_NETWORKS_PATH + networkName + ".csv"; bool newBest = false; ActivationNetwork bestNetwork = neuralNet; if(File.Exists(previousNetworkPath)) { //Load the previous network & evaluate it ActivationNetwork previous = ActivationNetwork.Load(previousNetworkPath) as ActivationNetwork; NeuralNetworkEvaluator prevCrossValEval = new NeuralNetworkEvaluator(previous); prevCrossValEval.Evaluate(crossValidationInput, crossValidationDataLabels); //If this network is better than the previous best, write it out as the new best if(prevCrossValEval.ConfusionMatrix.NumMisclassifications > crossValEvaluator.ConfusionMatrix.NumMisclassifications) { DefaultLog.Info("New best cross-validation score for network \"{0}\". Previous was {1}/{2}, new best is {3}/{2}", networkName, prevCrossValEval.ConfusionMatrix.NumMisclassifications, prevCrossValEval.ConfusionMatrix.TotalClassifications, crossValEvaluator.ConfusionMatrix.NumMisclassifications); //Delete the old files File.Delete(previousNetworkPath); File.Delete(networkCMPath); newBest = true; } else //The previous network is still the best { DefaultLog.Info("Existing \"{0}\" network performed better than new one. New network scored {1}/{2}, existing scored {3}/{2}", networkName, crossValEvaluator.ConfusionMatrix.NumMisclassifications, crossValEvaluator.ConfusionMatrix.TotalClassifications, prevCrossValEval.ConfusionMatrix.NumMisclassifications); bestNetwork = previous; } } else //Otherwise there isn't a previous best { DefaultLog.Info("No previous best record for network \"{0}\" . . .", networkName); newBest = true; } //Evaluate the best system on the evaluation data NeuralNetworkEvaluator evaluator = new NeuralNetworkEvaluator(bestNetwork); evaluator.Evaluate(evaluationInput, evaluationDataLabels); //If there is a new best to write out if(newBest) { DefaultLog.Info("Writing out net best network of type\"{0}\"", networkName); neuralNet.Save(previousNetworkPath); //Write out the Confusion Matrix for the evaluation data, not cross-validation evaluator.ConfusionMatrix.WriteToCsv(networkCMPath); DefaultLog.Info("Finished writing out network \"{0}\"", networkName); } return evaluator; }