コード例 #1
0
 public LinearClassifier createLinearClassifier(double[] weights) {
   double[][] weights2D;
   if (objective != null) {
     weights2D = objective.to2D(weights);
   } else {
     weights2D = ArrayUtils.to2D(weights, featureIndex.size(), labelIndex.size());
   }
   return new LinearClassifier<L, F>(weights2D, featureIndex, labelIndex);
 }
コード例 #2
0
    public Double apply(Double sigmaToTry) {
      double[][] weights2D;
      setSigma(sigmaToTry);

      weights2D = trainWeights(trainSet, weights, true); // bypass.

      weights = ArrayUtils.flatten(weights2D);

      LinearClassifier<L, F> classifier =
          new LinearClassifier<L, F>(weights2D, trainSet.featureIndex, trainSet.labelIndex);

      double score = scorer.score(classifier, devSet);
      // System.out.println("score: "+score);
      // System.out.print(".");
      System.err.print("##sigma = " + getSigma() + " ");
      System.err.println("-> average Score: " + score);
      System.err.println("##time elapsed: " + timer.stop() + " milliseconds.");
      timer.restart();
      return -score;
    }
コード例 #3
0
  /**
   * Sets the sigma parameter to a value that optimizes the held-out score given by <code>scorer
   * </code>. Search for an optimal value is carried out by <code>minimizer</code> dataset the data
   * set to optimize sigma on. kfold
   *
   * @return an interim set of optimal weights: the weights
   */
  public double[] heldOutSetSigma(
      final GeneralDataset<L, F> trainSet,
      final GeneralDataset<L, F> devSet,
      final Scorer<L> scorer,
      LineSearcher minimizer) {

    featureIndex = trainSet.featureIndex;
    labelIndex = trainSet.labelIndex;
    // double[] resultWeights = null;
    Timing timer = new Timing();

    NegativeScorer negativeScorer = new NegativeScorer(trainSet, devSet, scorer, timer);

    timer.start();
    double bestSigma = minimizer.minimize(negativeScorer);
    System.err.println("##best sigma: " + bestSigma);
    setSigma(bestSigma);

    return ArrayUtils.flatten(
        trainWeights(
            trainSet,
            negativeScorer.weights,
            true)); // make sure it's actually the interim weights from best sigma
  }
コード例 #4
0
 public LinearClassifier<L, F> trainClassifierWithInitialWeights(
     GeneralDataset<L, F> dataset, double[][] initialWeights2D) {
   double[] initialWeights =
       (initialWeights2D != null) ? ArrayUtils.flatten(initialWeights2D) : null;
   return trainClassifier(dataset, initialWeights);
 }