libsvm.svm.svm_save_model()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(259)

本文整理了Java中libsvm.svm.svm_save_model()方法的一些代码示例,展示了svm.svm_save_model()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。svm.svm_save_model()方法的具体详情如下:
包路径:libsvm.svm
类名称:svm
方法名:svm_save_model

svm.svm_save_model介绍

暂无

代码示例

代码示例来源:origin: prestodb/presto

@Override
public byte[] getSerializedData()
{
  File file = null;
  try {
    // libsvm doesn't have a method to serialize the model into a buffer, so write it out to a file and then read it back in
    file = File.createTempFile("svm", null);
    svm.svm_save_model(file.getAbsolutePath(), model);
    return Files.readAllBytes(file.toPath());
  }
  catch (IOException e) {
    throw new UncheckedIOException(e);
  }
  finally {
    if (file != null) {
      //noinspection ResultOfMethodCallIgnored
      file.delete();
    }
  }
}

代码示例来源:origin: jzy3d/jzy3d-api

public void save(String filename) throws IOException{
  svm.svm_save_model(filename, model);
}

代码示例来源:origin: jzy3d/jzy3d-api

public void save(String directory, String filename) throws IOException{
  File dir = new File( directory );
  if( ! dir.exists() )
    dir.mkdirs();
  svm.svm_save_model(directory + filename, model);
}

代码示例来源:origin: prestosql/presto

@Override
public byte[] getSerializedData()
{
  File file = null;
  try {
    // libsvm doesn't have a method to serialize the model into a buffer, so write it out to a file and then read it back in
    file = File.createTempFile("svm", null);
    svm.svm_save_model(file.getAbsolutePath(), model);
    return Files.readAllBytes(file.toPath());
  }
  catch (IOException e) {
    throw new UncheckedIOException(e);
  }
  finally {
    if (file != null) {
      //noinspection ResultOfMethodCallIgnored
      file.delete();
    }
  }
}

代码示例来源:origin: eu.fbk.utils/utils-svm

@Override
void doWrite(final Path path) throws IOException {
  // Write the dictionary
  this.dictionary.writeTo(path.resolve("dictionary"));
  // Write the model
  final File tmpFile = File.createTempFile("svm", ".bin");
  tmpFile.deleteOnExit();
  svm.svm_save_model(tmpFile.getAbsolutePath(), this.model);
  final String modelString = com.google.common.io.Files.toString(tmpFile,
      Charset.defaultCharset());
  tmpFile.delete();
  Files.write(path.resolve("model"), modelString.getBytes(Charsets.UTF_8));
}

代码示例来源:origin: org.maochen.nlp/CoreNLP-NLP

@Override
public void persistModel(String modelFile) throws IOException {
  if (this.labelIndexer == null) {
    throw new RuntimeException("LabelIndexer is null!");
  }
  ZipOutputStream zipos = new ZipOutputStream(new FileOutputStream(modelFile));
  String svmModelAbsolutePath = modelFile + ".model";
  String svmModelFilename = new File(svmModelAbsolutePath).getName();
  svm.svm_save_model(svmModelAbsolutePath, model);
  ZipEntry libSVMModelZipEntry = new ZipEntry(svmModelFilename);
  zipos.putNextEntry(libSVMModelZipEntry);
  IOUtils.copy(new FileInputStream(svmModelAbsolutePath), zipos);
  zipos.closeEntry();
  String labelIndexerAbsolutePath = modelFile + ".lbindexer";
  String labelIndexerFileName = new File(labelIndexerAbsolutePath).getName();
  String labelIndexerString = this.labelIndexer.serializeToString();
  ZipEntry labelIndexerZipEntry = new ZipEntry(labelIndexerFileName);
  zipos.putNextEntry(labelIndexerZipEntry);
  IOUtils.write(labelIndexerString, zipos, Charset.defaultCharset());
  zipos.closeEntry();
  IOUtils.closeQuietly(zipos);
  FileUtils.forceDelete(new File(svmModelAbsolutePath));
}

代码示例来源:origin: jatecs/jatecs

public void write(String modelDir, IClassifier learningData)
    throws IOException {
  SvmClassifier cl = (SvmClassifier) learningData;
  File f = new File(modelDir);
  f.mkdirs();
  String vc = modelDir + Os.pathSeparator() + "validCategories.db";
  DataOutputStream valid_os = new DataOutputStream(
      new BufferedOutputStream(new FileOutputStream(vc), 4096));
  valid_os.writeInt(cl.getCategoryCount());
  // Write categories vectors and ranges.
  for (short catID = 0; catID < cl.getCategoryCount(); catID++) {
    valid_os.writeShort(catID);
    String filename = modelDir + Os.pathSeparator() + catID + ".db";
    svm.svm_save_model(filename, cl._models[catID]);
  }
  valid_os.close();
}

代码示例来源:origin: jatecs/jatecs

public void write(String modelDir, IClassifier learningData)
    throws IOException {
  SvmRegressionClassifier cl = (SvmRegressionClassifier) learningData;
  File f = new File(modelDir);
  f.mkdirs();
  String vc = modelDir + Os.pathSeparator() + "validCategories.db";
  DataOutputStream valid_os = new DataOutputStream(
      new BufferedOutputStream(new FileOutputStream(vc), 4096));
  valid_os.writeInt(cl.getCategoryCount());
  // Write categories vectors and ranges.
  for (short catID = 0; catID < cl.getCategoryCount(); catID++) {
    valid_os.writeShort(catID);
    String filename = modelDir + Os.pathSeparator() + catID + ".db";
    svm.svm_save_model(filename, cl.model());
  }
  valid_os.close();
}

代码示例来源:origin: pl.edu.icm.yadda/yadda-analysis-impl

@Override
public void saveModel(String modelPath) throws IOException
{
  Formatter formatter = new Formatter(new StringBuilder());
  BufferedWriter fp_save;
  fp_save = new BufferedWriter(new FileWriter(modelPath + ".range"));
  
  Double lower = 0.0;
  Double upper = 1.0;
  formatter.format("x\n");
  formatter.format("%.16g %.16g\n", lower, upper);
  for(Integer i=0 ; i<featureVectorBuilder.size() ; ++i) {
    formatter.format("%d %.16g %.16g\n", i, scaler.getLimits()[i].getMin(), scaler.getLimits()[i].getMax());
  }
  
  fp_save.write(formatter.toString());
  fp_save.close();
  
  svm.svm_save_model(modelPath, model);
}

代码示例来源:origin: eu.fbk.utils/utils-svm

private static Classifier trainJava(final Parameters parameters,
    final Iterable<LabelledVector> trainingSet) throws IOException {
  // Prepare the svm_parameter object based on supplied parameters
  final svm_parameter parameter = encodeParameters(parameters);
  // Encode the training set as an svm_problem object, filling a dictionary meanwhile
  final Dictionary<String> dictionary = Dictionary.create();
  final svm_problem problem = encodeProblem(dictionary, trainingSet);
  // Perform training
  final svm_model model = svm.svm_train(problem, parameter);
  // Compute model hash, by saving and reloading SVM model
  final File tmpFile = File.createTempFile("svm", ".bin");
  tmpFile.deleteOnExit();
  svm.svm_save_model(tmpFile.getAbsolutePath(), model);
  final String modelString = com.google.common.io.Files.toString(tmpFile,
      Charset.defaultCharset());
  final String modelHash = computeHash(dictionary, modelString);
  final svm_model reloadedModel = svm
      .svm_load_model(new BufferedReader(new StringReader(modelString)));
  tmpFile.delete();
  // Build and return the SVM object
  return new LibSvmClassifier(parameters, modelHash, dictionary, reloadedModel);
}

代码示例来源:origin: jatecs/jatecs

File f = new File(filename);
f.getParentFile().mkdirs();
svm.svm_save_model(filename, cl.model());

代码示例来源:origin: dkpro/dkpro-tc

public void run(String argv[]) throws Exception
{
  parse_command_line(argv);
  read_problem();
  error_msg = svm.svm_check_parameter(prob, param);
  if (error_msg != null) {
    throw new Exception(error_msg);
  }
  model = svm.svm_train(prob, param);
  svm.svm_save_model(model_file_name, model);
}

代码示例来源:origin: ClearTK/cleartk

private void run(String argv[]) throws IOException {
 parse_command_line(argv);
 read_problem();
 error_msg = svm.svm_check_parameter(prob, param);
 if (error_msg != null) {
  System.err.print("ERROR: " + error_msg + "\n");
  System.exit(1);
 }
 if (cross_validation != 0) {
  do_cross_validation();
 } else {
  model = svm.svm_train(prob, param);
  svm.svm_save_model(model_file_name, model);
 }
}

代码示例来源:origin: chungkwong/MathOCR

@Override
public void write(SvmModel model,String fileName) throws IOException{
  saveFeatureList(model.getFeatures(),fileName);
  svm.svm_save_model(fileName,model.getModel());
}
@Override

代码示例来源:origin: org.dkpro.tc/dkpro-tc-ml-libsvm

public void run(String argv[]) throws Exception
{
  parse_command_line(argv);
  read_problem();
  error_msg = svm.svm_check_parameter(prob, param);
  if (error_msg != null) {
    throw new Exception(error_msg);
  }
  model = svm.svm_train(prob, param);
  svm.svm_save_model(model_file_name, model);
}

代码示例来源:origin: ch.epfl.bbp.nlp/bluima_jsre

public void run(File input_file, File model_file, double c, int mem, double[] weight) throws IOException
{
  input_file_name = input_file.getAbsolutePath();
  model_file_name = model_file.getAbsolutePath();
  //System.out.println("input_file_name: " + input_file_name);
  //System.out.println("model_file_name: " + model_file_name);
  //System.out.println("mem: " + mem);
  set_param(c, mem, weight);
  read_problem();
  error_msg = svm.svm_check_parameter(prob,param);
  if(error_msg != null)
  {
    System.err.print("Error: "+error_msg+"\n");
    System.exit(1);
  }
  if(cross_validation != 0)
  {
    //do_cross_validation();
  }
  else
  {
    model = svm.svm_train(prob,param);
    svm.svm_save_model(model_file_name, model);
  }
}

代码示例来源:origin: DigitalPebble/TextClassification

public void internal_learn() throws Exception {
  // dumps a file with the vectors for the documents
  File learningFile = new File(this.vector_location);
  // make space
  parse_command_line();
  if (cross_validation && nfold < 2)
    throw new Exception("n-fold cross validation: n must >= 2\n");
  read_problem(learningFile);
  error_msg = svm.svm_check_parameter(prob, param);
  if (error_msg != null) {
    System.err.print("Error: " + error_msg + "\n");
    throw new Exception(error_msg);
  }
  if (cross_validation) {
    do_cross_validation();
  } else {
    model = svm.svm_train(prob, param);
    svm.svm_save_model(model_file_name, model);
  }
}

代码示例来源:origin: org.cleartk/cleartk-ml-libsvm

private void run(String argv[]) throws IOException {
 parse_command_line(argv);
 read_problem();
 error_msg = svm.svm_check_parameter(prob, param);
 if (error_msg != null) {
  System.err.print("ERROR: " + error_msg + "\n");
  System.exit(1);
 }
 if (cross_validation != 0) {
  do_cross_validation();
 } else {
  model = svm.svm_train(prob, param);
  svm.svm_save_model(model_file_name, model);
 }
}

代码示例来源:origin: openimaj/openimaj

/**
 *    {@inheritDoc}
 *     @see org.openimaj.ml.training.BatchTrainer#train(java.util.List)
 */
@Override
public void train( final List<? extends Annotated<OBJECT, ANNOTATION>> data )
{
  // Check the data has 2 classes and update the class map.
  if( this.checkInputDataOK( data ) )
  {
    // Setup the SVM problem
    final svm_parameter param = SVMAnnotator.getDefaultSVMParameters();
    final svm_problem prob = this.getSVMProblem( data, param, this.extractor );
    // Train the SVM
    this.model = libsvm.svm.svm_train( prob, param );
    // Save the model if we're going to do that.
    if( this.saveModel != null ) try
    {
      svm.svm_save_model( this.saveModel.getAbsolutePath(), this.model );
    }
    catch( final IOException e )
    {
      e.printStackTrace();
    }
  }
}

代码示例来源:origin: jatecs/jatecs

svm.svm_save_model(filename, cl._models[catID]);

相关文章