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.
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.
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.
18
* PrecomputedKernelMatrixKernel.java
19
* Copyright (C) 2008 University of Waikato, Hamilton, New Zealand
22
package weka.classifiers.functions.supportVector;
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;
34
import java.util.Enumeration;
35
import java.util.Vector;
37
import java.io.FileReader;
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.
45
<!-- globalinfo-end -->
47
<!-- options-start -->
48
* Valid options are: <p/>
51
* Enables debugging output (if available) to be printed.
52
* (default: off)</pre>
55
* Turns off all checks - use with caution!
56
* (default: checks on)</pre>
58
* <pre> -M <file name>
59
* The file name of the file that holds the kernel matrix.
60
* (default: kernelMatrix.matrix)</pre>
64
* @author Eibe Frank (eibe@cs.waikato.ac.nz)
65
* @version $Revision: 1.1 $
67
public class PrecomputedKernelMatrixKernel extends Kernel {
69
/** for serialization */
70
static final long serialVersionUID = -321831645846363333L;
72
/** The file holding the kernel matrix. */
73
protected File m_KernelMatrixFile = new File("kernelMatrix.matrix");
75
/** The kernel matrix. */
76
protected Matrix m_KernelMatrix;
78
/** A classifier counter. */
79
protected int m_Counter;
82
* Returns a string describing the kernel
84
* @return a description suitable for displaying in the
85
* explorer/experimenter gui
87
public String globalInfo() {
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.";
97
* Returns an enumeration describing the available options.
99
* @return an enumeration of all the available options.
101
public Enumeration listOptions() {
104
result = new Vector();
106
Enumeration en = super.listOptions();
107
while (en.hasMoreElements())
108
result.addElement(en.nextElement());
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>"));
115
return result.elements();
119
* Parses a given list of options. <p/>
121
<!-- options-start -->
122
* Valid options are: <p/>
125
* Enables debugging output (if available) to be printed.
126
* (default: off)</pre>
129
* Turns off all checks - use with caution!
130
* (default: checks on)</pre>
132
* <pre> -M <file name>
133
* The file name of the file that holds the kernel matrix.
134
* (default: kernelMatrix.matrix)</pre>
138
* @param options the list of options as an array of strings
139
* @throws Exception if an option is not supported
141
public void setOptions(String[] options) throws Exception {
144
tmpStr = Utils.getOption('M', options);
145
if (tmpStr.length() != 0)
146
setKernelMatrixFile(new File(tmpStr));
148
setKernelMatrixFile(new File("kernelMatrix.matrix"));
150
super.setOptions(options);
154
* Gets the current settings of the Kernel.
156
* @return an array of strings suitable for passing to setOptions
158
public String[] getOptions() {
160
Vector<String> result;
163
result = new Vector<String>();
165
options = super.getOptions();
166
for (i = 0; i < options.length; i++)
167
result.add(options[i]);
170
result.add("" + getKernelMatrixFile());
172
return (String[]) result.toArray(new String[result.size()]);
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
183
public double eval(int id1, int id2, Instance inst1)
186
if (m_KernelMatrix == null) {
187
throw new IllegalArgumentException("Kernel matrix has not been loaded successfully.");
191
index1 = (int)m_data.instance(id1).value(0);
193
index1 = (int)inst1.value(0);
195
int index2 = (int)m_data.instance(id2).value(0);
196
return m_KernelMatrix.get(index1, index2);
200
* initializes variables etc.
202
* @param data the data to use
204
protected void initVars(Instances data) {
205
super.initVars(data);
208
if (m_KernelMatrix == null) {
209
m_KernelMatrix = new Matrix(new FileReader(m_KernelMatrixFile));
210
// System.err.println("Read kernel matrix.");
212
} catch (Exception e) {
213
System.err.println("Problem reading matrix from " + m_KernelMatrixFile);
216
// System.err.print("Building classifier: " + m_Counter + "\r");
220
* Returns the Capabilities of this kernel.
222
* @return the capabilities of this object
225
public Capabilities getCapabilities() {
226
Capabilities result = super.getCapabilities();
228
result.enable(Capability.NOMINAL_ATTRIBUTES);
229
result.enableAllClasses();
230
result.enable(Capability.MISSING_CLASS_VALUES);
236
* Sets the file holding the kernel matrix
238
* @param value the exponent value
240
public void setKernelMatrixFile(File f) {
241
m_KernelMatrixFile = f;
245
* Gets the file containing the kernel matrix.
247
* @return the exponent value
249
public File getKernelMatrixFile() {
250
return m_KernelMatrixFile;
254
* Returns the tip text for this property
256
* @return tip text for this property suitable for
257
* displaying in the explorer/experimenter gui
259
public String kernelMatrixFileTipText() {
260
return "The file holding the kernel matrix.";
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.
268
* @param km the kernel matrix to use
270
protected void setKernelMatrix(Matrix km) {
275
* returns a string representation for the Kernel
277
* @return a string representaiton of the kernel
279
public String toString() {
280
return "Using kernel matrix from file with name: " + getKernelMatrixFile();
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.
289
public void clean() {
294
* Returns the number of kernel evaluation performed.
296
* @return the number of kernel evaluation performed.
298
public int numEvals() {
303
* Returns the number of dot product cache hits.
305
* @return the number of dot product cache hits, or -1 if not supported by this kernel.
307
public int numCacheHits() {
312
* Returns the revision string.
314
* @return the revision
316
public String getRevision() {
317
return RevisionUtils.extract("$Revision: 1.1 $");