/// <summary> /// Phần train cho SVM /// </summary> private void TrainSVM(bool isBatchMode) { string strTrainFile = null; if (isBatchMode) { strTrainFile = _trainFilePath; } else { strTrainFile = tbxTrainFilePath.Text; } int iPos = strTrainFile.LastIndexOf('_'); string strMutualPath = strTrainFile.Remove(iPos + 1); string strModelFile = strMutualPath + "model.txt"; Problem prob = Problem.Read(strTrainFile); Parameter param = new Parameter(); if (cmbModelSelection.SelectedItem.ToString() == "Grid search") { string strLogFile = strMutualPath + "Grid.txt"; double dblC; double dblGamma; ParameterSelection paramSel = new ParameterSelection(); paramSel.NFOLD = Int32.Parse(tbxNumFold.Text); paramSel.EndEpochEvent += new CrossEpochEventHandler( delegate(object senderNetwork, CrossEpochEventArgs args) { tlsProgressBar.Value = (int)(args.TrainingIteration * 100d / args.Cycles); tlsStatus.Text = "Current parameter set: " + args.TrainingIteration; Application.DoEvents(); }); paramSel.Grid(prob, param, strLogFile, out dblC, out dblGamma); param.C = dblC; param.Gamma = dblGamma; param.Probability = ckbProbEstimate.Checked; Model model = Training.Train(prob, param); Model.Write(strModelFile, model); tlsProgressBar.Value = 0; } else if (cmbModelSelection.SelectedItem.ToString() == "Use default values") { if (tbxC.Text == "" || tbxGamma.Text == "") { MessageBox.Show("Please fill in parameters!"); return; } param.C = double.Parse(tbxC.Text); param.Gamma = double.Parse(tbxGamma.Text); param.Probability = ckbProbEstimate.Checked; Model model = Training.Train(prob, param); Model.Write(strModelFile, model); } }
/// <summary> /// Phần train cho K-SVMeans /// </summary> private void TrainKSVMeans(bool isBatchMode) { string strTrainFile = null; if (isBatchMode) { strTrainFile = _trainFilePath; } else { strTrainFile = tbxTrainFilePath.Text; } int iNumCluster = (int)nmNumCluster.Value; int iPos = strTrainFile.LastIndexOf('_'); string strMutualPath = strTrainFile.Remove(iPos + 1); string strClusterModelFile = strMutualPath + "_clusterModel.txt"; string[] strClusterResultFiles = new string[iNumCluster]; string[] strSVMModelFiles = new string[iNumCluster]; for (int i = 0; i < iNumCluster; i++) { strClusterResultFiles[i] = strMutualPath + "cluster" + (i + 1).ToString() + ".txt"; strSVMModelFiles[i] = strMutualPath + "model" + (i + 1).ToString() + ".txt"; } // Thực hiện cluster SampleDataBUS samDataBUS = new SampleDataBUS(); samDataBUS.Read(strTrainFile); Clustering clustering = new Clustering(iNumCluster, samDataBUS.Samples, DistanceType.Manhattan); clustering.Run(strClusterModelFile, false); samDataBUS.WriteIntoCluster(strClusterResultFiles, clustering.SampleData.ClusterIndices); // Thực hiện train SVM int iProgressBaseline = 0; for (int i = 0; i < iNumCluster; i++) { Problem prob = Problem.Read(strClusterResultFiles[i]); Parameter param = new Parameter(); iProgressBaseline = i * 100 / iNumCluster; if (cmbModelSelection.SelectedItem.ToString() == "Grid search") { string strLogFile = strMutualPath + "GridCluster" + (i + 1).ToString() + ".txt"; double dblC; double dblGamma; ParameterSelection paramSel = new ParameterSelection(); paramSel.NFOLD = Int32.Parse(tbxNumFold.Text); paramSel.EndEpochEvent += new CrossEpochEventHandler( delegate(object senderNetwork, CrossEpochEventArgs args) { tlsProgressBar.Value = iProgressBaseline + (int)(args.TrainingIteration * 100d / (args.Cycles * iNumCluster)); tlsStatus.Text = "Cluster: " + (i + 1) + " | Current parameter set: " + args.TrainingIteration; Application.DoEvents(); }); paramSel.Grid(prob, param, strLogFile, out dblC, out dblGamma); param.C = dblC; param.Gamma = dblGamma; param.Probability = ckbProbEstimate.Checked; Model model = Training.Train(prob, param); Model.Write(strSVMModelFiles[i], model); } else if (cmbModelSelection.SelectedItem.ToString() == "Use default values") { if (tbxC.Text == "" || tbxGamma.Text == "") { MessageBox.Show("Please fill in parameters!"); return; } param.C = double.Parse(tbxC.Text); param.Gamma = double.Parse(tbxGamma.Text); param.Probability = ckbProbEstimate.Checked; Model model = Training.Train(prob, param); Model.Write(strSVMModelFiles[i], model); } } tlsProgressBar.Value = 0; }