//загрузка Format Data из файла private Tuple <PatternsType, KeysType, AnswersType> FormatLoad(string filePath) { string message = "Данные загружены из "; message += filePath + "\r\n"; PatternsType patts = new PatternsType(); KeysType keys = new KeysType(); AnswersType answs = new AnswersType(); #region Read Answers int posLastSlesh = filePath.LastIndexOf('\\'); string answersPath = filePath.Substring(0, posLastSlesh); answersPath += "\\answers.txt"; answs = LoadVec.LoadFloatVecWithKey(answersPath); #endregion #region Read Patterns string[] readText = File.ReadAllLines(filePath); foreach (string s in readText) { if (s.Trim() != "") { string[] elemsStr = s.Split(new char[] { ',' }); List <float> vec = new List <float>(elemsStr.Length - 1); string key = elemsStr[0]; for (int i = 1; i < elemsStr.Length; i++) { float floatEl; bool ok = float.TryParse(elemsStr[i], out floatEl); if (ok) { vec.Add(floatEl); } else { FormConsole.PrintlnAndScroll("error try parse"); return(new Tuple <PatternsType, KeysType, AnswersType>(null, null, null)); } } patts.Add(vec); keys.Add(key); } } #endregion FormConsole.PrintlnAndScroll(message); return(new Tuple <PatternsType, KeysType, AnswersType>(patts, keys, answs)); }
//вывод в консоль выхода НН по входу public static void consoleRunNN(bool isPrintIn = true) { if (Information.inputSignals.Count == Information.inputSize + (Information.NN.IsBiasNeuron ? 1 : 0)) { List <float> outputSignals = Information.NN.Run(Information.inputSignals, Information.NN.IsBiasNeuron); FormConsole.AppendText("Run Result:\r\n"); int i = 1; if (isPrintIn) { FormConsole.AppendText("input: "); foreach (float signal in Information.inputSignals) { FormConsole.AppendText(i + ") " + signal.ToString() + " "); i++; } FormConsole.AppendText("\r\n"); } i = 1; FormConsole.AppendText("output: "); float max = float.MinValue; int maxInd = -1; foreach (float signal in outputSignals) { if (max < signal) { max = signal; maxInd = i - 1; } FormConsole.AppendText(i + ") " + Math.Round(signal, 4).ToString() + " "); i++; } FormConsole.AppendText("\r\n"); FormConsole.AppendText("Answer Pattern = " + maxInd); FormConsole.AppendText("\r\n"); FormConsole.Scroll(); } else { FormConsole.PrintlnAndScroll("Some Error In Run NN\r\n"); } }
//Начать учить НН private async void startLearnNN(object sender, EventArgs e) { int MaxEp; if (int.TryParse(textBoxLearnRate.Text, out MaxEp)) { Information.NN.MaxEp = MaxEp; } int DistPrint; if (int.TryParse(textBoxMoment.Text, out DistPrint)) { Information.NN.DistPrint = DistPrint; } if (Information.pattNames.Count == Information.answNames.Count && Information.pattNames.Count != 0) { string[] fileNames1 = Information.pattNames.ToArray(); LoadFromFile lF1 = new LoadFromFile(fileNames1); string[] fileNames2 = Information.answNames.ToArray(); LoadFromFile lF2 = new LoadFromFile(fileNames2); List <List <float> > pattList = lF1.ConvertIntoList(); List <List <float> > answList = lF2.ConvertIntoList(); Information.NN.Learning(pattList.Count, pattList, answList); } else if (Information.isMnistLearn && Information.answNames.Count == 10) { ReadMnist readMnist = new ReadMnist(true, false); //File.WriteAllLines("mnist_data.txt", Information.mnistStrs); string[] fileNames2 = Information.answNames.ToArray(); LoadFromFile lF2 = new LoadFromFile(fileNames2); List <List <float> > answList = lF2.ConvertIntoList(); Information.NN.LearningMnist(readMnist, answList); } else if (Information.isFormatLearn) { if (Information.NN != null) { openFileDialog1.FileName = "Выбор файла NN"; openFileDialog1.Filter = "*(*.txt)|*.txt"; openFileDialog1.InitialDirectory = Directory.GetCurrentDirectory() + @"\formatData"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { string filePath = openFileDialog1.FileName; Information.FormatFilePath = filePath; Tuple <PatternsType, KeysType, AnswersType> data = FormatLoad(filePath); PatternsType pattList = data.Item1; KeysType keys = data.Item2; AnswersType answList = data.Item3; await Information.NN.LearningFormat(pattList, keys, answList); } else { FormConsole.PrintlnAndScroll("erroe open file dialog"); } } else { FormConsole.PrintlnAndScroll("erroe NN is not define"); } } else { printErrorMessage("Неверно заданы образцы и ответы"); } }