Search in sources :

Example 1 with SequentialAccessSparseVector

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

the class CMLCRFElasticNet method iterate.

public void iterate() {
    //        System.out.println("weights: " + cmlcrf.getWeights().getAllWeights());
    // O(NdL)
    //        System.out.println(Arrays.toString(cmlcrf.getCombinationLabelPartScores()));
    updateClassScoreMatrix();
    cmlcrf.updateCombLabelPartScores();
    updateAssignmentScoreMatrix();
    updateAssignmentProbMatrix();
    updateCombProbSums();
    updatePredictedCounts();
    updateClassProbMatrix();
    // update for each support label set
    Vector accumulateWeights = new SequentialAccessSparseVector(numParameters);
    Vector oldWeights = cmlcrf.getWeights().deepCopy().getAllWeights();
    for (int l = 0; l < numSupport; l++) {
        //            System.out.println("label: " + supportedCombinations.get(l));
        DataSet newData = expandData(l);
        iterateForOneComb(newData, l);
        accumulateWeights = accumulateWeights.plus(cmlcrf.getWeights().getAllWeights());
        cmlcrf.getWeights().setWeightVector(oldWeights);
    }
    // lineSearch
    if (true) {
        Vector searchDirection = accumulateWeights;
        Vector gradient = this.predictedCounts.minus(empiricalCounts).divide(numData);
        lineSearch(searchDirection, gradient);
    }
    this.terminator.add(getValue());
}
Also used : SequentialSparseDataSet(edu.neu.ccs.pyramid.dataset.SequentialSparseDataSet) MultiLabelClfDataSet(edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet) DataSet(edu.neu.ccs.pyramid.dataset.DataSet) DenseVector(org.apache.mahout.math.DenseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector) Vector(org.apache.mahout.math.Vector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector)

Example 2 with SequentialAccessSparseVector

use of org.apache.mahout.math.SequentialAccessSparseVector 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 3 with SequentialAccessSparseVector

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

the class SerializableVectorTest method test1.

private static void test1() throws Exception {
    Vector vector = new SequentialAccessSparseVector(10);
    vector.set(0, 2);
    vector.set(5, 8);
    SerializableVector serializableVector = new SerializableVector(vector);
    Serialization.serialize(serializableVector, new File(TMP, "v.ser"));
    Vector loaded = ((SerializableVector) Serialization.deserialize(new File(TMP, "v.ser"))).getVector();
    System.out.println(loaded.size());
    System.out.println(loaded.getClass().getName());
    System.out.println(loaded);
}
Also used : DenseVector(org.apache.mahout.math.DenseVector) RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector) Vector(org.apache.mahout.math.Vector) File(java.io.File) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector)

Example 4 with SequentialAccessSparseVector

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

the class Vectors method concatenate.

public static Vector concatenate(Vector vector, double number) {
    Vector con = null;
    if (vector instanceof DenseVector) {
        con = new DenseVector(vector.size() + 1);
    }
    if (vector instanceof RandomAccessSparseVector) {
        con = new RandomAccessSparseVector(vector.size() + 1);
    }
    if (vector instanceof SequentialAccessSparseVector) {
        con = new SequentialAccessSparseVector(vector.size() + 1);
    }
    for (Vector.Element nonZeros : vector.nonZeroes()) {
        int index = nonZeros.index();
        double value = nonZeros.get();
        con.set(index, value);
    }
    con.set(con.size() - 1, number);
    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 SequentialAccessSparseVector

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

the class SerializableVector method readObject.

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    if (type == Type.DENSE) {
        double[] values = (double[]) in.readObject();
        vector = new DenseVector(values);
    } else if (type == Type.SPARSE_RANDOM) {
        int[] indices = (int[]) in.readObject();
        double[] values = (double[]) in.readObject();
        vector = new RandomAccessSparseVector(size);
        for (int i = 0; i < indices.length; i++) {
            vector.set(indices[i], values[i]);
        }
    } else if (type == Type.SPARSE_SEQUENTIAL) {
        int[] indices = (int[]) in.readObject();
        double[] values = (double[]) in.readObject();
        vector = new SequentialAccessSparseVector(size);
        for (int i = 0; i < indices.length; i++) {
            vector.set(indices[i], values[i]);
        }
    }
}
Also used : RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) DenseVector(org.apache.mahout.math.DenseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector)

Aggregations

DenseVector (org.apache.mahout.math.DenseVector)5 SequentialAccessSparseVector (org.apache.mahout.math.SequentialAccessSparseVector)5 RandomAccessSparseVector (org.apache.mahout.math.RandomAccessSparseVector)4 Vector (org.apache.mahout.math.Vector)4 DataSet (edu.neu.ccs.pyramid.dataset.DataSet)1 MultiLabelClfDataSet (edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet)1 SequentialSparseDataSet (edu.neu.ccs.pyramid.dataset.SequentialSparseDataSet)1 File (java.io.File)1