public List <string[]> SelectNeuronsInLayers(string NeuroNetName, int countNeurons, string NameAF) { int IDAF = LibraryOfActivateFunctions.GetCountParametersOfAF(NameAF, LibraryOfActivateFunctions.GetterParameter.ActivateFunctionName); if (IDAF != -1) { List <string[]> ls = new List <string[]>(); connector.ConnectToDB(); SQLiteCommand cmd = new SQLiteCommand(connector.connection); int[] neurons = new int[countNeurons]; double[] valuesPars = new double[IDAF]; cmd.CommandText = "SELECT NeuronsInLayer " + "FROM NeuroNet,TASK WHERE NeuroNet.TaskID = TASK.ID AND NeuroNet.Name ='" + NeuroNetName + "'"; try { string line = Convert.ToString(cmd.ExecuteScalar()); int k = 0; int j = 0; string buf = ""; string[] row; while (j < line.Length) { row = new string[2]; if (line[j] != ' ') { buf += line[j]; } else { row[0] = Convert.ToString(k + 1); row[1] = buf; ls.Add(row); k++; buf = ""; } j++; } row = new string[2]; row[0] = Convert.ToString(k + 1); row[1] = buf; ls.Add(row); } catch (SQLiteException ex) { MessageBox.Show(ex.Message); } connector.DisconnectFromDB(); return(ls); } else { throw new Exception("No such name of Activate Function!"); } }
public NeuroNetDefinition(string[] definition) { if (definition.Length != 8) { throw new Exception("Definition is not valid"); } name = definition[0]; topologyTypeName = definition[1]; task_name = definition[2]; neurons_count = Convert.ToInt32(definition[3]); layer_count = Convert.ToInt32(definition[4]); neurons_in_layer = new int[layer_count]; int k = 0; for (int i = 0; i < layer_count; i++) { string buf = ""; while (k < definition[5].Length && definition[5][k] != ' ') { buf += definition[5][k]; k++; } k++; if (buf.Length != 0) { neurons_in_layer[i] = Convert.ToInt32(buf); } } activate_function = definition[6]; af_parameters = new double[LibraryOfActivateFunctions.GetCountParametersOfAF(activate_function, LibraryOfActivateFunctions.GetterParameter.ActivateFunctionName)]; k = 0; for (int i = 0; i < af_parameters.Length; i++) { string buf = ""; while (k < definition[7].Length && definition[7][k] != ' ') { buf += definition[7][k]; k++; } k++; if (buf.Length != 0) { af_parameters[i] = Convert.ToDouble(buf); } } }
private void RefreshDgwParametersAF() { dgwEditParametersAF.Rows.Clear(); int countParameters = LibraryOfActivateFunctions.GetCountParametersOfAF(cbActivateFunction.SelectedItem.ToString(), LibraryOfActivateFunctions.GetterParameter.ActivateFunctionName); ActivateFunction af = LibraryOfActivateFunctions.GetActivateFunction(cbActivateFunction.SelectedItem.ToString(), LibraryOfActivateFunctions.GetterParameter.ActivateFunctionName); for (int i = 0; i < countParameters; i++) { string[] row = new string[2]; row[0] = Convert.ToString(af.GetNameOfParameter(i)); row[1] = Convert.ToString(af.GetDefaultValueOfParameter(i)); dgwEditParametersAF.Rows.Add(row); dgwEditParametersAF.Rows[i].Cells[0].ReadOnly = true; } }
private void LoadNeuroNetIntoDB() { if (mode == form_destination.Creating) { try { string NameNN = tbNameNeuroNet.Text; if (NameNN == "") { throw new Exception("Имя НС не может быть пустым"); } if (dbHandler.IsNeuroNetExist(NameNN)) { throw new Exception("Такое имя НС уже используется"); } if (cbTopology.SelectedItem == null) { throw new Exception("Необходимо выбрать топологию"); } string TopologyTypeName = LibraryOfTopologies.GetTopologyTypeName(cbTopology.SelectedItem.ToString()); if (cbTask.SelectedItem == null) { throw new Exception("Необходимо выбрать задачу"); } string TaskName = cbTask.SelectedItem.ToString(); int CountNeurons = Convert.ToInt32(numNeuronsNumber.Value); int CountLayers = Convert.ToInt32(numLayersNumber.Value); string typeAF = LibraryOfActivateFunctions.GetActivateFunctionTypeName(cbActivateFunction.SelectedItem.ToString()); if (cbActivateFunction.SelectedIndex < 0) { throw new Exception("Необходимо выбрать активационную функцию"); } string NeuronsInLayers = ""; int[] NeuInL = new int[CountLayers]; string ParametersAF = ""; int sumNeurons = 0; for (int i = 0; i < CountLayers; i++) { int neurs = Convert.ToInt32(dgwEditNeuronsInLayers.Rows[i].Cells[1].Value); if (neurs <= 0) { throw new Exception("В каждом слое должен быть по крайней мере 1 нейрон"); } sumNeurons += neurs; NeuInL[i] = neurs; NeuronsInLayers += Convert.ToString(dgwEditNeuronsInLayers.Rows[i].Cells[1].Value); if (i != CountLayers - 1) { NeuronsInLayers += " "; } } if (sumNeurons != CountNeurons) { throw new Exception("Сумма нейронов по слоям должна равняться общему числу нейронов"); } for (int i = 0; i < LibraryOfActivateFunctions.GetCountParametersOfAF(typeAF, LibraryOfActivateFunctions.GetterParameter.TypeOfActivateFunctionName); i++) { double par = Convert.ToDouble(dgwEditParametersAF.Rows[i].Cells[1].Value); ParametersAF += Convert.ToString(par); if (i != LibraryOfActivateFunctions.GetCountParametersOfAF(typeAF, LibraryOfActivateFunctions.GetterParameter.TypeOfActivateFunctionName) - 1) { ParametersAF += " "; } } dbHandler.InsertNeuroNet(NameNN, TopologyTypeName, TaskName, CountNeurons, CountLayers, NeuronsInLayers, typeAF, ParametersAF); LoadingWindow loadingWindow = new LoadingWindow(); loadingWindow.MakeLoading( () => dbHandler.InsertTopologyOfNeuroNet(NameNN, TopologyTypeName, CountNeurons, CountLayers, NeuInL, loadingWindow), "Запись нейронной сети в БД"); Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else if (mode == form_destination.Editing) { try { string NameNN = tbNameNeuroNet.Text; if (NameNN == "") { throw new Exception("Имя НС не может быть пустым"); } if (cbTopology.SelectedItem == null) { throw new Exception("Необходимо выбрать топологию"); } string TopologyTypeName = LibraryOfTopologies.GetTopologyTypeName(cbTopology.SelectedItem.ToString()); string TaskName = cbTask.SelectedItem.ToString(); int CountNeurons = Convert.ToInt32(numNeuronsNumber.Value); int CountLayers = Convert.ToInt32(numLayersNumber.Value); string typeAF = LibraryOfActivateFunctions.GetActivateFunctionTypeName(cbActivateFunction.SelectedItem.ToString()); string NeuronsInLayers = ""; int[] NeuInL = new int[CountLayers]; double[] ParAF = new double[LibraryOfActivateFunctions.GetCountParametersOfAF(typeAF, LibraryOfActivateFunctions.GetterParameter.TypeOfActivateFunctionName)]; string ParametersAF = ""; int sumNeurons = 0; for (int i = 0; i < CountLayers; i++) { int neurs = Convert.ToInt32(dgwEditNeuronsInLayers.Rows[i].Cells[1].Value); if (neurs <= 0) { throw new Exception("В каждом слое должен быть по крайней мере 1 нейрон"); } sumNeurons += neurs; NeuInL[i] = neurs; NeuronsInLayers += Convert.ToString(dgwEditNeuronsInLayers.Rows[i].Cells[1].Value); if (i != CountLayers - 1) { NeuronsInLayers += " "; } } if (sumNeurons != CountNeurons) { throw new Exception("Сумма нейронов по слоям должна равняться общему числу нейронов"); } for (int i = 0; i < LibraryOfActivateFunctions.GetCountParametersOfAF(typeAF, LibraryOfActivateFunctions.GetterParameter.TypeOfActivateFunctionName); i++) { ParAF[i] = Convert.ToDouble(dgwEditParametersAF.Rows[i].Cells[1].Value); ParametersAF += Convert.ToString(dgwEditParametersAF.Rows[i].Cells[1].Value); if (i != LibraryOfActivateFunctions.GetCountParametersOfAF(typeAF, LibraryOfActivateFunctions.GetterParameter.TypeOfActivateFunctionName) - 1) { ParametersAF += " "; } } bool isContining = true; bool isNeuronsInLayersSame = false; bool isParametersAFSame = false; bool isNeedToDelete = false; if (CountLayers == definition_of_editing_net.LayerCount) { int k = 0; for (int i = 0; i < CountLayers; i++) { if (NeuInL[i] == definition_of_editing_net.NeuronsInLayer[i]) { k++; } } if (k == CountLayers) { isNeuronsInLayersSame = true; } } if (String.Compare(cbActivateFunction.SelectedItem.ToString(), definition_of_editing_net.ActivateFunction) == 0) { int k = 0; for (int i = 0; i < definition_of_editing_net.AFParameters.Length; i++) { if (ParAF[i] == definition_of_editing_net.AFParameters[i]) { k++; } } if (k == definition_of_editing_net.AFParameters.Length) { isParametersAFSame = true; } } if (String.Compare(TaskName, definition_of_editing_net.TaskName) != 0 || String.Compare(TopologyTypeName, definition_of_editing_net.TopologyName) != 0 || CountNeurons != definition_of_editing_net.NeuronsCount || isNeuronsInLayersSame == false || isParametersAFSame == false) { DialogResult res = MessageBox.Show("Данные изменения влекут удаление обученных весов сети. " + "Нейронную сеть необходимо будет обучать заново.", "Предупреждение потери данных", MessageBoxButtons.OKCancel); isNeedToDelete = true; if (res == DialogResult.Cancel) { isContining = false; } } if (isContining == true) { if (isNeedToDelete == true) { LoadingWindow loadingWindow = new LoadingWindow(); if (isNeuronsInLayersSame && (String.Compare(cbTopology.SelectedItem.ToString(), definition_of_editing_net.TopologyName) == 0)) { loadingWindow.MakeLoading( () => dbHandler.DeleteWeightsMatrix(definition_of_editing_net.Name, loadingWindow), "Удаление устаревших весовых коэффициентов"); } else { loadingWindow.MakeLoading( () => dbHandler.DeleteTopologyAndWeightsMatrix(definition_of_editing_net.Name, loadingWindow), "Удаление старой топологии нейронной сети"); loadingWindow.MakeLoading( () => dbHandler.InsertTopologyOfNeuroNet(definition_of_editing_net.Name, TopologyTypeName, CountNeurons, CountLayers, NeuInL, loadingWindow), "Запись измененной нейронной сети в БД"); } } dbHandler.UpdateNeuroNet(NameNN, definition_of_editing_net.Name, TopologyTypeName, TaskName, CountNeurons, CountLayers, NeuronsInLayers, typeAF, ParametersAF); Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }