Example usage for edu.stanford.nlp.math ArrayMath innerProduct

List of usage examples for edu.stanford.nlp.math ArrayMath innerProduct

Introduction

In this page you can find the example usage for edu.stanford.nlp.math ArrayMath innerProduct.

Prototype

public static double innerProduct(float[] a, float[] b) 

Source Link

Usage

From source file:cmu.arktweetnlp.impl.OWLQN.java

void mapDirByInverseHessian() {
    int count = sList.size();

    if (count != 0) {
        for (int i = count - 1; i >= 0; i--) {
            //mheilman: The program will try to divide by zero here unless there is a check 
            //that the parameters change at each iteration.  See comments in the minimize() method.
            //A roList value is the inner product of the change in the gradient 
            //and the change in parameters between the current and last iterations.  
            //See the discussion of L-BFGS in Nocedal and Wright's Numerical Optimization book 
            //(though I think that defines rho as the multiplicative inverse of what is here).
            alphas[i] = -ArrayMath.innerProduct(sList.get(i), dir) / roList.get(i);
            ArrayMath.addMultInPlace(dir, yList.get(i), alphas[i]);
        }/*from  w w w .j  a va  2 s  . co  m*/

        double[] lastY = yList.get(count - 1);
        double yDotY = ArrayMath.innerProduct(lastY, lastY);
        double scalar = roList.get(count - 1) / yDotY;
        ArrayMath.multiplyInPlace(dir, scalar);

        for (int i = 0; i < count; i++) {
            double beta = ArrayMath.innerProduct(yList.get(i), dir) / roList.get(i);
            ArrayMath.addMultInPlace(dir, sList.get(i), -alphas[i] - beta);
        }
    }
}

From source file:cmu.arktweetnlp.impl.OWLQN.java

double dirDeriv() {
    if (l1weight == 0) {
        return ArrayMath.innerProduct(dir, grad);
    } else {//  ww  w  . j a va  2 s.  co  m
        double val = 0.0;
        for (int i = 0; i < dim; i++) {
            //mheilman: I added this if-statement to avoid penalizing bias parameters.
            if (OWLQN.biasParameters.contains(i)) {
                val += dir[i] * grad[i];
                continue;
            }
            if (dir[i] != 0) {
                if (x[i] < 0) {
                    val += dir[i] * (grad[i] - l1weight);
                } else if (x[i] > 0) {
                    val += dir[i] * (grad[i] + l1weight);
                } else if (dir[i] < 0) {
                    val += dir[i] * (grad[i] - l1weight);
                } else if (dir[i] > 0) {
                    val += dir[i] * (grad[i] + l1weight);
                }
            }
        }
        return val;
    }
}

From source file:cmu.arktweetnlp.impl.OWLQN.java

void backTrackingLineSearch() {

    double origDirDeriv = dirDeriv();
    // if a non-descent direction is chosen, the line search will break anyway, so throw here
    // The most likely reason for this is a bug in your function's gradient computation
    if (origDirDeriv >= 0) {
        throw new RuntimeException("L-BFGS chose a non-descent direction: check your gradient!");
    }//from ww w.ja  v  a  2s .c  o  m

    double alpha = 1.0;
    double backoff = 0.5;
    if (iter == 1) {
        double normDir = Math.sqrt(ArrayMath.innerProduct(dir, dir));
        alpha = (1 / normDir);
        backoff = 0.1;
    }

    double c1 = 1e-4;
    double oldValue = value;
    while (true) {
        getNextPoint(alpha);
        value = evalL1();

        //mheilman: I think loss of precision can happen here for pathological cases because 
        //origDirDeriv can be many orders of magnitude less than value and oldValue.
        //Then, the right side will just end up being oldValue.
        if (value <= oldValue + c1 * origDirDeriv * alpha) {
            break;
        }

        //mheilman: I added this extra check to keep the program
        //from backing off for a long time until numerical underflow happens.
        //If the line search hasn't found something by 1e-30, it's probably not going to,
        //and I think not having this check may cause the program to crash 
        //for some rare, pathological cases.
        if (alpha < 1e-30) {
            System.err.println(
                    "Warning: The line search backed off to alpha < 1e-30, and stayed with the current parameter values.  This probably means converged has been reached.");
            value = oldValue;
            break;
        }

        if (!quiet)
            System.err.print(".");

        alpha *= backoff;
    }

    if (!quiet)
        System.err.println();
}

From source file:cmu.arktweetnlp.impl.OWLQN.java

void shift() {
    double[] nextS = null, nextY = null;

    int listSize = sList.size();

    if (listSize < m) {
        try {/*  w  w w. ja  v a2s  .c o  m*/
            nextS = new double[dim];
            nextY = new double[dim];
        } catch (OutOfMemoryError e) {
            m = listSize;
            nextS = null;
        }
    }

    if (nextS == null) {
        nextS = sList.poll();
        nextY = yList.poll();
        roList.poll();
    }

    ArrayMath.addMultInto(nextS, newX, x, -1);
    ArrayMath.addMultInto(nextY, newGrad, grad, -1);

    double ro = ArrayMath.innerProduct(nextS, nextY);
    assert (ro != 0.0);

    sList.offer(nextS);
    yList.offer(nextY);
    roList.offer(ro);

    double[] tmpX = newX;
    newX = x;
    x = tmpX;

    // TODO: added: nschneid
    /*if (OWLQN.isConstrained()) {
       newX = OWLQN.projectWeights(newX);
       x = OWLQN.projectWeights(x);
    }*/

    double[] tmpGrad = newGrad;
    newGrad = grad;
    grad = tmpGrad;

    ++iter;
}

From source file:com.gp.extract.twitter.labeler.OWLQN.java

void backTrackingLineSearch() {

    double origDirDeriv = dirDeriv();
    // if a non-descent direction is chosen, the line search will break anyway, so throw here
    // The most likely reason for this is a bug in your function's gradient computation
    if (origDirDeriv >= 0) {
        throw new RuntimeException("L-BFGS chose a non-descent direction: check your gradient!");
    }/*from  ww  w  . jav  a  2 s  . c  om*/

    double alpha = 1.0;
    double backoff = 0.5;
    if (iter == 1) {
        double normDir = Math.sqrt(ArrayMath.innerProduct(dir, dir));
        alpha = (1 / normDir);
        backoff = 0.1;
    }

    double c1 = 1e-4;
    double oldValue = value;
    while (true) {
        getNextPoint(alpha);
        value = evalL1();

        //mheilman: I think loss of precision can happen here for pathological cases because 
        //origDirDeriv can be many orders of magnitude less than value and oldValue.
        //Then, the right side will just end up being oldValue.
        if (value <= oldValue + c1 * origDirDeriv * alpha) {
            break;
        }

        //mheilman: I added this extra check to keep the program
        //from backing off for a long time until numerical underflow happens.
        //If the line search hasn't found something by 1e-30, it's probably not going to,
        //and I think not having this check may cause the program to crash 
        //for some rare, pathological cases.
        if (alpha < 1e-30) {
            System.out.println("Warning: The line search backed off to alpha < 1e-30, and stayed with the "
                    + "current parameter values.  This probably means converged has been reached.");
            value = oldValue;
            break;
        }

        if (!quiet)
            System.out.print(".");

        alpha *= backoff;
    }

    if (!quiet)
        System.out.println();
}

From source file:dz.pfe.storm.ressources.cmu.arktweetnlp.impl.OWLQN.java

synchronized void mapDirByInverseHessian() {
    int count = sList.size();

    if (count != 0) {
        for (int i = count - 1; i >= 0; i--) {
            //mheilman: The program will try to divide by zero here unless there is a check
            //that the parameters change at each iteration.  See comments in the minimize() method.
            //A roList value is the inner product of the change in the gradient
            //and the change in parameters between the current and last iterations.
            //See the discussion of L-BFGS in Nocedal and Wright's Numerical Optimization book
            //(though I think that defines rho as the multiplicative inverse of what is here).
            alphas[i] = -ArrayMath.innerProduct(sList.get(i), dir) / roList.get(i);
            ArrayMath.addMultInPlace(dir, yList.get(i), alphas[i]);
        }/*from  w ww .jav a 2 s . c  o  m*/

        double[] lastY = yList.get(count - 1);
        double yDotY = ArrayMath.innerProduct(lastY, lastY);
        double scalar = roList.get(count - 1) / yDotY;
        ArrayMath.multiplyInPlace(dir, scalar);

        for (int i = 0; i < count; i++) {
            double beta = ArrayMath.innerProduct(yList.get(i), dir) / roList.get(i);
            ArrayMath.addMultInPlace(dir, sList.get(i), -alphas[i] - beta);
        }
    }
}

From source file:dz.pfe.storm.ressources.cmu.arktweetnlp.impl.OWLQN.java

synchronized double dirDeriv() {
    OWLQN owlqn = new OWLQN();
    if (l1weight == 0) {
        return ArrayMath.innerProduct(dir, grad);
    } else {/*  w  w  w. j a va2 s  .  c o  m*/
        double val = 0.0;
        for (int i = 0; i < dim; i++) {
            //mheilman: I added this if-statement to avoid penalizing bias parameters.
            if (owlqn.biasParameters.contains(i)) {
                val += dir[i] * grad[i];
                continue;
            }
            if (dir[i] != 0) {
                if (x[i] < 0) {
                    val += dir[i] * (grad[i] - l1weight);
                } else if (x[i] > 0) {
                    val += dir[i] * (grad[i] + l1weight);
                } else if (dir[i] < 0) {
                    val += dir[i] * (grad[i] - l1weight);
                } else if (dir[i] > 0) {
                    val += dir[i] * (grad[i] + l1weight);
                }
            }
        }
        return val;
    }
}

From source file:dz.pfe.storm.ressources.cmu.arktweetnlp.impl.OWLQN.java

synchronized void backTrackingLineSearch() {

    double origDirDeriv = dirDeriv();
    // if a non-descent direction is chosen, the line search will break anyway, so throw here
    // The most likely reason for this is a bug in your function's gradient computation
    if (origDirDeriv >= 0) {
        throw new RuntimeException("L-BFGS chose a non-descent direction: check your gradient!");
    }//  ww w  . j a  v a2 s.  c  o m

    double alpha = 1.0;
    double backoff = 0.5;
    if (iter == 1) {
        double normDir = Math.sqrt(ArrayMath.innerProduct(dir, dir));
        alpha = (1 / normDir);
        backoff = 0.1;
    }

    double c1 = 1e-4;
    double oldValue = value;
    while (true) {
        getNextPoint(alpha);
        value = evalL1();

        //mheilman: I think loss of precision can happen here for pathological cases because
        //origDirDeriv can be many orders of magnitude less than value and oldValue.
        //Then, the right side will just end up being oldValue.
        if (value <= oldValue + c1 * origDirDeriv * alpha) {
            break;
        }

        //mheilman: I added this extra check to keep the program
        //from backing off for a long time until numerical underflow happens.
        //If the line search hasn't found something by 1e-30, it's probably not going to,
        //and I think not having this check may cause the program to crash
        //for some rare, pathological cases.
        if (alpha < 1e-30) {
            System.err.println(
                    "Warning: The line search backed off to alpha < 1e-30, and stayed with the current parameter values.  This probably means converged has been reached.");
            value = oldValue;
            break;
        }

        if (!quiet)
            System.err.print(".");

        alpha *= backoff;
    }

    if (!quiet)
        System.err.println();
}

From source file:dz.pfe.storm.ressources.cmu.arktweetnlp.impl.OWLQN.java

synchronized void shift() {
    double[] nextS = null, nextY = null;

    int listSize = sList.size();

    if (listSize < m) {
        try {// w w w.j  a  v a  2s  .c  o  m
            nextS = new double[dim];
            nextY = new double[dim];
        } catch (OutOfMemoryError e) {
            m = listSize;
            nextS = null;
        }
    }

    if (nextS == null) {
        nextS = sList.poll();
        nextY = yList.poll();
        roList.poll();
    }

    ArrayMath.addMultInto(nextS, newX, x, -1);
    ArrayMath.addMultInto(nextY, newGrad, grad, -1);

    double ro = ArrayMath.innerProduct(nextS, nextY);
    assert (ro != 0.0);

    sList.offer(nextS);
    yList.offer(nextY);
    roList.offer(ro);

    double[] tmpX = newX;
    newX = x;
    x = tmpX;

    // TODO: added: nschneid
    /*if (OWLQN.isConstrained()) {
       newX = OWLQN.projectWeights(newX);
       x = OWLQN.projectWeights(x);
    }*/

    double[] tmpGrad = newGrad;
    newGrad = grad;
    grad = tmpGrad;

    ++iter;
}