📄 abstractmysvmlearner.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.operator.learner.kernel;
import edu.udo.cs.yale.operator.learner.AbstractLearner;
import edu.udo.cs.yale.operator.learner.Model;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.AttributeWeights;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.mySVM.SVM.*;
import edu.udo.cs.mySVM.Kernel.*;
import java.util.List;
/** This is the abstract superclass for the support vector machine / KLR implementations
* of Stefan Rüping.
*
* @yale.reference Rueping/2000a
* @yale.reference Vapnik/98a
* @yale.index SVM
*
* @version $Id: AbstractMySVMLearner.java,v 1.3 2004/08/27 11:57:39 ingomierswa Exp $
*/
public abstract class AbstractMySVMLearner extends AbstractLearner {
/** The kernels which can be used from Yale for the mySVM / myKLR. */
private static final String[] KERNEL_TYPES = { "dot","radial","polynomial", "neural" };
/** Indicates a linear kernel. */
public static final int KERNEL_DOT = 0;
/** Indicates a rbf kernel. */
public static final int KERNEL_RADIAL = 1;
/** Indicates a polynomial kernel. */
public static final int KERNEL_POLYNOMIAL = 2;
/** Indicates a neural net kernel. */
public static final int KERNEL_NEURAL = 3;
/** The SVM which is used for learning. */
private SVMInterface svm = null;
/** The SVM kernel. */
private Kernel kernel;
/** Returns the kernel of this SVM. */
protected Kernel getKernel() {
return kernel;
}
/** Returns the used SVM. */
protected SVMInterface getSVM() {
return svm;
}
/** Returns true for linear kernels. */
public boolean canCalculateWeights() {
return ((getParameterAsInt("kernel_type") == KERNEL_DOT) && getParameterAsBoolean("create_weights"));
}
/** Returns the weights for all features. */
public AttributeWeights getWeights(ExampleSet exampleSet) {
double[] weights = svm.getWeights();
AttributeWeights weightVector = new AttributeWeights();
for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++)
weightVector.setWeight(exampleSet.getAttribute(i).getName(), weights[i]);
return weightVector;
}
/** Creates a new SVM according to the given label. */
public abstract SVMInterface createSVM(Attribute label,
Kernel kernel,
edu.udo.cs.mySVM.Examples.ExampleSet svmExamples) throws OperatorException;
/** Creates a new SVM model from the given data. */
public abstract AbstractMySVMModel createSVMModel(Attribute label,
edu.udo.cs.mySVM.Examples.ExampleSet svmExamples,
Kernel kernel,
int kernelType);
public Model learn(ExampleSet exampleSet) throws OperatorException {
edu.udo.cs.mySVM.Examples.ExampleSet svmExamples =
new edu.udo.cs.mySVM.Examples.ExampleSet(exampleSet, getParameterAsBoolean("scale"));
// kernel
int kernelType = getParameterAsInt("kernel_type");
int cacheSize = getParameterAsInt("kernel_cache");
kernel = createKernel(kernelType);
if (kernelType == KERNEL_RADIAL) ((KernelRadial)kernel).setGamma(getParameterAsDouble("kernel_gamma"));
else if (kernelType == KERNEL_POLYNOMIAL) ((KernelPolynomial)kernel).setDegree(getParameterAsInt("kernel_degree"));
else if (kernelType == KERNEL_NEURAL) ((KernelNeural)kernel).setParameter(getParameterAsDouble("kernel_a"),
getParameterAsDouble("kernel_b"));
kernel.init(svmExamples, cacheSize);
// SVM
Attribute label = exampleSet.getLabel();
svm = createSVM(label, kernel, svmExamples);
svm.init(kernel, svmExamples);
svm.train();
return createSVMModel(exampleSet.getLabel(), svmExamples, kernel, kernelType);
}
public List getParameterTypes() {
List types = super.getParameterTypes();
ParameterType type = new ParameterTypeCategory("kernel_type", "The SVM kernel type", KERNEL_TYPES, 0);
type.setExpert(false);
types.add(type);
type = new ParameterTypeDouble("kernel_gamma", "The SVM kernel parameter gamma (radial).", 0.0d,
Double.POSITIVE_INFINITY, 1.0d);
type.setExpert(false);
types.add(type);
type = new ParameterTypeInt("kernel_degree", "The SVM kernel parameter degree (polynomial).", 0, Integer.MAX_VALUE, 2);
type.setExpert(false);
types.add(type);
type = new ParameterTypeDouble("kernel_a", "The SVM kernel parameter a (neural).",
Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0d);
type.setExpert(false);
types.add(type);
type = new ParameterTypeDouble("kernel_b", "The SVM kernel parameter b (neural).",
Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0.0d);
type.setExpert(false);
types.add(type);
types.add(new ParameterTypeInt("kernel_cache", "Size of the cache for kernel evaluations im MB ", 0, Integer.MAX_VALUE, 200));
type = new ParameterTypeDouble("C", "The SVM complexity constant. Use -1 for different C values for positive and negative.",
Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0.0d);
types.add(type);
type = new ParameterTypeDouble("convergence_epsilon", "Precision on the KKT conditions",
0.0d, Double.POSITIVE_INFINITY, 1e-3);
types.add(type);
types.add(new ParameterTypeInt("max_iterations", "Stop after this many iterations", 1, Integer.MAX_VALUE, 100000));
types.add(new ParameterTypeBoolean("scale", "Scale the example values and store the scaling parameters for test set.", true));
types.add(new ParameterTypeBoolean("create_weights", "Indicates if weights should be created additionally to the model", false));
return types;
}
/** Creates a new kernel of the given type. The kernel type has to be one out of KERNEL_DOT, KERNEL_RADIAL,
* KERNEL_POLYNOMIAL, or KERNEL_NEURAL. */
public static Kernel createKernel(int kernelType) {
switch (kernelType) {
case KERNEL_RADIAL:
return new KernelRadial();
case KERNEL_POLYNOMIAL:
return new KernelPolynomial();
case KERNEL_NEURAL:
return new KernelNeural();
default:
return new KernelDot();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -