~ubuntu-branches/ubuntu/oneiric/weka/oneiric

« back to all changes in this revision

Viewing changes to weka/classifiers/functions/supportVector/PrecomputedKernelMatrixKernel.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner, Soeren Sonnenburg, Torsten Werner
  • Date: 2008-08-10 21:27:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080810212705-tr8etpnkdx2ziktp
Tags: 3.5.8-1
[ Soeren Sonnenburg ]
* Bump Standards Version to 3.8.0.
* Remove references to non-free Java in debian/copyright.

[ Torsten Werner ]
* new upstream release
* Switch to openjdk-6.
* Move package to main.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    This program is free software; you can redistribute it and/or modify
 
3
 *    it under the terms of the GNU General Public License as published by
 
4
 *    the Free Software Foundation; either version 2 of the License, or
 
5
 *    (at your option) any later version.
 
6
 *
 
7
 *    This program is distributed in the hope that it will be useful,
 
8
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *    GNU General Public License for more details.
 
11
 *
 
12
 *    You should have received a copy of the GNU General Public License
 
13
 *    along with this program; if not, write to the Free Software
 
14
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
15
 */
 
16
 
 
17
/*
 
18
 *    PrecomputedKernelMatrixKernel.java
 
19
 *    Copyright (C) 2008 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
package weka.classifiers.functions.supportVector;
 
23
 
 
24
import weka.core.Option;
 
25
import weka.core.matrix.Matrix;
 
26
import weka.core.Instances;
 
27
import weka.core.Utils;
 
28
import weka.core.Capabilities;
 
29
import weka.core.Capabilities.Capability;
 
30
import weka.core.Instance;
 
31
import weka.core.RevisionHandler;
 
32
import weka.core.RevisionUtils;
 
33
 
 
34
import java.util.Enumeration;
 
35
import java.util.Vector;
 
36
 
 
37
import java.io.FileReader;
 
38
import java.io.File;
 
39
 
 
40
/**
 
41
 *
 
42
 <!-- globalinfo-start -->
 
43
 * This kernel is based on a static kernel matrix that is read from a file. Instances must have a single nominal attribute (excluding the class). This attribute must be the first attribute in the file and its values are used to reference rows/columns in the kernel matrix. The second attribute must be the class attribute.
 
44
 * <p/>
 
45
 <!-- globalinfo-end -->
 
46
 * 
 
47
 <!-- options-start -->
 
48
 * Valid options are: <p/>
 
49
 * 
 
50
 * <pre> -D
 
51
 *  Enables debugging output (if available) to be printed.
 
52
 *  (default: off)</pre>
 
53
 * 
 
54
 * <pre> -no-checks
 
55
 *  Turns off all checks - use with caution!
 
56
 *  (default: checks on)</pre>
 
57
 * 
 
58
 * <pre> -M &lt;file name&gt;
 
59
 *  The file name of the file that holds the kernel matrix.
 
60
 *  (default: kernelMatrix.matrix)</pre>
 
61
 * 
 
62
 <!-- options-end -->
 
63
 *
 
64
 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
 
65
 * @version $Revision: 1.1 $
 
66
 */
 
67
public class PrecomputedKernelMatrixKernel extends Kernel {
 
68
 
 
69
  /** for serialization */
 
70
  static final long serialVersionUID = -321831645846363333L;
 
71
 
 
72
  /** The file holding the kernel matrix. */
 
73
  protected File m_KernelMatrixFile = new File("kernelMatrix.matrix");
 
74
          
 
75
  /** The kernel matrix. */
 
76
  protected Matrix m_KernelMatrix;
 
77
          
 
78
  /** A classifier counter. */
 
79
  protected int m_Counter;
 
80
 
 
81
  /**
 
82
   * Returns a string describing the kernel
 
83
   * 
 
84
   * @return a description suitable for displaying in the
 
85
   *         explorer/experimenter gui
 
86
   */
 
87
  public String globalInfo() {
 
88
    return 
 
89
      "This kernel is based on a static kernel matrix that is read from a file. " +
 
90
      "Instances must have a single nominal attribute (excluding the class). " +
 
91
      "This attribute must be the first attribute in the file and its values are " +
 
92
      "used to reference rows/columns in the kernel matrix. The second attribute " +
 
93
      "must be the class attribute.";
 
94
  }
 
95
          
 
96
  /**
 
97
   * Returns an enumeration describing the available options.
 
98
   *
 
99
   * @return            an enumeration of all the available options.
 
100
   */
 
101
  public Enumeration listOptions() {
 
102
    Vector              result;
 
103
            
 
104
    result = new Vector();
 
105
 
 
106
    Enumeration en = super.listOptions();
 
107
    while (en.hasMoreElements())
 
108
      result.addElement(en.nextElement());
 
109
 
 
110
    result.addElement(new Option(
 
111
                                 "\tThe file name of the file that holds the kernel matrix.\n"
 
112
                                 + "\t(default: kernelMatrix.matrix)",
 
113
                                 "M", 1, "-M <file name>"));
 
114
 
 
115
    return result.elements();
 
116
  }
 
117
 
 
118
  /**
 
119
   * Parses a given list of options. <p/>
 
120
   * 
 
121
   <!-- options-start -->
 
122
   * Valid options are: <p/>
 
123
   * 
 
124
   * <pre> -D
 
125
   *  Enables debugging output (if available) to be printed.
 
126
   *  (default: off)</pre>
 
127
   * 
 
128
   * <pre> -no-checks
 
129
   *  Turns off all checks - use with caution!
 
130
   *  (default: checks on)</pre>
 
131
   * 
 
132
   * <pre> -M &lt;file name&gt;
 
133
   *  The file name of the file that holds the kernel matrix.
 
134
   *  (default: kernelMatrix.matrix)</pre>
 
135
   * 
 
136
   <!-- options-end -->
 
137
   * 
 
138
   * @param options     the list of options as an array of strings
 
139
   * @throws Exception  if an option is not supported
 
140
   */
 
141
  public void setOptions(String[] options) throws Exception {
 
142
    String      tmpStr;
 
143
            
 
144
    tmpStr = Utils.getOption('M', options);
 
145
    if (tmpStr.length() != 0)
 
146
      setKernelMatrixFile(new File(tmpStr));
 
147
    else
 
148
      setKernelMatrixFile(new File("kernelMatrix.matrix"));
 
149
            
 
150
    super.setOptions(options);
 
151
  }
 
152
 
 
153
  /**
 
154
   * Gets the current settings of the Kernel.
 
155
   *
 
156
   * @return an array of strings suitable for passing to setOptions
 
157
   */
 
158
  public String[] getOptions() {
 
159
    int       i;
 
160
    Vector<String>    result;
 
161
    String[]  options;
 
162
 
 
163
    result = new Vector<String>();
 
164
            
 
165
    options = super.getOptions();
 
166
    for (i = 0; i < options.length; i++)
 
167
      result.add(options[i]);
 
168
 
 
169
    result.add("-M");
 
170
    result.add("" + getKernelMatrixFile());
 
171
 
 
172
    return (String[]) result.toArray(new String[result.size()]);          
 
173
  }
 
174
 
 
175
  /**
 
176
   * 
 
177
   * @param id1         the index of instance 1
 
178
   * @param id2         the index of instance 2
 
179
   * @param inst1       the instance 1 object
 
180
   * @return            the dot product
 
181
   * @throws Exception  if something goes wrong
 
182
   */
 
183
  public double eval(int id1, int id2, Instance inst1)
 
184
    throws Exception {
 
185
                        
 
186
    if (m_KernelMatrix == null) {
 
187
      throw new IllegalArgumentException("Kernel matrix has not been loaded successfully.");
 
188
    }
 
189
    int index1 = -1;
 
190
    if (id1 > -1) { 
 
191
      index1 = (int)m_data.instance(id1).value(0);
 
192
    } else {
 
193
      index1 = (int)inst1.value(0);
 
194
    }
 
195
    int index2 = (int)m_data.instance(id2).value(0);
 
196
    return m_KernelMatrix.get(index1, index2);
 
197
  }
 
198
          
 
199
  /**
 
200
   * initializes variables etc.
 
201
   * 
 
202
   * @param data        the data to use
 
203
   */
 
204
  protected void initVars(Instances data) {
 
205
    super.initVars(data);
 
206
 
 
207
    try {
 
208
      if (m_KernelMatrix == null) {
 
209
        m_KernelMatrix = new Matrix(new FileReader(m_KernelMatrixFile));
 
210
        //        System.err.println("Read kernel matrix.");
 
211
      }
 
212
    } catch (Exception e) {
 
213
      System.err.println("Problem reading matrix from " + m_KernelMatrixFile);
 
214
    }
 
215
    m_Counter++;
 
216
    //    System.err.print("Building classifier: " + m_Counter + "\r");
 
217
  }
 
218
 
 
219
  /** 
 
220
   * Returns the Capabilities of this kernel.
 
221
   *
 
222
   * @return            the capabilities of this object
 
223
   * @see               Capabilities
 
224
   */
 
225
  public Capabilities getCapabilities() {
 
226
    Capabilities result = super.getCapabilities();
 
227
            
 
228
    result.enable(Capability.NOMINAL_ATTRIBUTES);
 
229
    result.enableAllClasses();
 
230
    result.enable(Capability.MISSING_CLASS_VALUES);
 
231
            
 
232
    return result;
 
233
  }
 
234
          
 
235
  /**
 
236
   * Sets the file holding the kernel matrix
 
237
   * 
 
238
   * @param value       the exponent value
 
239
   */
 
240
  public void setKernelMatrixFile(File f) {
 
241
    m_KernelMatrixFile = f;
 
242
  }
 
243
          
 
244
  /**
 
245
   * Gets the file containing the kernel matrix.
 
246
   * 
 
247
   * @return            the exponent value
 
248
   */
 
249
  public File getKernelMatrixFile() {
 
250
    return m_KernelMatrixFile;
 
251
  }
 
252
 
 
253
  /**
 
254
   * Returns the tip text for this property
 
255
   * 
 
256
   * @return            tip text for this property suitable for
 
257
   *                    displaying in the explorer/experimenter gui
 
258
   */
 
259
  public String kernelMatrixFileTipText() {
 
260
    return "The file holding the kernel matrix.";
 
261
  }
 
262
 
 
263
  /**
 
264
   * Set the kernel matrix. This method is used by the
 
265
   * unit test for this class, as it loads at test matrix
 
266
   * as a system resource.
 
267
   *
 
268
   * @param km the kernel matrix to use
 
269
   */
 
270
  protected void setKernelMatrix(Matrix km) {
 
271
    m_KernelMatrix = km;
 
272
  }
 
273
                  
 
274
  /**
 
275
   * returns a string representation for the Kernel
 
276
   * 
 
277
   * @return            a string representaiton of the kernel
 
278
   */
 
279
  public String toString() {
 
280
    return "Using kernel matrix from file with name: " + getKernelMatrixFile();
 
281
  }
 
282
          
 
283
  /**
 
284
   * Frees the memory used by the kernel.
 
285
   * (Useful with kernels which use cache.)
 
286
   * This function is called when the training is done.
 
287
   * i.e. after that, eval will be called with id1 == -1.
 
288
   */
 
289
  public void clean() {
 
290
    // do nothing
 
291
  }
 
292
 
 
293
  /**
 
294
   * Returns the number of kernel evaluation performed.
 
295
   *
 
296
   * @return the number of kernel evaluation performed.
 
297
   */
 
298
  public int numEvals() {
 
299
    return 0;
 
300
  }
 
301
 
 
302
  /**
 
303
   * Returns the number of dot product cache hits.
 
304
   *
 
305
   * @return the number of dot product cache hits, or -1 if not supported by this kernel.
 
306
   */
 
307
  public int numCacheHits() {
 
308
    return 0;
 
309
  }
 
310
 
 
311
  /**
 
312
   * Returns the revision string.
 
313
   * 
 
314
   * @return            the revision
 
315
   */
 
316
  public String getRevision() {
 
317
    return RevisionUtils.extract("$Revision: 1.1 $");
 
318
  }
 
319
}
 
320