Package de.bwaldvogel.liblinear

Examples of de.bwaldvogel.liblinear.Parameter


      throw new LiblinearException("The liblinear bias value is not numerical value. ", e);
    }
  }

  public Parameter getLiblinearParameters() throws MaltChainedException {
    Parameter param = new Parameter(SolverType.MCSVM_CS, 0.1, 0.1);
    String type = liblinearOptions.get("s");
   
    if (type.equals("0")) {
      param.setSolverType(SolverType.L2R_LR);
    } else if (type.equals("1")) {
      param.setSolverType(SolverType.L2R_L2LOSS_SVC_DUAL);
    } else if (type.equals("2")) {
      param.setSolverType(SolverType.L2R_L2LOSS_SVC);
    } else if (type.equals("3")) {
      param.setSolverType(SolverType.L2R_L1LOSS_SVC_DUAL);
    } else if (type.equals("4")) {
      param.setSolverType(SolverType.MCSVM_CS);
    } else if (type.equals("5")) {
      param.setSolverType(SolverType.L1R_L2LOSS_SVC)
    } else if (type.equals("6")) {
      param.setSolverType(SolverType.L1R_LR)
    } else {
      throw new LiblinearException("The liblinear type (-s) is not an integer value between 0 and 4. ");
    }
    try {
      param.setC(Double.valueOf(liblinearOptions.get("c")).doubleValue());
    } catch (NumberFormatException e) {
      throw new LiblinearException("The liblinear cost (-c) value is not numerical value. ", e);
    }
    try {
      param.setEps(Double.valueOf(liblinearOptions.get("e")).doubleValue());
    } catch (NumberFormatException e) {
      throw new LiblinearException("The liblinear epsilon (-e) value is not numerical value. ", e);
    }
    return param;
  }
View Full Code Here


    }
    double[] wmodel = null;
    int[] labels = null;
    int nr_class = 0;
    int nr_feature = 0;
    Parameter parameter = getLiblinearParameters();
    try
      Problem problem = readProblem(getInstanceInputStreamReader(".ins"));
      boolean res = checkProblem(problem);
      if (res == false) {
        throw new LibException("Abort (The number of training instances * the number of classes) > "+Integer.MAX_VALUE+" and this is not supported by LibLinear. ");
      }
      if (configLogger.isInfoEnabled()) {
        owner.getGuide().getConfiguration().getConfigLogger().info("- Train a parser model using LibLinear.\n");
      }
      final PrintStream out = System.out;
      final PrintStream err = System.err;
      System.setOut(NoPrintStream.NO_PRINTSTREAM);
      System.setErr(NoPrintStream.NO_PRINTSTREAM);
      Model model = Linear.train(problem, parameter);
      System.setOut(err);
      System.setOut(out);
      problem = null;
      wmodel = model.getFeatureWeights();
      labels = model.getLabels();
      nr_class = model.getNrClass();
      nr_feature = model.getNrFeature();
      if (!saveInstanceFiles) {
        getFile(".ins").delete();
      }
    } catch (OutOfMemoryError e) {
      throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e);
    } catch (IllegalArgumentException e) {
      throw new LibException("The Liblinear learner was not able to redirect Standard Error stream. ", e);
    } catch (SecurityException e) {
      throw new LibException("The Liblinear learner cannot remove the instance file. ", e);
    } catch (NegativeArraySizeException e) {
      throw new LibException("(The number of training instances * the number of classes) > "+Integer.MAX_VALUE+" and this is not supported by LibLinear.", e);
    }
   
    if (configLogger.isInfoEnabled()) {
      configLogger.info("- Optimize the memory usage\n");
    }
    MaltLiblinearModel xmodel = null;
    try {
//      System.out.println("Nr Features:" +  nr_feature);
//      System.out.println("nr_class:" + nr_class);
//      System.out.println("wmodel.length:" + wmodel.length);   
      double[][] wmatrix = convert2(wmodel, nr_class, nr_feature);
      xmodel = new MaltLiblinearModel(labels, nr_class, wmatrix.length, wmatrix, parameter.getSolverType());
      if (configLogger.isInfoEnabled()) {
        configLogger.info("- Save the Liblinear model "+getFile(".moo").getName()+"\n");
      }
    } catch (OutOfMemoryError e) {
      throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e);
View Full Code Here

    }
    return true;
  }
 
  private Parameter getLiblinearParameters() throws MaltChainedException {
    Parameter param = new Parameter(SolverType.MCSVM_CS, 0.1, 0.1);
    String type = libOptions.get("s");
   
    if (type.equals("0")) {
      param.setSolverType(SolverType.L2R_LR);
    } else if (type.equals("1")) {
      param.setSolverType(SolverType.L2R_L2LOSS_SVC_DUAL);
    } else if (type.equals("2")) {
      param.setSolverType(SolverType.L2R_L2LOSS_SVC);
    } else if (type.equals("3")) {
      param.setSolverType(SolverType.L2R_L1LOSS_SVC_DUAL);
    } else if (type.equals("4")) {
      param.setSolverType(SolverType.MCSVM_CS);
    } else if (type.equals("5")) {
      param.setSolverType(SolverType.L1R_L2LOSS_SVC)
    } else if (type.equals("6")) {
      param.setSolverType(SolverType.L1R_LR)
    } else if (type.equals("7")) {
      param.setSolverType(SolverType.L2R_LR_DUAL)
    } else {
      throw new LibException("The liblinear type (-s) is not an integer value between 0 and 4. ");
    }
    try {
      param.setC(Double.valueOf(libOptions.get("c")).doubleValue());
    } catch (NumberFormatException e) {
      throw new LibException("The liblinear cost (-c) value is not numerical value. ", e);
    }
    try {
      param.setEps(Double.valueOf(libOptions.get("e")).doubleValue());
    } catch (NumberFormatException e) {
      throw new LibException("The liblinear epsilon (-e) value is not numerical value. ", e);
    }
    return param;
  }
View Full Code Here

    ps[0] = 0.1;
    cs = new double[1];
    cs[0] = 1;
    eps = 0.1;
   
    params = new Parameter(solver, cs[0], eps);
  }
View Full Code Here

  public Parameter getParams() {
    return params;
  }
 
  public Parameter getParamsCopy() {
    Parameter p2 = new Parameter(params.getSolverType(), params.getC(), params.getEps());
    if (weights != null) {
      p2.setWeights(params.getWeights(), params.getWeightLabels());
    }   
    p2.setEps(params.getEps());
   
    return p2;
  }
View Full Code Here

    Problem trainProb = null;
    Problem testProb = null;

    Map<Double, Double> cc = null;

    Parameter linearParams = params.getParamsCopy();

    /*
     * We set the weights here, because LibLINEAR crashes when there are weights for labels that are in the total set, but not in the train set.
     * However in the case of cross-validation for parameter optimisation we cannot make sure that this does not happen, since the CV is done by the LibLINEAR CV method.
     * In the future we should implement our own CV. Moreover, we introduce a slight bias in this way, since the weights are computed over train+test in the CV case.
     *
     */
    if (params.isDoCrossValidation()) {
      target = prob.y;
      cc = EvaluationUtils.computeClassCounts(EvaluationUtils.doubles2target(prob.y));

      /*
      if (params.isDoWeightLabels()) {
        linearParams.setWeights(EvaluationUtils.computeWeights(EvaluationUtils.doubles2target(prob.y)), EvaluationUtils.computeWeightLabels(EvaluationUtils.doubles2target(prob.y)));
      }
       */

    } else {
      trainProb = createProblemTrainSplit(prob, params.getSplitFraction());
      testProb  = createProblemTestSplit(prob, params.getSplitFraction());
      target = testProb.y;

      cc = EvaluationUtils.computeClassCounts(EvaluationUtils.doubles2target(trainProb.y));     

      /*
      if (params.isDoWeightLabels()) {
        linearParams.setWeights(EvaluationUtils.computeWeights(EvaluationUtils.doubles2target(trainProb.y)), EvaluationUtils.computeWeightLabels(EvaluationUtils.doubles2target(trainProb.y)));
      }
       */
    }

    if (params.isDoWeightLabels()) {
      List<Double> wl = new ArrayList<Double>();
      List<Integer> wll = new ArrayList<Integer>();
      for (int i = 0; i < params.getWeightLabels().length; i++) {
        if (cc.containsKey((double) params.getWeightLabels()[i])) {
          wll.add(params.getWeightLabels()[i]);
          wl.add(params.getWeights()[i]);
        }
      }
      linearParams.setWeights(EvaluationUtils.target2Doubles(wl), EvaluationUtils.target2Integers(wll));
    }


    double score = 0, bestScore = 0, bestC = 0, bestP = 0;


    for (double p : params.getPs()) {
      linearParams.setP(p);
      for (double c : params.getCs()) {
        linearParams.setC(c);

        if (params.isDoCrossValidation()) {
          prediction = crossValidate(prob, linearParams, params.getNumFolds());

        } else {
          prediction = testLinearModel(new LibLINEARModel(Linear.train(trainProb, linearParams)), testProb.x);
        }

        score = params.getEvalFunction().computeScore(target, prediction);

        if (bestC == 0 || params.getEvalFunction().isBetter(score, bestScore)) {
          bestC = c;
          bestP = p;
          bestScore = score;
       
      }
    }

    linearParams.setC(bestC)
    System.out.println("Training model for C: " + bestC + " and P (SVR only): " + bestP);
    return new LibLINEARModel(Linear.train(prob, linearParams));
  }
View Full Code Here

     *
     * @return the configured svm_parameter object
     */
    protected Parameter getParameters() {

        Parameter parameter = new Parameter(m_SolverType, m_Cost, m_eps);

        if (m_Weight.length > 0) {
            parameter.setWeights(m_Weight, m_WeightLabel);
        }

        return parameter;
    }
View Full Code Here

TOP

Related Classes of de.bwaldvogel.liblinear.Parameter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.