Search in sources :

Example 6 with SparseDoubleVector

use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.

the class DoubleVectorTest method timesBySparseTest.

@Test
public void timesBySparseTest() {
    SparseDoubleVector vector = new SparseDoubleVector(10);
    vector.set(1, 1.0);
    vector.set(3, 3.0);
    vector.set(5, 5.0);
    vector.timesBy(2.0);
    Assert.assertEquals(2.0, vector.get(1), 0.0);
    Assert.assertEquals(6.0, vector.get(3), 0.0);
    Assert.assertEquals(10.0, vector.get(5), 0.0);
}
Also used : SparseDoubleVector(com.tencent.angel.ml.math.vector.SparseDoubleVector) Test(org.junit.Test)

Example 7 with SparseDoubleVector

use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.

the class DoubleVectorTest method sparseDoubleVectorTest.

@Test
public void sparseDoubleVectorTest() {
    int dim = 1000;
    int[] indices = new int[dim];
    double[] values = new double[dim];
    Random random = new Random(Time.monotonicNow());
    for (int i = 0; i < dim; i++) {
        indices[i] = random.nextInt(dim);
        values[i] = random.nextInt(dim);
    }
    SparseDoubleVector vector = new SparseDoubleVector(dim, indices, values);
    int[] indices2 = vector.getIndices();
    double[] values2 = vector.getValues();
    for (int i = 0; i < indices2.length; i++) {
        Assert.assertEquals(vector.get(indices2[i]), values2[i], 0.0);
    }
}
Also used : Random(java.util.Random) SparseDoubleVector(com.tencent.angel.ml.math.vector.SparseDoubleVector) Test(org.junit.Test)

Example 8 with SparseDoubleVector

use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.

the class GetValueOfIndexTask method denseDouble.

public void denseDouble(TaskContext tContext) {
    long startGen = System.currentTimeMillis();
    int[] index = genIndexs(feaNum, nnz);
    long cost = System.currentTimeMillis() - startGen;
    LOG.info("Gen index cost: " + cost + " ms.");
    try {
        MatrixClient dMatClient = tContext.getMatrix(DENSE_DOUBLE_MAT);
        // Set PS Model values
        long startInc = System.currentTimeMillis();
        DenseDoubleVector delt = new DenseDoubleVector(feaNum);
        for (int i = 0; i < feaNum; i++) delt.set(i, (double) i);
        dMatClient.increment(0, delt);
        dMatClient.clock().get();
        long costInc = System.currentTimeMillis() - startInc;
        LOG.info("Increment delt cost " + costInc + " ms.");
        // Wait for all tasks finish this clock
        dMatClient.getTaskContext().globalSync(dMatClient.getMatrixId());
        // Get values of index array
        long startGet = System.currentTimeMillis();
        IndexGetFunc func = new IndexGetFunc(new IndexGetParam(tContext.getMatrix(DENSE_DOUBLE_MAT).getMatrixId(), 0, index));
        SparseDoubleVector row = (SparseDoubleVector) ((GetRowResult) dMatClient.get(func)).getRow();
        long costGet = System.currentTimeMillis() - startGet;
        LOG.info("Get row of indexs cost " + costGet + " ms.");
    } catch (Throwable e) {
        throw new AngelException(e);
    }
}
Also used : AngelException(com.tencent.angel.exception.AngelException) DenseDoubleVector(com.tencent.angel.ml.math.vector.DenseDoubleVector) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) IndexGetFunc(com.tencent.angel.ml.matrix.psf.get.enhance.indexed.IndexGetFunc) IndexGetParam(com.tencent.angel.ml.matrix.psf.get.enhance.indexed.IndexGetParam) SparseDoubleVector(com.tencent.angel.ml.math.vector.SparseDoubleVector)

Example 9 with SparseDoubleVector

use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.

the class DoubleVectorTest method densePlusBySparseTest.

@Test
public void densePlusBySparseTest() {
    DenseDoubleVector vector = new DenseDoubleVector(10);
    vector.set(1, 1.0);
    vector.set(3, 3.0);
    vector.set(5, 5.0);
    SparseDoubleVector sparseDoubleVector = new SparseDoubleVector(10);
    sparseDoubleVector.set(1, 1.0);
    sparseDoubleVector.set(3, 3.0);
    sparseDoubleVector.set(7, 7.0);
    vector.plusBy(sparseDoubleVector, 1.0);
    Assert.assertEquals(2.0, vector.get(1), 0.0);
    Assert.assertEquals(6.0, vector.get(3), 0.0);
    Assert.assertEquals(5.0, vector.get(5), 0.0);
    Assert.assertEquals(7.0, vector.get(7), 0.0);
}
Also used : DenseDoubleVector(com.tencent.angel.ml.math.vector.DenseDoubleVector) SparseDoubleVector(com.tencent.angel.ml.math.vector.SparseDoubleVector) Test(org.junit.Test)

Example 10 with SparseDoubleVector

use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.

the class DoubleVectorTest method plusBySparseTest.

@Test
public void plusBySparseTest() {
    SparseDoubleVector vector = new SparseDoubleVector(10);
    vector.set(1, 1.0);
    vector.set(3, 3.0);
    vector.set(5, 5.0);
    SparseDummyVector dummyVector = new SparseDummyVector(10);
    dummyVector.set(1, 1.0);
    dummyVector.set(3, 1.0);
    vector.plusBy(dummyVector, 1.0);
    Assert.assertEquals(2.0, vector.get(1), 0.0);
    Assert.assertEquals(4.0, vector.get(3), 0.0);
    Assert.assertEquals(5.0, vector.get(5), 0.0);
}
Also used : SparseDummyVector(com.tencent.angel.ml.math.vector.SparseDummyVector) SparseDoubleVector(com.tencent.angel.ml.math.vector.SparseDoubleVector) Test(org.junit.Test)

Aggregations

SparseDoubleVector (com.tencent.angel.ml.math.vector.SparseDoubleVector)11 Test (org.junit.Test)7 AngelException (com.tencent.angel.exception.AngelException)3 DenseDoubleVector (com.tencent.angel.ml.math.vector.DenseDoubleVector)3 MatrixClient (com.tencent.angel.psagent.matrix.MatrixClient)3 IndexGetFunc (com.tencent.angel.ml.matrix.psf.get.enhance.indexed.IndexGetFunc)2 IndexGetParam (com.tencent.angel.ml.matrix.psf.get.enhance.indexed.IndexGetParam)2 TVector (com.tencent.angel.ml.math.TVector)1 SparseDummyVector (com.tencent.angel.ml.math.vector.SparseDummyVector)1 TDoubleVector (com.tencent.angel.ml.math.vector.TDoubleVector)1 Random (java.util.Random)1