コード例 #1
0
        public double[][] StepTrainingMethod()
        {
            ANNParameterBUS.TrainingSize = 35;

            PreprocessBUS preprocessBUS = new PreprocessBUS();
            ANNTrainBUS annTrain = new ANNTrainBUS();

            int len = ArrPattern.Length - ANNParameterBUS.TrainingSize;
            double[][] dblActual_Forecast = new double[2][];
            dblActual_Forecast[0] = new double[len];
            dblActual_Forecast[1] = new double[len];

            ArrPattern = Preprocess.DenormalizeByMinMax(ArrPattern);
            DblActual_Forecast[0] = Preprocess.DenormalizeByMinMax(DblActual_Forecast[0]);
            for (int i = 0; i < len; i++)
            {
                double[][] trainingSet = SelectTrainingSet(i);
                preprocessBUS.FindMinMax(trainingSet);
                trainingSet = preprocessBUS.PreprocessByMinMax(trainingSet);
                annTrain.LoadDataSet(trainingSet);
                annTrain.Main();

                ANNModel.LoadModelFile();
                ANNModel.OutInputLayer = preprocessBUS.PreprocessByMinMax(ArrPattern[i + ANNParameterBUS.TrainingSize - 1]);
                dblActual_Forecast[1][i] = preprocessBUS.DenormalizeByMinMax(ANNModel.FeedforwardTraining());
                dblActual_Forecast[0][i] = DblActual_Forecast[0][i + ANNParameterBUS.TrainingSize];
            }

            DblActual_Forecast = dblActual_Forecast;
            NumPattern = len;

            return dblActual_Forecast;
        }
