📄 mysvmmodel.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.FileModel;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.operator.UserError;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.example.ExampleReader;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.SkipNANExampleReader;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.ParameterService;
import edu.udo.cs.yale.tools.TempFileService;
import edu.udo.cs.yale.tools.Tools;
import edu.udo.cs.yale.tools.Ontology;
import java.io.*;
/** A <code>MySVMModel</code> stores a classification or regression model produced by a
* <code>MySVMLearner</code> and can apply this model to an <code>ExampleSet</code>
* in order to label the examples of this example set with their predicted labels.
* <p>
* For applying a stored <code>mySVM</code> model to an example set, a
* <code>MySVMModel</code> invokes the <code>predict</code> program of the external
* <a href="http://www-ai.cs.uni-dortmund.de/SOFTWARE/MYSVM/index.eng.html"><code>mySVM</code></a>
* program by
* <a href="http://www-ai.cs.uni-dortmund.de/PERSONAL/rueping.eng.html">Stefan Rüping</a>
* [Rüping/2000], an implementation of Vladimir Vapnik's Support Vector Machine (SVM)
* learning method from statistical learning theory [Vapnik/1998].
* The <code>mySVM</code> parameters are the same that were previously used by the
* <code>MySVMLearner</code> to learn the model and that are stored in the model.
* See the description of the <code>MySVMLearner</code> for further information.
* </p>
*
* <p>
* <i>Bibliography:</i><br>
* <b>[Rüping/2000]</b> Stefan Rüping.
* <a href="http://www-ai.cs.uni-dortmund.de/SOFTWARE/MYSVM/"><i>mySVM Manual</i></a>.
* <a href="http://www-ai.cs.uni-dortmund.de/">Artificial Intelligence Unit</a>,
* Computer Science Department, University of Dortmund, Dortmund, Germany, 2000.
* <a href="http://www-ai.cs.uni-dortmund.de/SOFTWARE/MYSVM/">http://www-ai.cs.uni-dortmund.de/SOFTWARE/MYSVM/</a>
* <br>
* <b>[Vapnik/1998]</b> Vladimir N. Vapnik.
* <i>Statistical Learning Theory</i>. Wiley, Chichester, UK, 1998.
* <br>
* </p>
*
* @author Ingo Mierswa, Ralf Klinkenberg
* @version $Id: MySVMModel.java,v 1.3 2004/08/27 11:57:40 ingomierswa Exp $
* @see edu.udo.cs.yale.operator.learner.kernel.MySVMLearner
* @see edu.udo.cs.yale.example.ExampleSet
*/
public class MySVMModel extends FileModel {
private boolean classificationTask;
private boolean weighted;
private String[] kernelParameters;
private String[] svmParameters;
public MySVMModel(Attribute label) throws IOException {
super(label);
}
public MySVMModel(Attribute label,
File modelFile,
boolean classificationTask,
boolean weighted,
String[] kernelParameters,
String[] svmParameters) throws IOException {
super(label, modelFile);
this.classificationTask = classificationTask;
this.weighted = weighted;
this.kernelParameters = kernelParameters;
this.svmParameters = svmParameters;
}
public void writeYaleData(ObjectOutputStream out) throws IOException {
out.writeBoolean(weighted);
out.writeBoolean(classificationTask);
out.writeInt(kernelParameters.length);
for (int i = 0; i < kernelParameters.length; i++)
out.writeUTF(kernelParameters[i]);
out.writeInt(svmParameters.length);
for (int i = 0; i < svmParameters.length; i++)
out.writeUTF(svmParameters[i]);
}
public void readYaleData(ObjectInputStream in) throws IOException {
weighted = in.readBoolean();
classificationTask = in.readBoolean();
kernelParameters = new String[in.readInt()];
for (int i = 0; i < kernelParameters.length; i++)
kernelParameters[i] = in.readUTF();
svmParameters = new String[in.readInt()];
for (int i = 0; i < svmParameters.length; i++)
svmParameters[i] = in.readUTF();
}
public void apply(ExampleSet exampleSet) throws OperatorException {
LogService.logMessage("MySVMModel '"+getName()+"': mySVM starts predicting labels ("
+exampleSet.getSize()+" examples).", LogService.TASK);
LogService.logMessage("MySVMModel '"+getName()+"': Using "+
(classificationTask ? "classification" : "regression")+" mode.",
LogService.MINIMUM);
File predictInputFile = TempFileService.createTempFile("svm_applier_examples_");
try {
LogService.logMessage("MySVMModel '"+getName()+"': Creating debug example file.", // RK/2003/05/05: TMP TEMP
LogService.MINIMUM);
PrintStream fileOut = new PrintStream(new FileOutputStream(predictInputFile));
writeMysvmPredictOutput(fileOut, exampleSet);
fileOut.close();
LogService.logMessage("MySVMModel '"+getName()+"': Test data file closed.", // RK/2003/05/05: TMP TEMP
LogService.MINIMUM);
} catch(IOException e) {
TempFileService.deleteTempFile(predictInputFile);
LogService.logException("MySVMModel '"+getName()+"': Cannot create new mySVM applier example file!", e);
}
Process process = null;
String command = null;
command = weighted ?
ParameterService.getProperty("yale.mysvm.weighted.applycommand") :
ParameterService.getProperty("yale.mysvm.applycommand");
LogService.logMessage("MySVMModel '" + getName() + "': Executing '" + command + "' in directory '" +
TempFileService.getTempDir() + "'", LogService.MINIMUM);
try {
//process = Runtime.getRuntime().exec(command, null, TempFileService.getTempDir());
process = Runtime.getRuntime().exec(new String[] {command, predictInputFile.getAbsolutePath() },
null, TempFileService.getTempDir());
} catch (IOException e) {
TempFileService.deleteTempFile(predictInputFile);
throw new UserError(null, e, 310, new Object[] {command, e});
}
// ---- IO ----
// PrintStream processOut = new PrintStream(process.getOutputStream());
// BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
// LogService.logMessage("MySVMModel '"+getName()+"': Writing test examples for mySVM applier...", // RK/2003/05/05: TMP TEMP
// LogService.MINIMUM);
// try {
// writeMysvmPredictOutput(processOut, exampleSet);
// } catch (IOException e) {
// throw new UserError(null, e, 309, "mysvm predict");
// }
// processOut.close();
// // get output
// String output = null;
// try {
// output = Tools.readOutput(in);
// if ((output==null) || (output.equals("")))
// LogService.logMessage("MySVMModel '"+getName()+"': "+
// "No output of mySVM.", LogService.WARNING);
// in.close();
// } catch (IOException e) {
// throw new UserError(null, e, 308, "mysvm predict");
// }
// try {
Tools.waitForProcess(null, process, "predict");
// } catch (OperatorException e) {
// LogService.logMessage("MySVMModel '"+getName()+"': "+
// "Output of mySVM's predict:\n"+output, LogService.MINIMUM);
// throw e;
// }
// ---- set predicted labels ----
LogService.logMessage("MySVMModel '"+getName()+"': Setting predicted labels...", // RK/2003/05/05: TMP TEMP
LogService.MINIMUM);
File predLabelsFile = null;
// predLabelsFile = TempFileService.createTempFile("mysvm_predicted_labels",
// new File(TempFileService.getTempDir(), "mysvm.pred"));
predLabelsFile = new File(predictInputFile.getAbsolutePath()+".pred");
try {
setPredictedLabels(exampleSet, predLabelsFile, classificationTask);
} catch (IOException e) {
throw new UserError(null, e, 302, new Object[] { predLabelsFile, e.getMessage()});
} finally {
TempFileService.deleteTempFile(predictInputFile);
TempFileService.deleteTempFile(predLabelsFile);
}
}
private void writeMysvmPredictOutput(PrintStream out, ExampleSet exampleSet) throws IOException, OperatorException {
LogService.logMessage("MySVMModel '"+getName()+"': Writing file header with parameters...", // RK/2003/05/05: TMP TEMP
LogService.MINIMUM);
out.println("@kernel");
for (int i = 0; i < kernelParameters.length; i++)
out.println(kernelParameters[i]);
out.println("@parameters");
for (int i = 0; i < svmParameters.length; i++)
out.println(svmParameters[i]);
out.println(classificationTask ? "pattern" : "regression");
LogService.logMessage("MySVMModel '"+getName()+"': Writing mySVM model...", // RK/2003/05/05: TMP TEMP
LogService.MINIMUM);
BufferedReader modelIn = new BufferedReader(getReader());
writeModel(modelIn, out);
modelIn.close();
LogService.logMessage("MySVMModel '"+getName()+"': Writing examples...", // RK/2003/05/05: TMP TEMP
LogService.MINIMUM);
MySVMLearner.writeExamples(exampleSet, out, MySVMLearner.SVM_APPLIER, weighted, true, classificationTask);
}
/** Writes the model to the mySVM predict process.
*/
private void writeModel(BufferedReader reader, PrintStream out) throws OperatorException {
out.println("@examples");
LogService.logMessage("MySVMModel '"+getName()+"': method 'writeModel()': "+ // RK/2003/05/05: TMP TEMP
"Reading mySVM from '"+reader+"' and writting it to '"+ // RK/2003/05/05: TMP TEMP
out+"'...", LogService.MINIMUM); // RK/2003/05/05: TMP TEMP
String line = "";
long numberOfLines = 0; // RK/2003/05/05: TMP TEMP
try {
while ((line=reader.readLine())!=null) { // RK/2003/05/05: TMP TEMP
if ((numberOfLines < 10) || (numberOfLines % 10 == 0)) // RK/2003/05/05: TMP TEMP
LogService.logMessage("MySVMModel '"+getName()+"': method 'writeModel()': "+ // RK/2003/05/05: TMP TEMP
numberOfLines+" lines of the model written.", // RK/2003/05/05: TMP TEMP
LogService.MINIMUM); // RK/2003/05/05: TMP TEMP
numberOfLines++; // RK/2003/05/05: TMP TEMP
out.println(line);
out.flush(); // RK/2003/05/05: line added
}
} catch (IOException e) {
throw new UserError(null, e, 302, new Object[] {"svm model file",e.getMessage()});
}
}
/** Sets the predicted labels of the examples of the given example set.
*/
private void setPredictedLabels(ExampleSet exampleSet, File labels, boolean classificationTask) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(labels));;
ExampleReader r = new SkipNANExampleReader(exampleSet.getExampleReader());
String line = "";
// throw away first line of output
reader.readLine();
while (((line=reader.readLine())!=null) && (r.hasNext())) {
double readLabel = Double.NaN;
try {
readLabel = Double.parseDouble(line);
} catch (NumberFormatException e) {
LogService.logMessage("MySVMModel '"+getName()+"': "+
"Read 'NaN' (Not a Number) from SVM", LogService.WARNING);
}
if (classificationTask) {
r.next().setPredictedLabel((readLabel >= 0) ?
Attribute.FIRST_CLASS_INDEX :
Attribute.FIRST_CLASS_INDEX + 1);
} else {
r.next().setPredictedLabel(readLabel);
}
}
}
public boolean equals(Object o) {
if (!super.equals(o)) return false;
MySVMModel other = (MySVMModel)o;
if (this.classificationTask != other.classificationTask) return false;
if (this.weighted != other.weighted) return false;
if (this.kernelParameters.length != other.kernelParameters.length) return false;
for (int i = 0; i < this.kernelParameters.length; i++) {
if (!this.kernelParameters[i].equals(other.kernelParameters[i])) return false;
}
if (this.svmParameters.length != other.svmParameters.length) return false;
for (int i = 0; i < this.svmParameters.length; i++) {
if (!this.svmParameters[i].equals(other.svmParameters[i])) return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -