static bool LoadANN(string annfilePath, DataFormater dfr) { bool finalresult = false; if (Equals(dfr, null)) { Console.WriteLine("No data !!!"); } var ann = new NeuralNetworkEngineEO(); bool loaded = ann.LoadNeuralNetwork(annfilePath); Console.WriteLine("ANN loading = {0}", loaded); if (Object.Equals(ann, null)) { Console.WriteLine("I can not open Neural network file !!!"); } var learning_out = ann.Compute(dfr.TrainingInput); var testing_out = ann.Compute(dfr.TestingInput); string filePath = string.Format("C:\\SSL\\ANN_Results_at_{0}.csv", DateTime.Now.Minute.ToString()); using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) { using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8)) { System.Text.StringBuilder strb = new System.Text.StringBuilder(); strb.AppendLine("Learning_Outputs;"); for (int i = 0; i < learning_out.Length; i++) { strb.Append(learning_out[i][0]).AppendLine(";"); } strb.AppendLine("*****************************************;"); strb.AppendLine("Testing_Outputs;"); for (int i = 0; i < testing_out.Length; i++) { strb.Append(testing_out[i][0]).AppendLine(";"); } sw.Write(strb.ToString()); sw.Flush(); sw.Close(); fs.Close(); finalresult = true; } } return(finalresult); }
private void HpsoGwoOptim_ObjectiveFunctionComputation(ref double[] positions, ref double fitnessValue) { fitnessValue = 0; AnnEo = new NeuralNetworkEngineEO(); AnnEo.LearningAlgorithm = Learning_Algorithm; //AnnEo.InputsCount = Training_Input_Count; //AnnEo.Training_Inputs = this.Obs_Training_Inputs; //AnnEo.Training_Outputs = this.Obs_Training_Outputs; //SetLearningAlgoParams(ref positions); //AnnEo.LuanchLearning(); // Compute testing error (for fitness computing) var testingOut = GetFirstColumn(AnnEo.Compute(this.Obs_Testing_Inputs)); double testingFitness = -2; if (!Equals(testingOut, null)) { testingFitness = Statistics.Compute_DeterminationCoeff_R2(testingOut, mObs_Testing_Outputs); if (Double.IsInfinity(testingFitness)) { testingFitness = -2; } } //----------------------------------------------------------------------------------------- Debug.Print("Testing R2 = {0}", testingFitness); //fitnessValue = AnnEo.FinalTeachingError; fitnessValue = ((0.01 * (AnnEo.LayersStruct.Length - 1)) + 1) * (AnnEo.FinalTeachingError + Math.Abs(1 - testingFitness)); //Objectve function without penality: //fitnessValue = AnnEo.FinalTeachingError; //Objectve function with penality : //fitnessValue = ((0.01*(AnnEo.LayersStruct.Length - 1))+1)* AnnEo.FinalTeachingError ; if (fitnessValue < BestComputedErr) { BestComputedErr = fitnessValue; BestNeuralNetwork = AnnEo; //mComputed_TrainingOutputs = DataSerie1D.Convert(AnnEo.Compute(this.Obs_Training_Inputs)); //mComputed_Testing_Outputs = AnnEo.Compute(mObs_Testing_Inputs); } }
public static NeuralNetworkEngineEO Load(string fileName) { NeuralNetworkEngineEO resultEoNet = null; try { if (File.Exists(fileName)) { FileStream fileStrem = new FileStream(fileName, FileMode.Open, FileAccess.Read); IFormatter formatter = new BinaryFormatter(); resultEoNet = (NeuralNetworkEngineEO)formatter.Deserialize(fileStrem); } } catch (Exception ex) { throw ex; } return(resultEoNet); }
static void LaunchANN(double[][] matrix, double[] vector) { //double[] vector = new double[] { 0.2, 0.2, 0.6, 0.9, 0.5}; //double[][] matrix = new double[][] //{ // new double[]{0.1, 0.1}, // new double[]{0.2, 0.0}, // new double[]{0.3, 0.3}, // new double[]{0.4, 0.5}, // new double[]{0.25, 0.25} //}; DataSerie1D annStrct = new DataSerie1D(); annStrct.Add("HL1", 4); annStrct.Add("HL2", 2); NeuralNetworkEngineEO ann = new NeuralNetworkEngineEO(matrix, vector, annStrct); ann.LearningMaxIterations = 100; ann.LearningError = 0.0001; ann.LearningAlgorithm = LearningAlgorithmEnum.LevenbergMarquardtLearning; ann.LearningAlgorithm_Params = new double[] { 0.1, 10 }; ann.ActivationFunction = ActivationFunctionEnum.BipolarSigmoidFunction; ann.ActiveFunction_Params = new double[] { 2.00 }; ann.Learn(); //-0.5440211109;-0.2720105555 var ans = ann.Compute(new double[] { 0.28, 0.29 }); foreach (var valu in ans) { Console.WriteLine("ans = {0}", Math.Round(valu, 3)); } Console.WriteLine("Final teaching err = {0}; MaxIter ={1}.", ann.FinalTeachingError, ann.FinalIterationsCount); }
/// <summary> /// The data must be standarize before learning. /// </summary> /// <param name="hidenLayerStructure"></param> public void Learn(DataSerie1D hidenLayerStructure) { // Step 0 : Check data if (CheckData() == false) { return; } _BestNeuralNetwork = new NeuralNetworkEngineEO(); // Step 1 : Standerize Data and get Input data; //------------------------------------------------ _BestNeuralNetwork.Learning_Inputs = this.LearningInputs; _BestNeuralNetwork.Learning_Outputs = ConvertToJagged(this.LearningOutputs); /// Step 2 : set ANN's structure, activation function and params, _BestNeuralNetwork.LayersStruct = GetLayersStruct(hidenLayerStructure, this.LearningInputs[0].Length, 1); _BestNeuralNetwork.ActivationFunction = DefaultActivationFunction; _BestNeuralNetwork.LearningAlgorithm_Params = DefaultActiveFunction_Params; _BestNeuralNetwork.Learn(); }
private void Optimizer_ObjectiveFunction(double[] positions, ref double fitnessValue) { neuralNet = new NeuralNetworkEngineEO(LearningInputs, LearningOutputs); neuralNet.LearningAlgorithm = this.Learning_Algorithm; SetLearningAlgoParams(ref positions); neuralNet.Learn(); double[] computedLearningOutputs = GetArray(neuralNet.Compute(LearningInputs)); double[] computedTestingOutputs = GetArray(neuralNet.Compute(TestingInputs)); // Compute statistical params for the objective function: LearningIndexScore = Statistics.Compute_RMSE(LearningOutputs, computedLearningOutputs); TestingIndexScore = Statistics.Compute_RMSE(TestingOutputs, computedTestingOutputs); // Compute correlation R for learning and testing to controle results : var Rlern = Statistics.Compute_CorrelationCoeff_R(LearningOutputs, computedLearningOutputs); var Rtest = Statistics.Compute_CorrelationCoeff_R(TestingOutputs, computedTestingOutputs); Console.WriteLine("Index (learn)= {0} | Index (test)= {1} ; Correlation : R (learn) = {2} | R (test) = {3}", LearningIndexScore, TestingIndexScore, Rlern, Rtest); //fitnessValue = Math.Pow(LearningIndexScore,2) + Math.Pow(TestingIndexScore, 2); fitnessValue = Math.Pow(10, (2 * (LearningIndexScore + TestingIndexScore))); //fitnessValue = ((0.01 * (neuralNet.LayersStruct.Length - 1)) + 1) * fitnessValue; if (fitnessValue < BestScore) { _BestScore = fitnessValue; _BestNeuralNetwork = neuralNet; _BestLearningScore = LearningIndexScore; _BestTestingScore = TestingIndexScore; } }