static void AccuracyTestPerc() { Console.WriteLine("Accuracy test perc:"); int[] neurons = new int[] { 3, 4, 2 }; bool[] delays = new bool[] { false, false }; string[] afs = new string[] { "Binary Step", "Identity" }; float[][] w = new float[][] { new float[] { 1.0f / 2, 1.0f / 3, -1.0f / 5, 1, 1.0f / 2, -1.0f / 4, 1.0f / 2, -1, 1.0f / 2, -1, 1.0f / 4, 1.0f / 3 }, new float[] { 1.0f / 2, -1.0f / 3, 1.0f / 4, -1, 1, -1.0f / 2, -1.0f / 3, 1.0f / 4 } }; PerceptronManaged p = new PerceptronManaged(new PerceptronTopology(3, neurons, delays, afs)); p.SetWeights(w); var p2 = p.Copy(); float[] y = p2.Solve(new float[] { 1, 0, 1 }); float[] answer = { 5.0f / 12, 1.0f / 6 }; float EPS = 1e-5f; if ((Math.Abs(y[0] - answer[0]) > EPS) || (Math.Abs(y[1] - answer[1]) > EPS)) { Console.WriteLine("FAIL"); } else { Console.WriteLine("PASS"); } }
static void PerformanceTest() { Console.WriteLine("Performance test"); Stopwatch sw = new Stopwatch(); int N = 1000000; int layers = 7; int[] neurons = new int[] { 5, 55, 70, 100, 60, 15, 2 }; bool[] delays = new bool[] { true, true, true, true, true, false }; string af = "Logistic"; string[] afs = new string[] { af, af, af, af, af, af }; sw.Start(); PerceptronTopology topology = new PerceptronTopology(layers, neurons, delays, afs); sw.Stop(); Console.WriteLine("topology creation = " + sw.ElapsedMilliseconds); sw.Start(); PerceptronManaged perc = new PerceptronManaged(topology); perc.SetWeights(GenerateWeights(neurons, delays)); sw.Stop(); Console.WriteLine("perc creation = " + sw.ElapsedMilliseconds); float[] x = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }; sw.Start(); for (int i = 0; i < N; i++) { float[] y = perc.Solve(x); } sw.Stop(); Console.WriteLine("calc = " + sw.ElapsedMilliseconds); }
private void learnSolver() { foreach (LearningModel learningModel in LearningList) { TaskTemplate taskTemplate = (TaskTemplate)TaskTemplate.where (new Query("TaskTemplate").addTypeQuery(TypeQuery.select) .addCondition("Name", "=", learningModel.SelectedPreprocessing), typeof(TaskTemplate))[0]; Selection selection = (Selection)Selection.where (new Query("Selection").addTypeQuery(TypeQuery.select) .addCondition("TaskTemplateID", "=", taskTemplate.ID.ToString()) .addCondition("Name", "=", learningModel.SelectedSelection), typeof(Selection))[0]; int countRows = selection.RowCount; LearningScenario learningScenario = (LearningScenario)LearningScenario.where (new Query("LearningScenario").addTypeQuery(TypeQuery.select) .addCondition("Name", "=", learningModel.SelectedScenario), typeof(LearningScenario))[0]; List <Entity> selectionRows = SelectionRow.where (new Query("SelectionRow").addTypeQuery(TypeQuery.select) .addCondition("SelectionID", "=", selection.ID.ToString()), typeof(SelectionRow)); List <Entity> parameters = dms.models.Parameter.where (new Query("Parameter").addTypeQuery(TypeQuery.select) .addCondition("TaskTemplateID", "=", selection.TaskTemplateID.ToString()), typeof(dms.models.Parameter)); int stepRow = 0; float[][] inputData = new float[countRows][]; float[] outputData = new float[countRows]; for (int i = 0; i < countRows; i++) { inputData[i] = new float[parameters.Count - 1]; } int outputParam = 0; for (int i = 0; i < parameters.Count; i++) { if (((models.Parameter)parameters[i]).IsOutput == 1) { outputParam = parameters[i].ID; } } string[][] vals = Selection.valuesOfSelectionId(selection.ID); float[][] fvals = new float[selection.RowCount][]; for (int i = 0; i < selection.RowCount; i++) { fvals[i] = new float[parameters.Count]; for (int j = 0; j < parameters.Count - 1; j++) { inputData[i][j] = float.Parse(vals[i][j].Replace(".", ",")); } outputData[i] = float.Parse(vals[i][parameters.Count - 1].Replace(".", ",")); } ISolver isolver = null; if (Solver.Description is PerceptronTopology) { PerceptronTopology topology = Solver.Description as PerceptronTopology; isolver = new PerceptronManaged(topology); } else if (Solver.Description is ConvNNTopology) { ConvNNTopology topology = Solver.Description as ConvNNTopology; isolver = new ConvNNManaged(topology); } else if (Solver.Description is WardNNTopology) { WardNNTopology topology = Solver.Description as WardNNTopology; isolver = new WardNNManaged(topology); } else if (Solver.Description is KohonenNNTopology) { KohonenNNTopology topology = Solver.Description as KohonenNNTopology; isolver = new KohonenManaged(topology); } else if (Solver.Description is TreeDescription) { TreeDescription topology = Solver.Description as TreeDescription; isolver = new DecisionTree(topology); } else if (Solver.Description is TreeDescriptionC4_5) { TreeDescriptionC4_5 topology = Solver.Description as TreeDescriptionC4_5; isolver = new DecisionTreeC4_5(topology); } else { throw new EntryPointNotFoundException(); } SeparationOfDataSet s = new SeparationOfDataSet(isolver, learningScenario, inputData, outputData); LearnedSolver ls = new LearnedSolver() { SelectionID = selection.ID, LearningScenarioID = learningScenario.ID, TaskSolverID = Solver.ID, Soul = s.separationAndLearn(selection.ID, outputParam) }; ls.save(); LearningQuality lq = new LearningQuality() { LearnedSolverID = ls.ID, MistakeTrain = Convert.ToInt32(s.MistakeTrain), MistakeTest = Convert.ToInt32(s.MistakeTest), ClosingError = s.ClosingError }; lq.save(); } OnClose?.Invoke(this, null); }
private void learnSolver() { foreach (LearningModel learningModel in LearningList) { TaskTemplate taskTemplate = (TaskTemplate)TaskTemplate.where (new Query("TaskTemplate").addTypeQuery(TypeQuery.select) .addCondition("Name", "=", learningModel.SelectedPreprocessing), typeof(TaskTemplate))[0]; Selection selection = (Selection)Selection.where (new Query("Selection").addTypeQuery(TypeQuery.select) .addCondition("TaskTemplateID", "=", taskTemplate.ID.ToString()) .addCondition("Name", "=", learningModel.SelectedSelection), typeof(Selection))[0]; int countRows = selection.RowCount; LearningScenario learningScenario = (LearningScenario)LearningScenario.where (new Query("LearningScenario").addTypeQuery(TypeQuery.select) .addCondition("Name", "=", learningModel.SelectedScenario), typeof(LearningScenario))[0]; List <Entity> selectionRows = SelectionRow.where (new Query("SelectionRow").addTypeQuery(TypeQuery.select) .addCondition("SelectionID", "=", selection.ID.ToString()), typeof(SelectionRow)); List <Entity> parameters = dms.models.Parameter.where (new Query("Parameter").addTypeQuery(TypeQuery.select) .addCondition("TaskTemplateID", "=", selection.TaskTemplateID.ToString()), typeof(dms.models.Parameter)); int stepRow = 0; float[][] inputData = new float[countRows][]; float[] outputData = new float[countRows]; for (int i = 0; i < countRows; i++) { inputData[i] = new float[parameters.Count - 1]; } int outputParam = 0; foreach (Entity selRow in selectionRows) { int selectionRowId = selRow.ID; int stepParam = 0; foreach (Entity param in parameters) { int paramId = param.ID; List <Entity> value = ValueParameter.where (new Query("ValueParameter").addTypeQuery(TypeQuery.select) .addCondition("ParameterID", "=", paramId.ToString()). addCondition("SelectionRowID", "=", selectionRowId.ToString()), typeof(ValueParameter)); if (((dms.models.Parameter)param).IsOutput == 1) { outputParam = param.ID; string outputValue = ((ValueParameter)value[0]).Value; float outputFloat; if (float.TryParse(outputValue, out outputFloat)) { outputData[stepRow] = outputFloat; } } else { inputData[stepRow][stepParam] = float.Parse(((ValueParameter)value[0]).Value, CultureInfo.InvariantCulture.NumberFormat); } stepParam++; } stepRow++; } ISolver isolver = null; if (Solver.Description is PerceptronTopology) { PerceptronTopology topology = Solver.Description as PerceptronTopology; isolver = new PerceptronManaged(topology); } else if (Solver.Description is ConvNNTopology) { ConvNNTopology topology = Solver.Description as ConvNNTopology; isolver = new ConvNNManaged(topology); } else if (Solver.Description is WardNNTopology) { WardNNTopology topology = Solver.Description as WardNNTopology; isolver = new WardNNManaged(topology); } else if (Solver.Description is TreeDescription) { TreeDescription topology = Solver.Description as TreeDescription; isolver = new DecisionTree(topology); } else { throw new EntryPointNotFoundException(); } SeparationOfDataSet s = new SeparationOfDataSet(isolver, learningScenario, inputData, outputData); LearnedSolver ls = new LearnedSolver() { SelectionID = selection.ID, LearningScenarioID = learningScenario.ID, TaskSolverID = Solver.ID, Soul = s.separationAndLearn(selection.ID, outputParam) }; ls.save(); LearningQuality lq = new LearningQuality() { LearnedSolverID = ls.ID, MistakeTrain = Convert.ToInt32(s.MistakeTrain), MistakeTest = Convert.ToInt32(s.MistakeTest), ClosingError = s.ClosingError }; lq.save(); } OnClose?.Invoke(this, null); }