Exemple #1
0
 private void saveToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         if (currentProject == null)
         {
             MessageBox.Show("No network loaded.", "Invalid Operation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
             return;
         }
         this.Cursor = Cursors.WaitCursor;
         EnableControls(false, true);
         SaveScreenParametersToCurrentNetwork();
         NnProject.Save(currentProject, "");
         this.Cursor = Cursors.Default;
     }
     catch (Exception ex)
     {
         this.Cursor = Cursors.Default;
         MessageBox.Show("Error saving network - " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     finally
     {
         EnableControls(true, true);
     }
 }
Exemple #2
0
 private void loadToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         OpenFileDialog ofd = new OpenFileDialog();
         ofd.CheckFileExists = true;
         ofd.CheckPathExists = true;
         ofd.ShowDialog();
         if (ofd.FileName.Length > 0)
         {
             this.Cursor = Cursors.WaitCursor;
             EnableControls(false, true);
             currentProject = NnProject.Load(ofd.FileName);
             this.Text      = currentProject.SaveFolder + @"\" + currentProject.ProjectName;
             RefreshScreenForCurrentNetWork();
             this.Cursor = Cursors.Default;
             EnableControls(true, true);
         }
     }
     catch (Exception ex)
     {
         this.Cursor = Cursors.Default;
         MessageBox.Show("Error loading network - " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     finally
     {
         EnableControls(true, true);
     }
 }
        public static NnProject Load(string path)
        {
            NnProject nnProject = new NnProject();

            nnProject.SaveFolder  = System.IO.Path.GetDirectoryName(path);
            nnProject.ProjectName = System.IO.Path.GetFileName(path);
            nnProject.ProjectName = nnProject.ProjectName.Remove(nnProject.ProjectName.IndexOf('.'));

            // 加载网络
            string path2 = System.IO.Path.Combine(nnProject.SaveFolder, nnProject.ProjectName + ".ndn");

            nnProject.Network = (BackpropagationNetwork)LoadNeuralNetwork(path2);

            // 加载数据集
            path2 = System.IO.Path.Combine(nnProject.SaveFolder, nnProject.ProjectName + ".trn");
            nnProject.TrainingSet = LoadTrainingSet(path2);
            path2 = System.IO.Path.Combine(nnProject.SaveFolder, nnProject.ProjectName + ".crv");
            if (File.Exists(path2))
            {
                nnProject.CrossValidationSet = LoadTrainingSet(path2);
            }

            // 保存学习参数
            path2 = System.IO.Path.Combine(nnProject.SaveFolder, nnProject.ProjectName + ".xml");
            nnProject.LearningParameters = NnLearningParameters.Deserialize(path2);

            return(nnProject);
        }
        public static void Save(NnProject nnProject, string saveFolder)
        {
            // 使用默认或传递的文件夹?
            if (saveFolder.Length > 0)
            {
                nnProject.SaveFolder = saveFolder;
            }

            // 保存网络
            string path = System.IO.Path.Combine(nnProject.SaveFolder, nnProject.ProjectName + ".ndn");

            SaveNeuralNetwork(nnProject.Network, path);

            // 保存数据集
            path = System.IO.Path.Combine(nnProject.SaveFolder, nnProject.ProjectName + ".trn");
            SaveTrainingSet(nnProject.TrainingSet, path);
            if (nnProject.CrossValidationSet != null)
            {
                path = System.IO.Path.Combine(nnProject.SaveFolder, nnProject.ProjectName + ".crv");
                SaveTrainingSet(nnProject.CrossValidationSet, path);
            }

            // 保存学习参数
            path = System.IO.Path.Combine(nnProject.SaveFolder, nnProject.ProjectName + ".xml");
            NnLearningParameters.Serialize(path, nnProject.LearningParameters);
        }
Exemple #5
0
 private void buttonExit_Click(object sender, EventArgs e)
 {
     if (currentProject != null)
     {
         DialogResult dr = MessageBox.Show("Save current network?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
         if (dr == DialogResult.Cancel)
         {
             return;
         }
         if (dr == DialogResult.Yes)
         {
             NnProject.Save(currentProject, "");
         }
     }
     this.Close();
 }
Exemple #6
0
 private void newToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         formCreateNew createNewForm = new formCreateNew();
         createNewForm.ShowDialog(this);
         if (createNewForm.NewProject != null)
         {
             currentProject = createNewForm.NewProject;
             this.Text      = currentProject.SaveFolder + @"\" + currentProject.ProjectName;
             RefreshScreenForCurrentNetWork();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Error in program - " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemple #7
0
 private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         if (currentProject == null)
         {
             MessageBox.Show("No network loaded.", "Invalid Operation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
             return;
         }
         SaveFileDialog sfd = new SaveFileDialog();
         sfd.AddExtension    = false;
         sfd.CheckFileExists = false;
         sfd.CheckPathExists = false;
         sfd.ValidateNames   = false;
         //sfd.DefaultExt = "Network files (*.ndn)|*.ndn";
         sfd.CreatePrompt    = false;
         sfd.OverwritePrompt = true;
         sfd.SupportMultiDottedExtensions = false;
         sfd.ShowDialog(this);
         if (sfd.FileName.Length > 0)
         {
             this.Cursor = Cursors.WaitCursor;
             EnableControls(false, true);
             currentProject.ProjectName = System.IO.Path.GetFileName(sfd.FileName);
             if (currentProject.ProjectName.Contains("."))
             {
                 currentProject.ProjectName = currentProject.ProjectName.Remove(currentProject.ProjectName.IndexOf('.'));
             }
             NnProject.Save(currentProject, System.IO.Path.GetDirectoryName(sfd.FileName));
             this.Text = currentProject.SaveFolder + @"\" + currentProject.ProjectName;
             RefreshScreenForCurrentNetWork();
             this.Cursor = Cursors.Default;
         }
     }
     catch (Exception ex)
     {
         this.Cursor = Cursors.Default;
         MessageBox.Show("Error saving network - " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     finally
     {
         EnableControls(true, true);
     }
 }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                //
                // 解析数据文件
                //
                int inputCount  = 0;
                int outputCount = 1;
                if (textInputCount.Text.Length > 0)
                {
                    inputCount = int.Parse(textInputCount.Text);
                }
                if (textOutputCount.Text.Length > 0)
                {
                    outputCount = int.Parse(textOutputCount.Text);
                }
                TrainingSet trainingSet   = DataFile.CsvFileToTrainingSet(textTrainingSet.Text, ref inputCount, ref outputCount);
                TrainingSet crossvalidSet = null;
                if (textCvSet.Text.Length > 0)
                {
                    crossvalidSet = DataFile.CsvFileToTrainingSet(textCvSet.Text, ref inputCount, ref outputCount);
                }


                //
                // 创建新网络
                //

                // 输入层始终与输入计数是线性关系
                LinearLayer inputLayer = new LinearLayer(inputCount);

                // 创建隐层
                ActivationLayer hiddenLayer1 = null;
                ActivationLayer hiddenLayer2 = null;
                ActivationLayer outputLayer  = null;
                if (comboActFunction1.SelectedIndex < 0)
                {
                    MessageBox.Show("请选择激活函数!"); return;
                }
                switch ((HiddenLayerType)comboActFunction1.SelectedItem)
                {
                case HiddenLayerType.Linear: hiddenLayer1 = new LinearLayer(int.Parse(textNeuronCount1.Text)); break;

                case HiddenLayerType.Logarithmic: hiddenLayer1 = new LogarithmLayer(int.Parse(textNeuronCount1.Text)); break;

                case HiddenLayerType.Sigmoid: hiddenLayer1 = new SigmoidLayer(int.Parse(textNeuronCount1.Text)); break;

                case HiddenLayerType.Sine: hiddenLayer1 = new SineLayer(int.Parse(textNeuronCount1.Text)); break;

                case HiddenLayerType.Tanh: hiddenLayer1 = new TanhLayer(int.Parse(textNeuronCount1.Text)); break;
                }
                if (textNeuronCount2.Text.Length > 0 && int.Parse(textNeuronCount2.Text) > 0)
                {
                    switch ((HiddenLayerType)comboActFunction2.SelectedItem)
                    {
                    case HiddenLayerType.Linear: hiddenLayer2 = new LinearLayer(int.Parse(textNeuronCount2.Text)); break;

                    case HiddenLayerType.Logarithmic: hiddenLayer2 = new LogarithmLayer(int.Parse(textNeuronCount2.Text)); break;

                    case HiddenLayerType.Sigmoid: hiddenLayer2 = new SigmoidLayer(int.Parse(textNeuronCount2.Text)); break;

                    case HiddenLayerType.Sine: hiddenLayer2 = new SineLayer(int.Parse(textNeuronCount2.Text)); break;

                    case HiddenLayerType.Tanh: hiddenLayer2 = new TanhLayer(int.Parse(textNeuronCount2.Text)); break;
                    }
                }

                if (comboOutputFunction.SelectedIndex < 0)
                {
                    MessageBox.Show("请选择输出函数!"); return;
                }
                switch ((HiddenLayerType)comboOutputFunction.SelectedItem)
                {
                case HiddenLayerType.Linear: outputLayer = new LinearLayer(outputCount); break;

                case HiddenLayerType.Logarithmic: outputLayer = new LogarithmLayer(outputCount); break;

                case HiddenLayerType.Sigmoid: outputLayer = new SigmoidLayer(outputCount); break;

                case HiddenLayerType.Sine: outputLayer = new SineLayer(outputCount); break;

                case HiddenLayerType.Tanh: outputLayer = new TanhLayer(outputCount); break;
                }

                // 连接层, hidden2是可选的
                new BackpropagationConnector(inputLayer, hiddenLayer1);
                if (hiddenLayer2 != null)
                {
                    new BackpropagationConnector(hiddenLayer1, hiddenLayer2);
                    new BackpropagationConnector(hiddenLayer2, outputLayer);
                }
                else
                {
                    new BackpropagationConnector(hiddenLayer1, outputLayer);
                }
                BackpropagationNetwork backpropNetwork = new BackpropagationNetwork(inputLayer, outputLayer);


                //
                // 设置学习和退出参数
                //

                double startLearningRate = double.Parse(textStartLearningRate.Text);
                double?finalLearningRate = null;
                if (textFinalLearningRate.Text.Length > 0)
                {
                    finalLearningRate = double.Parse(textFinalLearningRate.Text);
                }

                // 如果选择了学习率函数则使用
                LearningRateFunction?lrf = null;
                if (comboLRFunction.SelectedIndex > 0)
                {
                    lrf = (LearningRateFunction)comboLRFunction.SelectedItem;
                    backpropNetwork.SetLearningRate(
                        LearningRateFactory.GetLearningRateFunction(lrf.Value, startLearningRate, finalLearningRate.Value));
                }
                else
                {
                    // 否则使用普通学习率,也许有起点和终点
                    if (finalLearningRate.HasValue)
                    {
                        backpropNetwork.SetLearningRate(startLearningRate, finalLearningRate.Value);
                    }
                    else
                    {
                        backpropNetwork.SetLearningRate(startLearningRate);
                    }
                }

                // 如果给定,在连接器中设置动量
                double?momentum = null;
                if (textMomentum.Text.Length > 0)
                {
                    momentum = double.Parse(textMomentum.Text);
                    foreach (ILayer layer in backpropNetwork.Layers)
                    {
                        foreach (BackpropagationConnector conn in layer.SourceConnectors)
                        {
                            conn.Momentum = momentum.Value;
                        }
                        foreach (BackpropagationConnector conn in layer.TargetConnectors)
                        {
                            conn.Momentum = momentum.Value;
                        }
                    }
                }


                //
                // 新建工程和保存工程
                //
                int tmpInt;
                NewProject         = new NnProject();
                NewProject.Network = backpropNetwork;
                // 确保为新网络初始化权重,默认情况下新的训练周期将不会初始化
                NewProject.Network.Initialize();
                NewProject.ProjectName        = textProjectName.Text.Trim();
                NewProject.SaveFolder         = textSaveFolder.Text;
                NewProject.TrainingSet        = trainingSet;
                NewProject.CrossValidationSet = crossvalidSet;
                NewProject.LearningParameters = new NnProject.NnLearningParameters();
                NewProject.LearningParameters.InitialLearningRate  = startLearningRate;
                NewProject.LearningParameters.FinalLearningRate    = finalLearningRate;
                NewProject.LearningParameters.LearningRateFunction = lrf;
                NewProject.LearningParameters.Momentum             = momentum;
                if (int.TryParse(textTrainingCycles.Text, out tmpInt))
                {
                    NewProject.LearningParameters.MaxTrainingCycles = tmpInt;
                }

                NnProject.Save(NewProject, textSaveFolder.Text);

                this.Close();
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show("Error creating network - " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            { this.Cursor = Cursors.Default; }
        }