public static RegressionParameter[] GridRegression(Problem problem, Problem validation, Parameter parameter, List <double> CValues, List <double> GammaValues, List <double> PValues, string outputFile, int MaxBestCount, double IgnoreVolatility) { if (!string.IsNullOrEmpty(outputFile)) { File.WriteAllText(outputFile, string.Empty); } List <RegressionParameter> list = new List <RegressionParameter>(CValues.Count * GammaValues.Count * PValues.Count); for (int i = 0; i < CValues.Count; i++) { for (int j = 0; j < GammaValues.Count; j++) { for (int k = 0; k < PValues.Count; k++) { parameter.C = CValues[i]; parameter.Gamma = GammaValues[j]; parameter.P = PValues[k]; Model model = Training.Train(problem, parameter); double[] array = Prediction.Predict(validation, model); double num = (IgnoreVolatility > 0.0 && (Math.Abs(array[0] - array[1]) <= IgnoreVolatility || Math.Abs(array[1] - array[2]) <= IgnoreVolatility)) ? 1.7976931348623157E+308 : Prediction.ComputeMSE(validation, array, 0); Console.Write("{0} {1} {2} {3}", parameter.C, parameter.Gamma, parameter.P, num); if (!string.IsNullOrEmpty(outputFile)) { File.AppendAllText(outputFile, string.Format("{0} {1} {2} {3}\n", parameter.C, parameter.Gamma, parameter.P, num)); } RegressionParameter regressionParameter = new RegressionParameter(parameter.C, parameter.Gamma, parameter.P, num); regressionParameter.ValidatePredictions = array; list.Add(regressionParameter); } } } list.Sort(); if (MaxBestCount > 0) { int l; for (l = Math.Min(MaxBestCount, list.Count); l < list.Count && l < MaxBestCount * 3 && list[l - 1].SortValue == list[l].SortValue; l++) { } if (list.Count > l) { list.RemoveRange(l, list.Count - l); } list.TrimExcess(); } return(list.ToArray()); }
public static void Train(params string[] args) { Parameter parameters = default(Parameter); Problem problem = default(Problem); bool flag = default(bool); int nrfold = default(int); string filename = default(string); Training.parseCommandLine(args, out parameters, out problem, out flag, out nrfold, out filename); if (flag) { Training.PerformCrossValidation(problem, parameters, nrfold); } else { Model.Write(filename, Training.Train(problem, parameters)); } }
public static void Grid(Problem problem, Problem validation, Parameter parameters, List <double> CValues, List <double> GammaValues, string outputFile, out double C, out double Gamma) { C = 0.0; Gamma = 0.0; double num = -1.7976931348623157E+308; if (!string.IsNullOrEmpty(outputFile)) { File.WriteAllText(outputFile, string.Empty); } for (int i = 0; i < CValues.Count; i++) { for (int j = 0; j < GammaValues.Count; j++) { parameters.C = CValues[i]; parameters.Gamma = GammaValues[j]; Model model = Training.Train(problem, parameters); double num2 = Prediction.Predict(validation, "tmp.txt", model, false, 1); Console.Write("{0} {1} {2}", parameters.C, parameters.Gamma, num2); if (!string.IsNullOrEmpty(outputFile)) { File.AppendAllText(outputFile, string.Format("{0} {1} {2}\n", parameters.C, parameters.Gamma, num2)); } if (num2 > num) { C = parameters.C; Gamma = parameters.Gamma; num = num2; Console.WriteLine(" New Maximum!"); } else { Console.WriteLine(); } } } }