Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/LibSVM/ParameterSelection.cs @ 4023

Last change on this file since 4023 was 2415, checked in by gkronber, 15 years ago

Updated LibSVM project to latest version. #774

File size: 11.2 KB
Line 
1/*
2 * SVM.NET Library
3 * Copyright (C) 2008 Matthew Johnson
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20using System;
21using System.Collections.Generic;
22using System.IO;
23
24namespace SVM
25{
26    /// <summary>
27    /// This class contains routines which perform parameter selection for a model which uses C-SVC and
28    /// an RBF kernel.
29    /// </summary>
30    public static class ParameterSelection
31    {
32        /// <summary>
33        /// Default number of times to divide the data.
34        /// </summary>
35        public const int NFOLD = 5;
36        /// <summary>
37        /// Default minimum power of 2 for the C value (-5)
38        /// </summary>
39        public const int MIN_C = -5;
40        /// <summary>
41        /// Default maximum power of 2 for the C value (15)
42        /// </summary>
43        public const int MAX_C = 15;
44        /// <summary>
45        /// Default power iteration step for the C value (2)
46        /// </summary>
47        public const int C_STEP = 2;
48        /// <summary>
49        /// Default minimum power of 2 for the Gamma value (-15)
50        /// </summary>
51        public const int MIN_G = -15;
52        /// <summary>
53        /// Default maximum power of 2 for the Gamma Value (3)
54        /// </summary>
55        public const int MAX_G = 3;
56        /// <summary>
57        /// Default power iteration step for the Gamma value (2)
58        /// </summary>
59        public const int G_STEP = 2;
60
61        /// <summary>
62        /// Returns a logarithmic list of values from minimum power of 2 to the maximum power of 2 using the provided iteration size.
63        /// </summary>
64        /// <param name="minPower">The minimum power of 2</param>
65        /// <param name="maxPower">The maximum power of 2</param>
66        /// <param name="iteration">The iteration size to use in powers</param>
67        /// <returns></returns>
68        public static List<double> GetList(double minPower, double maxPower, double iteration)
69        {
70            List<double> list = new List<double>();
71            for (double d = minPower; d <= maxPower; d += iteration)
72                list.Add(Math.Pow(2, d));
73            return list;
74        }
75
76        /// <summary>
77        /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the
78        /// 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
79        /// divide it 5 times to allow 5-fold validation (training on 4/5 and validating on 1/5, 5 times).
80        /// </summary>
81        /// <param name="problem">The training data</param>
82        /// <param name="parameters">The parameters to use when optimizing</param>
83        /// <param name="outputFile">Output file for the parameter results.</param>
84        /// <param name="C">The optimal C value will be put into this variable</param>
85        /// <param name="Gamma">The optimal Gamma value will be put into this variable</param>
86        public static void Grid(
87            Problem problem,
88            Parameter parameters,
89            string outputFile,
90            out double C,
91            out double Gamma)
92        {
93            Grid(problem, parameters, GetList(MIN_C, MAX_C, C_STEP), GetList(MIN_G, MAX_G, G_STEP), outputFile, NFOLD, out C, out Gamma);
94        }
95        /// <summary>
96        /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the
97        /// combination which performed best.  Use this method if there is no validation data available, and it will
98        /// divide it 5 times to allow 5-fold validation (training on 4/5 and validating on 1/5, 5 times).
99        /// </summary>
100        /// <param name="problem">The training data</param>
101        /// <param name="parameters">The parameters to use when optimizing</param>
102        /// <param name="CValues">The set of C values to use</param>
103        /// <param name="GammaValues">The set of Gamma values to use</param>
104        /// <param name="outputFile">Output file for the parameter results.</param>
105        /// <param name="C">The optimal C value will be put into this variable</param>
106        /// <param name="Gamma">The optimal Gamma value will be put into this variable</param>
107        public static void Grid(
108            Problem problem,
109            Parameter parameters,
110            List<double> CValues,
111            List<double> GammaValues,
112            string outputFile,
113            out double C,
114            out double Gamma)
115        {
116            Grid(problem, parameters, CValues, GammaValues, outputFile, NFOLD, out C, out Gamma);
117        }
118        /// <summary>
119        /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the
120        /// combination which performed best.  Use this method if validation data isn't available, as it will
121        /// divide the training data and train on a portion of it and test on the rest.
122        /// </summary>
123        /// <param name="problem">The training data</param>
124        /// <param name="parameters">The parameters to use when optimizing</param>
125        /// <param name="CValues">The set of C values to use</param>
126        /// <param name="GammaValues">The set of Gamma values to use</param>
127        /// <param name="outputFile">Output file for the parameter results.</param>
128        /// <param name="nrfold">The number of times the data should be divided for validation</param>
129        /// <param name="C">The optimal C value will be placed in this variable</param>
130        /// <param name="Gamma">The optimal Gamma value will be placed in this variable</param>
131        public static void Grid(
132            Problem problem,
133            Parameter parameters,
134            List<double> CValues,
135            List<double> GammaValues,
136            string outputFile,
137            int nrfold,
138            out double C,
139            out double Gamma)
140        {
141            C = 0;
142            Gamma = 0;
143            double crossValidation = double.MinValue;
144            StreamWriter output = null;
145            if(outputFile != null)
146                output = new StreamWriter(outputFile);
147            for(int i=0; i<CValues.Count; i++)
148                for (int j = 0; j < GammaValues.Count; j++)
149                {
150                    parameters.C = CValues[i];
151                    parameters.Gamma = GammaValues[j];
152                    double test = Training.PerformCrossValidation(problem, parameters, nrfold);
153                    Console.Write("{0} {1} {2}", parameters.C, parameters.Gamma, test);
154                    if(output != null)
155                        output.WriteLine("{0} {1} {2}", parameters.C, parameters.Gamma, test);
156                    if (test > crossValidation)
157                    {
158                        C = parameters.C;
159                        Gamma = parameters.Gamma;
160                        crossValidation = test;
161                        Console.WriteLine(" New Maximum!");
162                    }
163                    else Console.WriteLine();
164                }
165            if(output != null)
166                output.Close();
167        }
168        /// <summary>
169        /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the
170        /// combination which performed best.  Uses the default values of C and Gamma.
171        /// </summary>
172        /// <param name="problem">The training data</param>
173        /// <param name="validation">The validation data</param>
174        /// <param name="parameters">The parameters to use when optimizing</param>
175        /// <param name="outputFile">The output file for the parameter results</param>
176        /// <param name="C">The optimal C value will be placed in this variable</param>
177        /// <param name="Gamma">The optimal Gamma value will be placed in this variable</param>
178        public static void Grid(
179            Problem problem,
180            Problem validation,
181            Parameter parameters,
182            string outputFile,
183            out double C,
184            out double Gamma)
185        {
186            Grid(problem, validation, parameters, GetList(MIN_C, MAX_C, C_STEP), GetList(MIN_G, MAX_G, G_STEP), outputFile, out C, out Gamma);
187        }
188        /// <summary>
189        /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the
190        /// combination which performed best.
191        /// </summary>
192        /// <param name="problem">The training data</param>
193        /// <param name="validation">The validation data</param>
194        /// <param name="parameters">The parameters to use when optimizing</param>
195        /// <param name="CValues">The C values to use</param>
196        /// <param name="GammaValues">The Gamma values to use</param>
197        /// <param name="outputFile">The output file for the parameter results</param>
198        /// <param name="C">The optimal C value will be placed in this variable</param>
199        /// <param name="Gamma">The optimal Gamma value will be placed in this variable</param>
200        public static void Grid(
201            Problem problem,
202            Problem validation,
203            Parameter parameters,
204            List<double> CValues,
205            List<double> GammaValues,
206            string outputFile,
207            out double C,
208            out double Gamma)
209        {
210            C = 0;
211            Gamma = 0;
212            double maxScore = double.MinValue;
213            StreamWriter output = null;
214            if(outputFile != null)
215                output = new StreamWriter(outputFile);
216            for (int i = 0; i < CValues.Count; i++)
217                for (int j = 0; j < GammaValues.Count; j++)
218                {
219                    parameters.C = CValues[i];
220                    parameters.Gamma = GammaValues[j];
221                    Model model = Training.Train(problem, parameters);
222                    double test = Prediction.Predict(validation, "tmp.txt", model, false);
223                    Console.Write("{0} {1} {2}", parameters.C, parameters.Gamma, test);
224                    if(output != null)
225                        output.WriteLine("{0} {1} {2}", parameters.C, parameters.Gamma, test);
226                    if (test > maxScore)
227                    {
228                        C = parameters.C;
229                        Gamma = parameters.Gamma;
230                        maxScore = test;
231                        Console.WriteLine(" New Maximum!");
232                    }
233                    else Console.WriteLine();
234                }
235            if(output != null)
236                output.Close();
237        }
238    }
239}
Note: See TracBrowser for help on using the repository browser.