Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/LibSVM/ParameterSelection.cs @ 2396

Last change on this file since 2396 was 1819, checked in by mkommend, 16 years ago

created new project for LibSVM source files (ticket #619)

File size: 10.9 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    /// <remarks>
27    /// This class contains routines which perform parameter selection for a model which uses C-SVC and
28    /// an RBF kernel.
29    /// </remarks>
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 = new StreamWriter("graph.txt");
145            for(int i=0; i<CValues.Count; i++)
146                for (int j = 0; j < GammaValues.Count; j++)
147                {
148                    parameters.C = CValues[i];
149                    parameters.Gamma = GammaValues[j];
150                    double test = Training.PerformCrossValidation(problem, parameters, nrfold);
151                    Console.Write("{0} {1} {2}", parameters.C, parameters.Gamma, test);
152                    output.WriteLine("{0} {1} {2}", parameters.C, parameters.Gamma, test);
153                    if (test > crossValidation)
154                    {
155                        C = parameters.C;
156                        Gamma = parameters.Gamma;
157                        crossValidation = test;
158                        Console.WriteLine(" New Maximum!");
159                    }
160                    else Console.WriteLine();
161                }
162            output.Close();
163        }
164        /// <summary>
165        /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the
166        /// combination which performed best.  Uses the default values of C and Gamma.
167        /// </summary>
168        /// <param name="problem">The training data</param>
169        /// <param name="validation">The validation data</param>
170        /// <param name="parameters">The parameters to use when optimizing</param>
171        /// <param name="outputFile">The output file for the parameter results</param>
172        /// <param name="C">The optimal C value will be placed in this variable</param>
173        /// <param name="Gamma">The optimal Gamma value will be placed in this variable</param>
174        public static void Grid(
175            Problem problem,
176            Problem validation,
177            Parameter parameters,
178            string outputFile,
179            out double C,
180            out double Gamma)
181        {
182            Grid(problem, validation, parameters, GetList(MIN_C, MAX_C, C_STEP), GetList(MIN_G, MAX_G, G_STEP), outputFile, out C, out Gamma);
183        }
184        /// <summary>
185        /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the
186        /// combination which performed best.
187        /// </summary>
188        /// <param name="problem">The training data</param>
189        /// <param name="validation">The validation data</param>
190        /// <param name="parameters">The parameters to use when optimizing</param>
191        /// <param name="CValues">The C values to use</param>
192        /// <param name="GammaValues">The Gamma values to use</param>
193        /// <param name="outputFile">The output file for the parameter results</param>
194        /// <param name="C">The optimal C value will be placed in this variable</param>
195        /// <param name="Gamma">The optimal Gamma value will be placed in this variable</param>
196        public static void Grid(
197            Problem problem,
198            Problem validation,
199            Parameter parameters,
200            List<double> CValues,
201            List<double> GammaValues,
202            string outputFile,
203            out double C,
204            out double Gamma)
205        {
206            C = 0;
207            Gamma = 0;
208            double maxScore = double.MinValue;
209            StreamWriter output = new StreamWriter(outputFile);
210            for (int i = 0; i < CValues.Count; i++)
211                for (int j = 0; j < GammaValues.Count; j++)
212                {
213                    parameters.C = CValues[i];
214                    parameters.Gamma = GammaValues[j];
215                    Model model = Training.Train(problem, parameters);
216                    double test = Prediction.Predict(validation, "tmp.txt", model, false);
217                    Console.Write("{0} {1} {2}", parameters.C, parameters.Gamma, test);
218                    output.WriteLine("{0} {1} {2}", parameters.C, parameters.Gamma, test);
219                    if (test > maxScore)
220                    {
221                        C = parameters.C;
222                        Gamma = parameters.Gamma;
223                        maxScore = test;
224                        Console.WriteLine(" New Maximum!");
225                    }
226                    else Console.WriteLine();
227                }
228            output.Close();
229        }
230    }
231}
Note: See TracBrowser for help on using the repository browser.