All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.stanford.nlp.classify.LogPrior Maven / Gradle / Ivy

Go to download

Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.

There is a newer version: 4.5.7
Show newest version
package edu.stanford.nlp.classify;

import edu.stanford.nlp.math.ArrayMath;

import java.io.Serializable;


/**
 * A Prior for functions.  Immutable.
 *
 * @author Galen Andrew
 */
public class LogPrior implements Serializable {

  private static final long serialVersionUID = 7826853908892790965L;

  public enum LogPriorType { NULL, QUADRATIC, HUBER, QUARTIC, COSH, ADAPT, MULTIPLE_QUADRATIC }

  public static LogPriorType getType(String name) {
    if (name.equalsIgnoreCase("null")) { return LogPriorType.NULL; }
    else if (name.equalsIgnoreCase("quadratic")) { return LogPriorType.QUADRATIC; }
    else if (name.equalsIgnoreCase("huber")) { return LogPriorType.HUBER; }
    else if (name.equalsIgnoreCase("quartic")) { return LogPriorType.QUARTIC; }
    else if (name.equalsIgnoreCase("cosh")) { return LogPriorType.COSH; }
//    else if (name.equalsIgnoreCase("multiple")) { return LogPriorType.MULTIPLE; }
    else { throw new RuntimeException("Unknown LogPriorType: " + name); }
  }

  // these fields are just for the ADAPT prior -
  // is there a better way to do this?
  private double[] means = null;
  private LogPrior otherPrior = null;

  public static LogPrior getAdaptationPrior(double[] means, LogPrior otherPrior) {
    LogPrior lp = new LogPrior(LogPriorType.ADAPT);
    lp.means = means;
    lp.otherPrior = otherPrior;
    return lp;
  }

  public LogPriorType getType() {
    return type;
  }

  private final LogPriorType type;

  public LogPrior() {
    this(LogPriorType.QUADRATIC);
  }

  public LogPrior(int intPrior) {
    this(intPrior, 1.0, 0.1);
  }

  public LogPrior(LogPriorType type) {
    this(type, 1.0, 0.1);
  }

  // why isn't this functionality in enum?
  private static LogPriorType intToType(int intPrior) {
    LogPriorType[] values = LogPriorType.values();
    for (LogPriorType val : values) {
      if (val.ordinal() == intPrior) {
        return val;
      }
    }
    throw new IllegalArgumentException(intPrior + " is not a legal LogPrior.");
  }

  public LogPrior(int intPrior, double sigma, double epsilon) {
    this(intToType(intPrior), sigma, epsilon);
  }

  public LogPrior(LogPriorType type, double sigma, double epsilon) {
    this.type = type;
    if (type != LogPriorType.ADAPT) {
      setSigma(sigma);
      setEpsilon(epsilon);
    }
  }


  // this is the C variable in CSFoo's MM paper C = 1/\sigma^2
//  private double[] regularizationHyperparameters = null;

  private double[] sigmaSqM = null;
  private double[] sigmaQuM = null;


//  public double[] getRegularizationHyperparameters() {
//    return regularizationHyperparameters;
//  }
//
//  public void setRegularizationHyperparameters(
//      double[] regularizationHyperparameters) {
//    this.regularizationHyperparameters = regularizationHyperparameters;
//  }

  /**
   * IMPORTANT NOTE: This constructor allows non-uniform regularization, but it
   * transforms the inputs C (like the machine learning people like) to sigma
   * (like we NLP folks like).  C = 1/\sigma^2
   */
  public LogPrior(double[] C) {
    this.type = LogPriorType.MULTIPLE_QUADRATIC;
    double[] sigmaSqM = new double[C.length];
    for (int i=0;i 30.0) {
          val = norm - Math.log(2);
          d = 1.0 / sigmaSq;
        } else {
          val = Math.log(Math.cosh(norm));
          d = (2 * (1 / (Math.exp(-2.0 * norm) + 1)) - 1.0) / sigmaSq;
        }
        for (int i=0; i < x.length; i++) {
          grad[i] += Math.signum(x[i]) * d;
        }
        return val;
      case MULTIPLE_QUADRATIC:
//        for (int i = 0; i < x.length; i++) {
//          val += x[i] * x[i]* 1/2 * regularizationHyperparameters[i];
//          grad[i] += x[i] * regularizationHyperparameters[i];
//        }

        for (int i = 0; i < x.length; i++) {
          val += x[i] * x[i] / 2.0 / sigmaSqM[i];
          grad[i] += x[i] / sigmaSqM[i];
        }

        return val;
      default:
        throw new RuntimeException("LogPrior.valueAt is undefined for prior of type " + this);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy