Search in sources :

Example 1 with RandomAccessSparseVector

use of org.apache.mahout.math.RandomAccessSparseVector in project elephant-bird by twitter.

the class VectorWritableConverter method convertSparseVectorDataToVector.

private Vector convertSparseVectorDataToVector(Tuple value) throws IOException {
    Vector v;
    // determine output vector size and fetch bag containing entries from input
    int size = 0;
    DataBag entries = null;
    if (value.size() == 2) {
        // cardinality defined by input
        size = (Integer) value.get(0);
        if (cardinality != null) {
            // cardinality defined by VectorWritableConverter instance
            size = cardinality;
        }
        entries = (DataBag) value.get(1);
    } else {
        Preconditions.checkNotNull(cardinality, "Cardinality is undefined");
        size = cardinality;
        entries = (DataBag) value.get(0);
    }
    // create vector, allowing conversion of sparse input vector data to dense output vector
    if (dense) {
        // TODO(Andy Schlaikjer): Test for OOM before it happens
        v = new DenseVector(size);
    } else {
        // more efficient to build sparse vector with this impl
        v = new RandomAccessSparseVector(size);
    }
    // populate vector
    for (Tuple entry : entries) {
        validateSparseVectorEntryData(entry);
        int i = (Integer) entry.get(0);
        // check index bounds
        if (i < 0 || i >= size) {
            counterHelper.incrCounter(Counter.INDEX_OUT_OF_BOUNDS, 1);
            continue;
        }
        double n = ((Number) entry.get(1)).doubleValue();
        v.setQuick(i, n);
    }
    // convert to (sparse) sequential vector if requested
    if (sequential) {
        v = new SequentialAccessSparseVector(v);
    }
    return v;
}
Also used : RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) DataBag(org.apache.pig.data.DataBag) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector) DenseVector(org.apache.mahout.math.DenseVector) RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector) Tuple(org.apache.pig.data.Tuple) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector)

Example 2 with RandomAccessSparseVector

use of org.apache.mahout.math.RandomAccessSparseVector in project pyramid by cheng-li.

the class CBM method toString.

public String toString() {
    Vector vector = new RandomAccessSparseVector(numFeatures);
    double[] mixtureCoefficients = multiClassClassifier.predictClassProbs(vector);
    final StringBuilder sb = new StringBuilder("CBM{\n");
    sb.append("numLabels=").append(numLabels).append("\n");
    sb.append("numComponents=").append(numComponents).append("\n");
    for (int k = 0; k < numComponents; k++) {
        sb.append("cluster ").append(k).append(":\n");
        sb.append("proportion = ").append(mixtureCoefficients[k]).append("\n");
    }
    sb.append("multi-class component = \n");
    sb.append(multiClassClassifier);
    sb.append("binary components = \n");
    for (int k = 0; k < numComponents; k++) {
        for (int l = 0; l < numLabels; l++) {
            sb.append("component ").append(k).append(" class ").append(l).append("\n");
            sb.append(binaryClassifiers[k][l]).append("\n");
        }
    }
    sb.append('}');
    return sb.toString();
}
Also used : RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) DenseVector(org.apache.mahout.math.DenseVector) RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) Vector(org.apache.mahout.math.Vector)

Example 3 with RandomAccessSparseVector

use of org.apache.mahout.math.RandomAccessSparseVector in project pyramid by cheng-li.

the class Vectors method concatenate.

public static Vector concatenate(Vector vector, Vector vector2) {
    Vector con = null;
    if (vector instanceof DenseVector) {
        con = new DenseVector(vector.size() + vector2.size());
    }
    if (vector instanceof RandomAccessSparseVector) {
        con = new RandomAccessSparseVector(vector.size() + vector2.size());
    }
    if (vector instanceof SequentialAccessSparseVector) {
        con = new SequentialAccessSparseVector(vector.size() + vector2.size());
    }
    for (Vector.Element nonZeros : vector.nonZeroes()) {
        int index = nonZeros.index();
        double value = nonZeros.get();
        con.set(index, value);
    }
    for (Vector.Element nonZeros : vector2.nonZeroes()) {
        int index = nonZeros.index();
        double value = nonZeros.get();
        con.set(index + vector.size(), value);
    }
    return con;
}
Also used : RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) DenseVector(org.apache.mahout.math.DenseVector) RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector)

Example 4 with RandomAccessSparseVector

use of org.apache.mahout.math.RandomAccessSparseVector in project pyramid by cheng-li.

the class Vectors method concatenate.

public static Vector concatenate(Vector vector, double[] numbers) {
    Vector con = null;
    if (vector instanceof DenseVector) {
        con = new DenseVector(vector.size() + numbers.length);
    }
    if (vector instanceof RandomAccessSparseVector) {
        con = new RandomAccessSparseVector(vector.size() + numbers.length);
    }
    if (vector instanceof SequentialAccessSparseVector) {
        con = new SequentialAccessSparseVector(vector.size() + numbers.length);
    }
    for (Vector.Element nonZeros : vector.nonZeroes()) {
        int index = nonZeros.index();
        double value = nonZeros.get();
        con.set(index, value);
    }
    for (int i = 0; i < numbers.length; i++) {
        con.set(i + vector.size(), numbers[i]);
    }
    return con;
}
Also used : RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) DenseVector(org.apache.mahout.math.DenseVector) RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector)

Example 5 with RandomAccessSparseVector

use of org.apache.mahout.math.RandomAccessSparseVector in project pyramid by cheng-li.

the class BackTrackingLineSearcher method moveAlongDirection.

/**
 * move to a new position along the direction
 */
public MoveInfo moveAlongDirection(Vector searchDirection) {
    Vector localSearchDir;
    if (logger.isDebugEnabled()) {
        logger.debug("start line search");
        // don't want to show too much; only show it on small problems
        if (searchDirection.size() < 100) {
            logger.debug("direction=" + searchDirection);
        }
    }
    MoveInfo moveInfo = new MoveInfo();
    double stepLength = initialStepLength;
    double value = function.getValue();
    moveInfo.setOldValue(value);
    Vector gradient = function.getGradient();
    double product = gradient.dot(searchDirection);
    if (product < 0) {
        localSearchDir = searchDirection;
    } else {
        if (logger.isWarnEnabled()) {
            logger.warn("Bad search direction! Use negative gradient instead. Product of gradient and search direction = " + product);
        }
        localSearchDir = gradient.times(-1);
    }
    Vector initialPosition;
    // keep a copy of initial parameters
    if (function.getParameters().isDense()) {
        initialPosition = new DenseVector(function.getParameters());
    } else {
        initialPosition = new RandomAccessSparseVector(function.getParameters());
    }
    while (true) {
        Vector step = localSearchDir.times(stepLength);
        Vector target = initialPosition.plus(step);
        function.setParameters(target);
        double targetValue = function.getValue();
        if (logger.isDebugEnabled()) {
            logger.debug("step length = " + stepLength + ", target value = " + targetValue);
        // logger.debug("requirement = "+(value + c*stepLength*product));
        }
        // todo: if equal ok?
        if ((targetValue <= value + c * stepLength * product && value < Double.POSITIVE_INFINITY) || stepLength == 0) {
            moveInfo.setStep(step);
            moveInfo.setStepLength(stepLength);
            moveInfo.setNewValue(targetValue);
            break;
        }
        stepLength *= shrinkage;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("line search done. " + moveInfo);
    }
    return moveInfo;
}
Also used : RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) DenseVector(org.apache.mahout.math.DenseVector) RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector)

Aggregations

DenseVector (org.apache.mahout.math.DenseVector)9 RandomAccessSparseVector (org.apache.mahout.math.RandomAccessSparseVector)9 Vector (org.apache.mahout.math.Vector)8 SequentialAccessSparseVector (org.apache.mahout.math.SequentialAccessSparseVector)7 DataBag (org.apache.pig.data.DataBag)1 Tuple (org.apache.pig.data.Tuple)1