コード例 #2
0
ファイル: MainGUI.cs プロジェクト: hksonngan/mytesgnikrow
        private void btnTrain_Click(object sender, EventArgs e)
        {
            if (tbxTrainFilePath.Text == "")
            {
                MessageBox.Show("Error: You must fill all required inputs!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int iMeasureType = 0;
            switch (cmbTrainingMeasure.SelectedItem.ToString())
            {
                case "MSE":
                    iMeasureType = MSE;
                    break;
                case "NMSE":
                    iMeasureType = NMSE;
                    break;
                case "RMSE":
                    iMeasureType = RMSE;
                    break;
                case "Sign":
                    iMeasureType = SIGN;
                    break;
            }
            int iCrossValidationType;
            if(rdShuffle.Checked)
            {
                iCrossValidationType = 0;
            }
            else if(rdNotShuffle.Checked)
            {
                iCrossValidationType = 1;
            }
            else
            {
                iCrossValidationType = 2;
            }

            #region Dự đoán giá
            if (rdPricePrediction.Checked)//Dự đoán giá
            {
                int iPos = tbxTrainFilePath.Text.LastIndexOf('_');
                string strMutualPath = tbxTrainFilePath.Text.Remove(iPos + 1);
                string strModelFile = strMutualPath + "model.txt";
                #region SVR
                if (rdSVR.Checked)//Mô hình SVR
                {
                    string[] strArgs = new string[8];
                    strArgs[0] = "-g";  //gama. Trong hàm Gauss kernel: exp(-gamma*|u-v|^2)
                    strArgs[2] = "-c";
                    strArgs[4] = "-p";  //epsilon
                    strArgs[6] = tbxTrainFilePath.Text;
                    strArgs[7] = strModelFile;

                    if (cmbModelSelection.SelectedItem.ToString() == "Use default values")
                    {
                        strArgs[1] = "0";   //gama
                        strArgs[3] = "1";   //c
                        strArgs[5] = "0.1";   //epsilon
                        //strArgs[1] = "0.686";   //gama
                        //strArgs[3] = "68.4717";   //c
                        //strArgs[5] = "0.00012124";   //epsilon
                        //strArgs[1] = "2.5";   //gama
                        //strArgs[3] = "0.01";   //c
                        //strArgs[5] = "0.3";   //epsilon
                        svm_train.Main(strArgs);
                    }
                    else if (cmbModelSelection.SelectedItem.ToString() == "Pattern search")
                    {
                        string[] strArgsModel = new string[4];
                        strArgsModel[0] = "-v";
                        strArgsModel[1] = tbxNumFold.Text;
                        strArgsModel[2] = tbxTrainFilePath.Text;
                        strArgsModel[3] = strModelFile;

                        int iNumModel = 10;
                        StreamWriter writer = new StreamWriter(strModelFile);
                        writer.WriteLine("Multiple");
                        writer.WriteLine(iNumModel.ToString());
                        StreamWriter patternTime = new StreamWriter(strMutualPath + "PatternTime.txt");
                        double dblTotalTime = 0;
                        for (int i = 0; i < iNumModel; i++)
                        {
                            svm_modelSelection modelSelection = new svm_modelSelection();
                            DateTime start = DateTime.Now;
                            modelSelection.Run(strArgsModel, strMutualPath + "PatternSearchLog" + (i + 1).ToString() + ".txt", false, iMeasureType, iCrossValidationType);
                            DateTime end = DateTime.Now;
                            TimeSpan elapsedTime = end - start;
                            dblTotalTime += elapsedTime.TotalMinutes;
                            patternTime.WriteLine((i + 1).ToString() + ": " + elapsedTime.TotalMinutes.ToString() + " minutes");

                            strArgs[1] = (Math.Pow(2, modelSelection.Y)).ToString();   //gama
                            strArgs[3] = (Math.Pow(2, modelSelection.X)).ToString();   //c
                            strArgs[5] = (Math.Pow(2, modelSelection.Z)).ToString();   //epsilon
                            //strArgs[3] = (Math.Pow(2, 6.09743805699862)).ToString();
                            //strArgs[1] = (Math.Pow(2, -3.86541887296616)).ToString();
                            //strArgs[5] = (Math.Pow(2, -13.0097538228192)).ToString();
                            strArgs[7] = strMutualPath + "model" + (i + 1).ToString() + ".txt";
                            writer.WriteLine(strArgs[7]);
                            svm_train.Main(strArgs);
                        }
                        writer.Close();
                        patternTime.WriteLine("Total: " + dblTotalTime.ToString() + " minutes");
                        patternTime.Close();
                    }
                    else if (cmbModelSelection.SelectedItem.ToString() == "Grid search")
                    {
                        svm_modelSelection modelSelection = new svm_modelSelection();
                        string[] strArgsModel = new string[4];
                        strArgsModel[0] = "-v";
                        strArgsModel[1] = tbxNumFold.Text;
                        strArgsModel[2] = tbxTrainFilePath.Text;
                        strArgsModel[3] = strModelFile;
                        DateTime start = DateTime.Now;
                        modelSelection.Run(strArgsModel, strMutualPath + "GridSearchLog.txt", true, iMeasureType, iCrossValidationType);
                        DateTime end = DateTime.Now;
                        TimeSpan elapsedTime = end - start;
                        StreamWriter gridTime = new StreamWriter(strMutualPath + "GridTime.txt");
                        gridTime.WriteLine(elapsedTime.TotalMinutes.ToString() + " minutes");
                        gridTime.Close();

                        strArgs[1] = modelSelection.Y.ToString();   //gama
                        strArgs[3] = modelSelection.X.ToString();   //c
                        strArgs[5] = modelSelection.Z.ToString();   //epsilon
                        svm_train.Main(strArgs);
                    }
                    MessageBox.Show("Finish!");
                }
                #endregion
                #region ANN
                else//Mô hình ANN
                {
                    ANNParameterBUS.InputNode = int.Parse(tbxNumInputNode.Text);
                    ANNParameterBUS.HiddenNode = int.Parse(tbxANNHiddenNode.Text);
                    ANNParameterBUS.OutputNode = 1;
                    ANNParameterBUS.MaxEpoch = int.Parse(tbxMaxLoops.Text);
                    ANNParameterBUS.LearningRate = double.Parse(tbxLearningRate.Text);
                    ANNParameterBUS.Momentum = double.Parse(tbxMomentum.Text);
                    ANNParameterBUS.Bias = double.Parse(tbxBias.Text);

                    ANNModelBUS.AnnModelFile = strModelFile;
                    ANNTrainBUS annTrain = new ANNTrainBUS();
                    annTrain.LoadDataSet(tbxTrainFilePath.Text);
                    annTrain.Main();
                    MessageBox.Show("Finish!");
                }
                #endregion
            }
            #endregion
            #region Dự đoán xu hướng
            else//Dự đoán xu hướng
            {
                if (rdSVR.Checked)//Mô hình SVR
                {
                    int iPos = tbxTrainFilePath.Text.LastIndexOf('_');
                    string strMutualPath = tbxTrainFilePath.Text.Remove(iPos + 1);
                    string strModelFile = strMutualPath + "model.txt";
                    string[] strArgs = new string[8];
                    strArgs[0] = "-g";  //gama. Trong hàm Gauss kernel: exp(-gamma*|u-v|^2)
                    strArgs[2] = "-c";
                    strArgs[4] = "-p";  //epsilon
                    strArgs[6] = tbxTrainFilePath.Text;
                    strArgs[7] = strModelFile;

                    if (cmbModelSelection.SelectedItem.ToString() == "Use default values")
                    {
                        //strArgs[1] = "0";   //gama
                        //strArgs[3] = "1";   //c
                        //strArgs[5] = "0.1";   //epsilon
                        strArgs[1] = "0.0110485434560398";   //gama
                        strArgs[3] = "304.437021440697";   //c
                        strArgs[5] = "0.000290333768311211";   //epsilon
                        //strArgs[1] = "2.5";   //gama
                        //strArgs[3] = "0.01";   //c
                        //strArgs[5] = "0.3";   //epsilon
                        svm_train.Main(strArgs);
                    }
                    else if (cmbModelSelection.SelectedItem.ToString() == "Pattern search")
                    {
                        string[] strArgsModel = new string[4];
                        strArgsModel[0] = "-v";
                        strArgsModel[1] = tbxNumFold.Text;
                        strArgsModel[2] = tbxTrainFilePath.Text;
                        strArgsModel[3] = strModelFile;

                        int iNumModel = 10;
                        StreamWriter writer = new StreamWriter(strModelFile);
                        writer.WriteLine("Multiple");
                        writer.WriteLine(iNumModel.ToString());
                        for (int i = 0; i < iNumModel; i++)
                        {
                            svm_modelSelection modelSelection = new svm_modelSelection();
                            modelSelection.Run(strArgsModel, strMutualPath + "PatternSearchLog" + (i + 1).ToString() + ".txt", false, iMeasureType, iCrossValidationType);
                            strArgs[1] = (Math.Pow(2, modelSelection.Y)).ToString();   //gama
                            strArgs[3] = (Math.Pow(2, modelSelection.X)).ToString();   //c
                            strArgs[5] = (Math.Pow(2, modelSelection.Z)).ToString();   //epsilon
                            //strArgs[3] = (Math.Pow(2, 6.09743805699862)).ToString();
                            //strArgs[1] = (Math.Pow(2, -3.86541887296616)).ToString();
                            //strArgs[5] = (Math.Pow(2, -13.0097538228192)).ToString();
                            strArgs[7] = strMutualPath + "model" + (i + 1).ToString() + ".txt";
                            writer.WriteLine(strArgs[7]);
                            svm_train.Main(strArgs);
                        }
                        writer.Close();
                    }
                    else if (cmbModelSelection.SelectedItem.ToString() == "Grid search")
                    {
                        svm_modelSelection modelSelection = new svm_modelSelection();
                        string[] strArgsModel = new string[4];
                        strArgsModel[0] = "-v";
                        strArgsModel[1] = tbxNumFold.Text;
                        strArgsModel[2] = tbxTrainFilePath.Text;
                        strArgsModel[3] = strModelFile;
                        modelSelection.Run(strArgsModel, strMutualPath + "GridSearchLog.txt", true, iMeasureType, iCrossValidationType);
                        strArgs[1] = modelSelection.Y.ToString();   //gama
                        strArgs[3] = modelSelection.X.ToString();   //c
                        strArgs[5] = modelSelection.Z.ToString();   //epsilon
                        svm_train.Main(strArgs);
                    }
                    MessageBox.Show("Finish!");
                }
                else//Mô hình ANN
                {
                    int iPos = tbxTrainFilePath.Text.LastIndexOf('_');
                    string strModelFile = tbxTrainFilePath.Text.Remove(iPos + 1) + "model.txt";

                    //khởi tạo các tham số cho mạng
                    ANNParameterBUS.InputNode = int.Parse(tbxNumInputNode.Text);
                    ANNParameterBUS.HiddenNode = int.Parse(tbxANNHiddenNode.Text);
                    ANNParameterBUS.OutputNode = 1;
                    ANNParameterBUS.MaxEpoch = int.Parse(tbxMaxLoops.Text);
                    ANNParameterBUS.LearningRate = double.Parse(tbxLearningRate.Text);
                    ANNParameterBUS.Momentum = double.Parse(tbxMomentum.Text);
                    ANNParameterBUS.Bias = double.Parse(tbxBias.Text);

                    ANNParameterBUS.Accuracy = double.Parse(tbxAccuracy.Text);
                    ANNParameterBUS.MeasureType = cmbTrainingMeasure.SelectedItem.ToString();

                    //Tiến hành train
                    ANNModelBUS.AnnModelFile = strModelFile;
                    ANNTrainBUS annTrain = new ANNTrainBUS();
                    annTrain.LoadDataSet(tbxTrainFilePath.Text);
                    annTrain.Main(iMeasureType);
                    MessageBox.Show("Finish!");
                }
            #endregion
            }
        }
コード例 #3
0
        //Giá trị trả về là giá dự đoán
        public double TrainAndPredict(bool isSVR)
        {
            double[][] dblActual_Forecast = null;
            #region Phần chung

            //1. Lấy đoạn dữ liệu cần thiết
            double[] dblData = PickNeededData();
            //2. Tiền xử lý
            PreprocessBUS preprocessBUS = new PreprocessBUS();
            if (Preprocess == "ScaleByMinMax")
            {
                preprocessBUS.FindMinMax(dblData);
                dblData = preprocessBUS.PreprocessByMinMax(dblData);
            }
            ConverterBUS converter = new ConverterBUS();
            int iNumLine = 0;
            if(ImprovedDirection)
            {
                converter.ConvertWImprovedDirection(NumInputNode, dblData, "Training.txt", 100, out iNumLine);
            }
            else
            {
                converter.Convert(NumInputNode, dblData, "Training.txt", out iNumLine);
            }

            //Tạo file test. File test này chỉ có 2 dòng:
            //+Dòng 1: Chứa thông tin tiền xử lý
            //+Dòng 2: Giống như 1 dòng của file training, nhưng giá trị đích không biết (để bằng 0)
            StreamWriter writer = new StreamWriter("Test.txt");

            if (Preprocess == "ScaleByMinMax")
            {
                writer.WriteLine("ScaleByMinMax " + preprocessBUS.Min.ToString() + " " + preprocessBUS.Max.ToString());
            }
            string strLine = "0 ";
            for (int i = 0; i < NumInputNode; i++)
            {
                strLine = strLine + (i+1).ToString() + ":" + dblData[TrainingSize - NumInputNode + i].ToString() + " ";
            }
            writer.WriteLine(strLine);

            writer.Close();
            #endregion

            #region Phần riêng
            //3. Huấn luyện và dự đoán
            #region SVR
            if (isSVR == true)   //SVR
            {
                string[] strArgs = new string[8];
                strArgs[0] = "-g";  //gama. Trong hàm Gauss kernel: exp(-gamma*|u-v|^2)
                strArgs[2] = "-c";
                strArgs[4] = "-p";  //epsilon
                strArgs[6] = "Training.txt";
                strArgs[7] = "SVRModel.txt";
                //Tìm bộ tham số (gama, c, epsilon)
                svm_modelSelection modelSelection = new svm_modelSelection();
                modelSelection.Log = false;
                string[] strArgsModel = new string[4];
                strArgsModel[0] = "-v";
                strArgsModel[1] = NumFold;
                strArgsModel[2] = "Training.txt";
                strArgsModel[3] = "SVRModel.txt";
                if (ModelSelection == "Grid search")
                {
                    modelSelection.Run(strArgsModel, "", true, TrainingMeasure, CrossValidationType);
                    strArgs[1] = modelSelection.Y.ToString(); //gama
                    strArgs[3] = modelSelection.X.ToString(); //c
                    strArgs[5] = modelSelection.Z.ToString(); //epsilon
                    //Huấn luyện
                    svm_train.Main(strArgs);
                    //Dự đoán
                    strArgs = new string[3];
                    strArgs[0] = "Test.txt";
                    strArgs[1] = "SVRModel.txt";
                    strArgs[2] = "Prediction.txt";
                    dblActual_Forecast = svm_predict.MainProcess(strArgs);
                }
                else if(ModelSelection == "Pattern search")
                {
                    const int iNumModel = 10;
                    for (int i = 0; i < iNumModel; i++)
                    {
                        modelSelection = new svm_modelSelection {Log = false};
                        strArgsModel[3] = "SVRModel" + i + 1 + ".txt";
                        modelSelection.Run(strArgsModel, "", false, TrainingMeasure, CrossValidationType);
                        strArgs[1] = (Math.Pow(2, modelSelection.Y)).ToString();   //gama
                        strArgs[3] = (Math.Pow(2, modelSelection.X)).ToString();   //c
                        strArgs[5] = (Math.Pow(2, modelSelection.Z)).ToString();   //epsilon
                        strArgs[7] = "SVRModel" + i + 1 + ".txt";
                        //Huấn luyện
                        svm_train.Main(strArgs);
                    }
                    //Dự đoán
                    double[][][] dblTemp = new double[iNumModel][][];
                    dblActual_Forecast = new double[2][];
                    dblActual_Forecast[0] = new double[]{0};
                    dblActual_Forecast[1] = new double[]{0};

                    for (int i = 0; i < iNumModel; i++)
                    {
                        strArgs = new string[3];
                        strArgs[0] = "Test.txt";
                        strArgs[1] = "SVRModel" + i + 1 + ".txt";
                        strArgs[2] = "Prediction.txt";
                        dblTemp[i] = svm_predict.MainProcess(strArgs);
                        dblActual_Forecast[1][0] += dblTemp[i][1][0];
                    }
                    dblActual_Forecast[1][0] /= iNumModel;
                }
            }
            #endregion
            #region ANN
            else   //ANN
            {
                //Huấn luyện
                ANNParameterBUS.InputNode = NumInputNode;

                ANNModelBUS.AnnModelFile = "ANNModel.txt";
                ANNTrainBUS annTrain = new ANNTrainBUS();
                annTrain.LoadDataSet("Training.txt");
                annTrain.Main();

                //Dự đoán
                ANNParameterBUS.LoadParameter();
                ANNPredictBUS annPredict = new ANNPredictBUS();
                annPredict.LoadDataSet("Test.txt");
                dblActual_Forecast = annPredict.MainProcess();
            }
            #endregion
            #endregion
            return dblActual_Forecast[1][0];    //Giá dự đoán
        }