/* * SVM.NET Library * Copyright (C) 2008 Matthew Johnson * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace SVM.BotSharp.MachineLearning { /// /// Class representing a grid square result. /// public class GridSquare { /// /// The C value /// public double C; /// /// The Gamma value /// public double Gamma; /// /// The cross validation score /// public double Score; public override string ToString() { return string.Format("{0} {1} {2}", C, Gamma, Score); } } /// /// This class contains routines which perform parameter selection for a model which uses C-SVC and /// an RBF kernel. /// public static class ParameterSelection { /// /// Default number of times to divide the data. /// public const int NFOLD = 5; /// /// Default minimum power of 2 for the C value (-5) /// public const int MIN_C = -5; /// /// Default maximum power of 2 for the C value (15) /// public const int MAX_C = 15; /// /// Default power iteration step for the C value (2) /// public const int C_STEP = 2; /// /// Default minimum power of 2 for the Gamma value (-15) /// public const int MIN_G = -15; /// /// Default maximum power of 2 for the Gamma Value (3) /// public const int MAX_G = 3; /// /// Default power iteration step for the Gamma value (2) /// public const int G_STEP = 2; /// /// Used to control the degree of parallelism used in grid exploration. Default value is the number of processors. /// public static int Threads = Environment.ProcessorCount; /// /// Returns a logarithmic list of values from minimum power of 2 to the maximum power of 2 using the provided iteration size. /// /// The minimum power of 2 /// The maximum power of 2 /// The iteration size to use in powers /// public static List GetList(double minPower, double maxPower, double iteration) { List list = new List(); for (double d = minPower; d <= maxPower; d += iteration) list.Add(Math.Pow(2, d)); return list; } /// /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the /// combination which performed best. The default ranges of C and Gamma values are used. Use this method if there is no validation data available, and it will /// divide it 5 times to allow 5-fold validation (training on 4/5 and validating on 1/5, 5 times). /// /// The training data /// The parameters to use when optimizing /// Function used to report results /// The optimal C value will be put into this variable /// The optimal Gamma value will be put into this variable /// A list of grid squares and their results public static List Grid( Problem problem, Func createParams, Action report, out double C, out double Gamma) { return Grid(problem, createParams, GetList(MIN_C, MAX_C, C_STEP), GetList(MIN_G, MAX_G, G_STEP), report, NFOLD, out C, out Gamma); } /// /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the /// combination which performed best. Use this method if there is no validation data available, and it will /// divide it 5 times to allow 5-fold validation (training on 4/5 and validating on 1/5, 5 times). /// /// The training data /// The parameters to use when optimizing /// The set of C values to use /// The set of Gamma values to use /// Function used to report results /// The optimal C value will be put into this variable /// The optimal Gamma value will be put into this variable /// A list of grid squares and their results public static List Grid( Problem problem, Func createParams, List CValues, List GammaValues, Action report, out double C, out double Gamma) { return Grid(problem, createParams, CValues, GammaValues, report, NFOLD, out C, out Gamma); } /// /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the /// combination which performed best. Use this method if validation data isn't available, as it will /// divide the training data and train on a portion of it and test on the rest. /// /// The training data /// The parameters to use when optimizing /// The set of C values to use /// The set of Gamma values to use /// Function used to report results /// The number of times the data should be divided for validation /// The optimal C value will be placed in this variable /// The optimal Gamma value will be placed in this variable /// A list of grid squares and their results public static List Grid( Problem problem, Func createParams, List CValues, List GammaValues, Action report, int nrfold, out double C, out double Gamma) { C = 0; Gamma = 0; List squares = new List(); foreach (double testC in CValues) foreach (double testGamma in GammaValues) squares.Add(new GridSquare { C = testC, Gamma = testGamma }); ThreadLocal parameters = new ThreadLocal(() => createParams()); Parallel.ForEach(squares, new ParallelOptions{MaxDegreeOfParallelism=Threads}, square => { parameters.Value.C = square.C; parameters.Value.Gamma = square.Gamma; square.Score = Training.PerformCrossValidation(problem, parameters.Value, nrfold); if(report != null) report(square); }); GridSquare best = squares.OrderByDescending(o => o.Score).First(); C = best.C; Gamma = best.Gamma; return squares.OrderBy(o => o.C).ThenBy(o => o.Gamma).ToList(); } /// /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the /// combination which performed best. Uses the default values of C and Gamma. /// /// The training data /// The validation data /// The parameters to use when optimizing /// Function used to report results /// The optimal C value will be placed in this variable /// The optimal Gamma value will be placed in this variable /// A list of grid squares and their results public static List Grid( Problem problem, Problem validation, Func createParams, Action report, out double C, out double Gamma) { return Grid(problem, validation, createParams, GetList(MIN_C, MAX_C, C_STEP), GetList(MIN_G, MAX_G, G_STEP), report, out C, out Gamma); } /// /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the /// combination which performed best. /// /// The training data /// The validation data /// The parameters to use when optimizing /// The C values to use /// The Gamma values to use /// Function used to report results /// The optimal C value will be placed in this variable /// The optimal Gamma value will be placed in this variable /// A list of grid squares and their results public static List Grid( Problem problem, Problem validation, Func createParams, List CValues, List GammaValues, Action report, out double C, out double Gamma) { C = 0; Gamma = 0; List squares = new List(); foreach (double testC in CValues) foreach (double testGamma in GammaValues) squares.Add(new GridSquare { C = testC, Gamma = testGamma }); ThreadLocal parameters = new ThreadLocal(() => createParams()); Parallel.ForEach(squares, new ParallelOptions { MaxDegreeOfParallelism = Threads }, square => { parameters.Value.C = square.C; parameters.Value.Gamma = square.Gamma; Model model = Training.Train(problem, parameters.Value); square.Score = Prediction.Predict(validation, null, model, false); if (report != null) report(square); }); GridSquare best = squares.OrderByDescending(o => o.Score).First(); C = best.C; Gamma = best.Gamma; return squares.OrderBy(o=>o.C).ThenBy(o=>o.Gamma).ToList(); } } }