⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysvmlearner.java

📁 著名的开源仿真软件yale
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
  	try {  	    PrintStream fileOut = new PrintStream(new FileOutputStream(svmInputFile));	    writeParameters(fileOut);	    LogService.logMessage("MySVMLearner '"+getName()+"': Writing examples in mySVM format to file",				  LogService.MINIMUM);	    writeExamples(exampleSet, fileOut, SVM_LEARNER, weighted, sparse, classificationTask);	    LogService.logMessage("MySVMLearner '"+getName()+"': Examples in mySVM format written to file.",				  LogService.MINIMUM);	    fileOut.close();  	} catch(IOException e) {   	    LogService.logException("MySVMLearner '" + getName() + "': Cannot create new mySVM learner example file!", e); 	    TempFileService.deleteTempFile(svmInputFile);  	}	// ---- IO (mySVM parameters and training via StdIn, mySVM output via StdOut, and mySVM model via file) ----	Process  process  = null;	try {	    //process = Runtime.getRuntime().exec(command, null, TempFileService.getTempDir());  // RK/2002/05/22	    process = Runtime.getRuntime().exec(new String[] { command, svmInputFile.getAbsolutePath() },						null,						TempFileService.getTempDir());                   // RK/2002/05/22	} catch (IOException e) {	    TempFileService.deleteTempFile(svmInputFile);	    throw new FatalException("MySVMLearner '"+getName()+"': Cannot find temporary directory '" +				     TempFileService.getTempDir() + "' and/or mySVM executable 'mysvm' ('" +				     command + "')", e);	}//  	BufferedReader  in         = new BufferedReader(new InputStreamReader(process.getInputStream()));//  	PrintStream     processOut = new PrintStream(process.getOutputStream());	//  	// ---- write mySVM Input ----//  	writeParameters(processOut);//  	LogService.logMessage("MySVMLearner '"+getName()+"': Writing examples in mySVM format to process...",//RK/2003/05/05:TMP//  			      LogService.MINIMUM);//  	writeExamples(exampleSet, processOut, SVM_LEARNER, weighted, sparse, classificationTask);//  	LogService.logMessage("MySVMLearner '"+getName()+"': Closing process output stream...",            // RK/2003/05/05: TMP//  			      LogService.MINIMUM);//  	processOut.close();//  	LogService.logMessage("MySVMLearner '"+getName()+"': Examples in mySVM format written to process.",// RK/2003/05/05: TMP//  			      LogService.MINIMUM);//  	// ---- get mySVM Output ----//  	String output = null;//  	try {//  	    output = Tools.readOutput(in);//  	    if ((output==null) || (output.equals(""))) {//  		throw new UserError(this, 307, "mySVM");//  	    } //  	    in.close();//    	} catch (IOException e) {//    	    throw new UserError(this, e, 308, "mySVM");//    	}//  	try {	Tools.waitForProcess(this, process, "mysvm");//  	} catch (OperatorException e) {//  	    LogService.logMessage("Output of mySVM's mysvm (Learner: '"+getName()+"')\n"+output,//  				  LogService.ERROR);//  	    throw e;//  	}	//File svmFile = new File(TempFileService.getTempDir(), "mysvm.svm");	File svmFile = new File(svmInputFile.getAbsolutePath()+".svm");	LogService.logMessage("MySVMLearner '"+getName()+"': mySVM has succesfully " 			      +"learned from the example set.", LogService.TASK);         	// ---- get xi-alpha-performance estimation (error and accuracy (not yet precision and recall)) ----	// ---- and add the estimated performance values to the output of the operator                  ----	boolean useXiAlpha = getParameterAsBoolean("xi_alpha_estimation");	if (useXiAlpha) {	    if (classificationTask) {		LogService.logMessage("MySVMLearner '" + getName() + "': The xi-alpha-values computed by "+				      "mySVM are read in for the computation of the xi-alpha-criterion...", 				      LogService.MINIMUM);                //// 2003/07/30: old version: data piped to mySVM                 //// 2003/07/30:              => mySVM stores xi-alpha estimations in file 'mysvm.xialpha'                // performanceEstimation = scanXiAlphaValues(exampleSet);                                       // 2003/07/30: old                //// 2003/07/30: new version: mySVM reads data from file 'xyz'                ////                          => mySVM stores xi-alpha estimations in file 'xyz.xialpha'                performanceEstimation = scanXiAlphaValues(exampleSet,                                           // 2003/07/30: new							  new File(svmInputFile.getAbsolutePath()+".xialpha")); // 2003/07/30: new		LogService.logMessage("MySVMLearner '" + getName() + "': The xi-alpha-criterion has been "+				      "successfully computed.", LogService.MINIMUM);	    } else {		LogService.logMessage("MySVMLearner '" + getName() + "': parameter 'xi_alpha_estimation' " +				      "is set to true, but parameter 'task_type' is not set to 'pattern'. " +				      "Xi-alpha-estimation may only be performed for classification tasks " +				      "and hence is not performed here. " +				      "If a consecutive or encapsulating operator expects the results of this " +				      "estimation, the operator chain will fail to work properly.",				      LogService.WARNING);	    }	}	try {	    return new MySVMModel(svmFile,				  classificationTask,				  weighted,				  kernelParameters,				  svmParameters);	} catch (IOException e) {	    throw new UserError(this, e, 302, new Object[] {svmFile, e.getMessage()});  // ?? comments ?? 302 ??	} finally {	    TempFileService.deleteTempFile(svmInputFile);	    TempFileService.deleteTempFile(svmFile);	}    }    /** Checks all mySVM parameters in the experiment configuration file and     *  returns them as a single string. As a side effect, it sets the values of     *  {@link #svmParameters} and {@link #kernelParameters}. */    private void writeParameters(PrintStream out) {	List l = new LinkedList();		out.println("@kernel");	for (int i = 0; i < KERNEL_PARAMETER.length; i++) {	    Object param = getParameter(KERNEL_PARAMETER[i]);   	    if (param != null) {		out.println(KERNEL_PARAMETER[i] + " " + param);		l.add(KERNEL_PARAMETER[i]+" "+param);	    }	}	kernelParameters = new String[l.size()];	l.toArray(kernelParameters);	l.clear();	out.println("@parameters");	out.println(classificationTask?"pattern":"regression");	for (int i = 0; i < PARAMETER.length; i++) {	    String key = PARAMETER[i].getKey();	    if (isParameterSet(key)) {		String paramLine = null;		if (PARAMETER[i] instanceof ParameterTypeBoolean) {		    if (getParameterAsBoolean(key)) {			paramLine = key;		    }		} else if (PARAMETER[i] instanceof ParameterTypeCategory) {		    int value = getParameterAsInt(key);		    if (value > 0) {			paramLine = key + " " + ((ParameterTypeCategory)PARAMETER[i]).getCategory(value);		    }		} else {		    paramLine = key + " " + getParameter(key).toString();		}		if (paramLine != null) {		    l.add(paramLine);		    out.println(paramLine);		}	    }	}	if (getParameterAsBoolean("scale")) l.add("scale");	else{	    l.add("no_scale");	    out.println("no_scale");	};	svmParameters = new String[l.size()];	l.toArray(svmParameters);    }    /** writes the given example set to the speicified output <tt>PrintStream</tt>     *  using the mySVM data format.     *  The parameter <tt>type</tt> specifies, wether the data is to be written as     *  training or test data file (<tt>SVM_LEARNER</tt> or <tt>SVM_APPLIER</tt>, i.e.     *  for usage by a <tt>MySVMLearner</tt> and a <tt>ModelApplier</tt> respectively).     *  If <tt>weighted</tt> is <tt>true</tt>, the examples are printed out with weights.     *  If <tt>sparse</tt> is <tt>true</tt>, the sparse mySVM data format is used.     *  If <tt>classificationTask</tt> is <tt>true</tt>, the labels are written out for      *  a classification task (as <tt>+1</tt> and <tt>-1</tt>). Otherwise they are written     *  out for a regression task (without translation).     */    protected static void writeExamples(ExampleSet exampleSet, 					PrintStream out, 					int type, 					boolean weighted, 					boolean sparse,					boolean classificationTask) 	throws OperatorException {	if (weighted && !sparse) {	    LogService.logMessage("MySVMLearner.writeExamples(): mySVM can handle weights only in "+				  "sparse format. Sparsifying.", LogService.WARNING);	    sparse = true;	}		//ExampleReader  r = new SkipNANExampleReader(exampleSet.getExampleReader());	ExampleReader r = exampleSet.getExampleReader();       	out.println("@examples");	if (type == SVM_LEARNER) {	    out.println(sparse ? "format sparse" : "format xy");	} else {	    out.println(sparse ? "format sparse" : "format x");	}	int  numberOfExamples = 0;	LogService.logMessage("MySVMLearner.writeExamples(): mySVM example file header written. "			      +"Writting examples...", LogService.MINIMUM);	while (r.hasNext()) {	    // ---- read next example ----	    Example  example = (Example)r.next();	    numberOfExamples++;	    // ---- write the weight of the example into the first column (if the example is weighted)  ----	    String weightString = "";	    if (weighted) {		double weight = example.getWeight();		if (weight == 0.0) continue;		else weightString = "L+:" + weight + " L-:" + weight + " ";	    } 	    // ---- read attribute values and write them ----	    String  attributes = sparse ? 		example.getAttributesAsSparseString(" ", ":") : 		example.getAttributesAsString(" ");	    String  labelString;	    double  labelIndex;	    if (type == SVM_APPLIER) { 		labelString = "";                                                       // SVM-Model-Applier needs no label	    } else {		if (sparse) { labelString = " y:"; }		else { labelString = " "; }		if (!classificationTask) { 		    labelString += example.getLabel();                                  // Regression: use original label		} else {                                                                // Classification: use labels +1 and -1		    labelIndex = example.getLabel();		    if (labelIndex == positiveLabelIndex)   { labelString += "+1"; }    // positive class		    else if (labelIndex == unlabelledIndex) { labelString += "0";  }    // unlabeled (no class defined)		    else                                    { labelString += "-1"; }    // negative class		}	    }	    out.println(weightString + attributes + labelString);	    if((numberOfExamples < 10) || (numberOfExamples % 10 == 0))		LogService.logMessage("MySVMLearner.writeExamples(): "+numberOfExamples+				      " examples written.", LogService.MINIMUM);	}	LogService.logMessage("MySVMLearner.writeExamples(): mySVM example file written. ",			      LogService.MINIMUM);	if ((numberOfExamples < 2) && (type == SVM_LEARNER)) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -